-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Gitpan::Config to deal with finding and reading the config file.
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
Showing
3 changed files
with
144 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |