Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace strings in Dancer2 config with environment variables #1629

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions lib/Dancer2/Core/Role/ConfigReader.pm
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,12 @@ sub load_config_file {
}

# TODO handle mergeable entries

if( defined $config->{'env_var_replace'} && $config->{'env_var_replace'} ) {
warn "Apply environment variables to config\n" if $ENV{DANCER_CONFIG_VERBOSE};
$self->_replace_env_vars($config);
}

return $config;
}

Expand Down Expand Up @@ -275,6 +281,48 @@ sub _compile_config_entry {
return $trigger->( $self, $value, $config );
}

# Attn. We are traversing along the original data structure all the time,
# using references, and changing values on the spot, not returning anything.
sub _replace_env_vars {
my ( $self, $entry ) = @_;
if( ref $entry ne 'HASH' && ref $entry ne 'ARRAY' ) {
croak 'Param entry is not HASH or ARRAY';
}
if( ref $entry eq 'HASH' ) {
foreach my $value (values %{ $entry }) {
if( (ref $value) =~ m/(HASH|ARRAY)/msx ) {
$self->_replace_env_vars( $value );
} elsif( (ref $value) =~ m/(CODE|REF|GLOB)/msx ) {
# Pretty much anything else except SCALAR. Do nothing
1;
} else {
if( $value ) {
while( my ($k, $v) = each %ENV) {
$value =~ s/ \$ [{] ENV:$k [}] /$v/gmsx;
}
}
}
}
} else {
# ref $entry is 'ARRAY'
foreach my $value (@{ $entry }) {
if( (ref $value) =~ m/(HASH|ARRAY)/msx ) {
$self->_replace_env_vars( $value );
} elsif( (ref $value) =~ m/(CODE|REF|GLOB)/msx ) {
# Pretty much anything else except SCALAR. Do nothing
1;
} else {
if( $value ) {
while( my ($k, $v) = each %ENV) {
$value =~ s/ \$ [{] ENV:$k [}] /$v/gmsx;
}
}
}
}
}
return;
}

1;

__END__
Expand Down
24 changes: 24 additions & 0 deletions t/config_reader.t
Original file line number Diff line number Diff line change
Expand Up @@ -186,4 +186,28 @@ like( exception { Foo->foo() }, qr{Foo::foo}, "traces are enabled", );
is $f->config_location, $tmpdir;
}

{
# Test _replace_env_vars
local $ENV{'SOME_ENV'} = 'Some Value';
my $cfg = { aa => 1,
coderef => sub { 1; },
name => 'Value "${ENV:SOME_ENV}" is.', ## no critic (ValuesAndExpressions::RequireInterpolationOfMetachars)
object => (
bless { name => 'Value "${ENV:SOME_ENV}" is.' }, ## no critic (ValuesAndExpressions::RequireInterpolationOfMetachars)
'An::Object'),
a => { bb => 2, b => { cc => 3, c => {
name => 'Value "${ENV:SOME_ENV}" is.', ## no critic (ValuesAndExpressions::RequireInterpolationOfMetachars)
dd => 4, d => [
'ee',
'Value "${ENV:SOME_ENV}" is.', ## no critic (ValuesAndExpressions::RequireInterpolationOfMetachars)
],
}, }, }, };
$f->_replace_env_vars( $cfg );
is( $cfg->{'name'}, 'Value "Some Value" is.', 'Replace with env var at root' );
is( $cfg->{'a'}->{'b'}->{'c'}->{'name'}, 'Value "Some Value" is.', 'Replace with env var at random' );
is( $cfg->{'a'}->{'b'}->{'c'}->{'d'}->[1], 'Value "Some Value" is.', 'Replace with env var in an array' );
is( $cfg->{'object'}->{'name'}, 'Value "${ENV:SOME_ENV}" is.', ## no critic (ValuesAndExpressions::RequireInterpolationOfMetachars)
'Not Replaced inside an object' );
}

done_testing;