Skip to content

Commit

Permalink
Add Gitpan::Config to deal with finding and reading the config file.
Browse files Browse the repository at this point in the history
I'm not 100% sure how overlay merging is going to behave with anything complicated,
but we'll deal with that when it comes up.

Right now the only thing we need is Github logins for testing and production.
Seemed like it made more sense to have the overlays separate than to have "test"
and "live" versions of every key.  We'll see how that turns out.
  • Loading branch information
schwern committed Jan 14, 2013
1 parent f9eb02b commit 5886e2c
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Build.PL
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@ my $build = MyBuilder->new(
"Path::Class" => 0,
"perl5i::2" => "v2.2.0",
"Net::GitHub::V3" => 0,
"YAML::XS" => 0,
},

test_requires => {
"Test::Most" => 0,
"Test::TypeConstraints" => 0.05,
},
);
Expand Down
82 changes: 82 additions & 0 deletions lib/Gitpan/Config.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package Gitpan::Config;

use Mouse;
use Gitpan::Types;
use perl5i::2;
use Method::Signatures;
use Path::Class;

use YAML::XS qw(LoadFile);

has config_filename =>
is => 'ro',
isa => 'Str',
default => ".gitpan";

# Search from first to last.
has search_dirs =>
is => 'ro',
isa => 'ArrayRef[Path::Class::Dir]',
default => method {
return [dir("."), dir($ENV{HOME})]
};

has config_file =>
is => 'ro',
isa => 'Maybe[Path::Class::File]',
lazy => 1,
builder => 'search_for_config_file';

has config =>
is => 'ro',
isa => 'HashRef',
lazy => 1,
builder => 'read_config_file';

has is_test =>
is => 'ro',
isa => 'Bool',
default => 1;

has use_overlays =>
is => 'ro',
isa => 'ArrayRef',
lazy => 1,
default => method {
return $self->is_test ? ["test"] : [];
};

method search_for_config_file {
my $filename = $self->config_filename;
my $dirs = $self->search_dirs;

if( my $dir = $dirs->first(sub{ -e file($_, $filename) }) ) {
return file($dir, $filename);
}
else {
return;
}
}

method read_config_file {
if( my $file = $self->config_file ) {
return $self->_apply_overlays( LoadFile( $file ) );
}
else {
return {};
}
}

method _apply_overlays( HashRef $config ) {
# Don't want them in the final config.
my $overlays = delete $config->{overlays};

$self->use_overlays->foreach( func($key) {
my $overlay = $overlays->{$key};
return unless $overlay;

$config = $config->merge($overlay);
});

return $config;
}
60 changes: 60 additions & 0 deletions t/config.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#!/usr/bin/env perl

use perl5i::2;
use Test::Most;
use Path::Class;
use YAML::XS qw(DumpFile);

my $CLASS = 'Gitpan::Config';
require_ok $CLASS;

subtest defaults => sub {
my $config = new_ok $CLASS;

ok $config->is_test;
is $config->config_filename, ".gitpan";
is_deeply $config->search_dirs, [".", $ENV{HOME}];
isa_ok $config->config, 'HASH', "config should always return something";
is_deeply $config->use_overlays, ["test"];
} or BAIL_OUT("config didn't pass basic tests, this is bad");


subtest read_config => sub {
my $tempdir = Path::Class::tempdir;
my $config_file = $tempdir->file("test.gitpan");

my $config_data = {
github => { token => "123abc" },
overlays => { foo => { github => { token => "deadbeef" } } }
};
DumpFile($config_file, $config_data);

my $config = new_ok $CLASS, [
config_filename => 'test.gitpan',
search_dirs => [$tempdir],
];

is $config->config_file, $config_file;
is_deeply $config->config, { github => { token => "123abc" } };
};

subtest overlays => sub {
my $tempdir = Path::Class::tempdir;
my $config_file = $tempdir->file("test.gitpan");

my $config_data = {
github => { token => "123abc", foo => "bar" },
overlays => { test => { github => { token => "deadbeef" } } }
};
DumpFile($config_file, $config_data);

my $config = new_ok $CLASS, [
config_filename => 'test.gitpan',
search_dirs => [$tempdir],
];

is $config->config_file, $config_file;
is_deeply $config->config, { github => { token => "deadbeef", foo => "bar" } };
};

done_testing;

0 comments on commit 5886e2c

Please sign in to comment.