Activator
view release on metacpan or search on metacpan
lib/Activator/Options.pm view on Meta::CPAN
# clean up key
$key =~ s/^--?//;
# clean up value, if quoted
if ( defined $value ) {
$value =~ s/^"//;
$value =~ s/"$//;
}
return ( $key, $value );
}
# Merge config files into this objects Activator::Registry object
sub _process_config_for {
my ( $pkg, $realm ) = @_;
my $self = &new( @_ );
# figure out what project we are working on
my $project =
$self->{ARGV}->{project} ||
$ENV{ACT_OPT_project} ||
Activator::Exception::Options->throw( 'project', 'missing' );
# assemble a list of paths to look for config files
# TODO: look in all these places:
# --conf_path
# ACT_OPT_conf_path
# $ENV{ACT_OPT_project_home}/.<$ENV{ACT_OPT_project}>.d/
# $ENV{HOME}/.<$ENV{ACT_OPT_project}>.d/
# $ENV{HOME}/.activator.d/
# /etc/<$ENV{ACT_OPT_project}>.d/
# /etc/activator.d/
#
# assemble a list of files to process into the keys of $seach_paths
# TODO: assemble list of files for each of the above dirs
# --conf_file= : use $self->{ARGV}->{conf_file} (which could be an arrayref )
# ACT_OPT_conf_file= : comma separated list of files
# $ENV{USER}.yml
# <realm>.yml - realm specific settings and defaults
# <project>.yml - project specific settings and defaults
# org.yml - top level organization settings and defaults
# For now, just use ~/.activator.d/$project : key/value is path/'where
# found', 'where found' being one of: hardcoded, env, or arg
my $dir = $self->{ARGV}->{conf_path} ||
$ENV{ACT_OPT_conf_path} ||
Activator::Exception::Options->throw( 'conf_path', 'missing');
my $search_paths = { $dir => 'arg' };
my $files = { user => { target => "$ENV{USER}.yml" },
realm => { target => "${realm}.yml" },
project => { target => "${project}.yml" },
org => { target => 'org.yml' } };
foreach my $path ( keys %$search_paths ) {
$path =~ s|/$||;
foreach my $which ( keys %$files ) {
my $target = $files->{ $which }->{target};
if ( !opendir DIR, $path ) {
# TODO: enhance this note to say where this path was detected
WARN( "Ignoring invalid path '$path'" );
}
else {
my @found = grep { /^$target$/ && -f "$path/$_" } readdir(DIR);
if ( @found ) {
my $file = "$path/$found[0]";
if ( !exists( $files->{ $which }->{ file } ) ) {
$files->{ $which }->{file} = $file;
}
else {
# TODO: enhance this note to say where this path was detected
INFO( "Ignoring lower priority config file '$file'" );
}
}
}
}
}
# now that we have all the files, import 'em! This is a super long
# winded but safe "left precedence" merge of all files
my ( $user_config, $realm_config, $project_config, $org_config );
try eval {
if( exists( $files->{user}->{file} ) ) {
$user_yml = YAML::Syck::LoadFile( $files->{user}->{file} );
}
};
if ( catch my $e ) {
Activator::Exception::Options->throw( 'user_config', 'invalid', $e );
}
try eval {
if( exists( $files->{realm}->{file} ) ) {
$realm_yml = YAML::Syck::LoadFile( $files->{realm}->{file} );
}
};
if ( catch my $e ) {
Activator::Exception::Options->throw( 'realm_config', 'invalid', $e );
}
try eval {
if( exists( $files->{project}->{file} ) ) {
$project_yml = YAML::Syck::LoadFile( $files->{project}->{file} );
}
};
if ( catch my $e ) {
Activator::Exception::Options->throw( 'project_config', 'invalid', $e );
}
try eval {
if( exists( $files->{org}->{file} ) ) {
$org_yml = YAML::Syck::LoadFile( $files->{org}->{file} );
}
};
if ( catch my $e ) {
Activator::Exception::Options->throw( 'org_config', 'invalid', $e );
}
if ( defined( $user_yml ) && exists( $user_yml->{ $realm } ) ) {
$self->{REGISTRY}->register_hash( 'left', $user_yml->{ $realm }, $realm );
}
if ( defined( $realm_yml ) && exists( $realm_yml->{ $realm } ) ) {
$self->{REGISTRY}->register_hash( 'left', $realm_yml->{ $realm }, $realm );
}
if ( defined( $project_yml ) && exists( $project_yml->{ $realm } ) ) {
$self->{REGISTRY}->register_hash( 'left', $project_yml->{ $realm }, $realm );
}
if ( defined( $org_yml ) && exists( $org_yml->{ $realm } ) ) {
( run in 0.420 second using v1.01-cache-2.11-cpan-d7a12ab2c7f )