Skip to content

Instantly share code, notes, and snippets.

@theory
Last active December 30, 2015 09:39
Show Gist options
  • Select an option

  • Save theory/7810971 to your computer and use it in GitHub Desktop.

Select an option

Save theory/7810971 to your computer and use it in GitHub Desktop.
# If two params, first is URI, second is target.
# If two params and either --to-target or --database is passed, it's an error.
# If one param:
# - If --to-target is passed, it's a database
# - If --database is passed, it's a target
# - If both --to-target and --database are passed, it's an error.
# - If contains ":", it's a URI; pass it through
# - If it is a database key name, return the URI value
# - If it's part of the plan, pass it as part of the plan.
# - Otherwise throw an exception.
sub _args {
my ($self, @params) = @_;
my ($uri, $target);
if (@params) {
if (@params == 1) {
my ($thing) = @params;
if ($self->database) {
# Die if both params were specified.
hurl deploy => __ 'Cannot detangle multiple database and target params'
if $self->to_target;
# Otherwise, it's a target.
return (undef, $thing);
$target = $thing;
} elsif ($thing =~ /:/) {
# It's a database URI.
return (URI::db->new($thing), undef);
} elsif (my $uri = $self->db_for_key($thing)) {
# It's a database.
return ($uri, undef);
} elsif ($self->to_target) {
# It's supposed to be a database.
return (URI::db->new($thing), undef);
} elsif ( defined try { $self->sqitch->plan->index_of($thing) } ) {
# It's a target.
return (undef, $thing);
} else {
hurl deploy => __x(
'Argument "{arg}" does not appear to be a change name or database URI',
arg => $thing
);
}
} elsif (@params >= 2) {
my ($db, $target) = @params;
# Die if we have two databases.
hurl deploy => join(
"\n"
__('Cannot determine which databases to use:'),
' ' . $self->database,
" $db",
__('Try specifying only one'),
) if $self->database;
# Die if we have two targets.
hurl deploy => join(
"\n"
__('Cannot determine which target to use:'),
' ' . $self->to_target,
" $target",
__('Try specifying only one'),
) if $self->to_target;
if ($db !~ /:/) {
$db = $self->db_for_key($db) or hurl deploy => __x(
'"{dbkey}" does not appear to be a known database',
dbkey => $db
);
}
return ($db, $target);
}
} else {
$db = $self->database;
if ($db && $db !~ /:/) {
$db = $self->db_for_key($db) or hurl deploy => __x(
'"{dbkey}" does not appear to be a known database',
dbkey => $db
);
}
return ($db, $self->to_target);
}
}
sub db_for_key {
my ($self, $key) = @_;
my $db = $self->sqitch->config->get( key => "database.$key" ) or return;
return URI::db->new($db);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment