Skip to content

Commit

Permalink
command line arguments override session file
Browse files Browse the repository at this point in the history
  • Loading branch information
nichtich committed Jul 2, 2014
1 parent 842457c commit dbbd3d3
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 23 deletions.
4 changes: 2 additions & 2 deletions dist.ini
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = App-PAIA
license = Perl_5
version = 0.26
copyright_year = 2013
version = 0.27
copyright_year = 2014
author = Jakob Voß
copyright_holder = Jakob Voß

Expand Down
31 changes: 18 additions & 13 deletions lib/App/PAIA/Command.pm
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,16 @@ sub has {
}
}

sub option {
sub explicit_option {
my ($self, $name) = @_;
$self->app->global_options->{$name} # command line
// $self->session->get($name) # session file
$self->app->global_options->{$name} # command line
// $self->config->get($name); # config file
}

sub explicit_option {
sub preferred_option {
my ($self, $name) = @_;
$self->app->global_options->{$name} # command line
// $self->config->get($name); # config file
$self->explicit_option($name) # command line or config file
// $self->session->get($name); # session file
}

has config => (
Expand All @@ -66,7 +65,7 @@ has session => (
has agent => (
default => sub {
App::PAIA::Agent->new(
insecure => $_[0]->option('insecure'),
insecure => $_[0]->preferred_option('insecure'),
logger => $_[0]->logger,
dumper => $_[0]->dumper,
);
Expand All @@ -91,31 +90,37 @@ has dumper => (

has auth => (
default => sub {
$_[0]->option('auth') // ( $_[0]->base ? $_[0]->base . '/auth' : undef )
my ($base) = map { s{/$}{}; $_ } ($_[0]->preferred_option('base') // '');
$_[0]->explicit_option('auth')
// ($base ? "$base/auth" : undef)
// $_[0]->session->get('auth');
}
);

has core => (
default => sub {
$_[0]->option('core') // ( $_[0]->base ? $_[0]->base . '/core' : undef )
my ($base) = map { s{/$}{}; $_ } ($_[0]->preferred_option('base') // '');
$_[0]->explicit_option('core')
// ($base ? "$base/core" : undef)
// $_[0]->session->get('core');
}
);

has base => (
default => sub { $_[0]->option('base') },
default => sub { $_[0]->preferred_option('base') },
coerce => sub { my ($b) = @_; $b =~ s!/$!!; $b; }
);

has patron => (
default => sub { $_[0]->option('patron') }
default => sub { $_[0]->preferred_option('patron') }
);

has scope => (
default => sub { $_[0]->option('scope') }
default => sub { $_[0]->preferred_option('scope') }
);

has token => (
default => sub { $_[0]->option('access_token') }
default => sub { $_[0]->preferred_option('access_token') }
);

has username => (
Expand Down
18 changes: 10 additions & 8 deletions t/15-session.t
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,20 @@ is output, <<OUT, 'auto-login with session';
# GET https://example.org/core/8362432
OUT

# config file overrides session ?
paia qw(session);
is output, "session looks fine.\n", "session looks fine";

paia qw(config -c test.json base http://example.com/paia);
paia qw(config -c test.json);
is output, <<OUT, 'config';
{
"base" : "http://example.com/paia"
}
paia qw(patron -b https://example.com/ -v -q);
is output, <<OUT, 'command line arguments override session file';
# loaded session file paia-session.json
# saved session file paia-session.json
# GET https://example.com/core/8362432
OUT

# TODO: config or session ?
paia qw(config -c test.json base http://example.com/paia);
paia qw(config -c test.json);
my $output = output;
$output =~ s/[\n\t ]+//gm;
is $output, '{"base":"http://example.com/paia"}', 'config';

done_paia_test;

0 comments on commit dbbd3d3

Please sign in to comment.