Move *_env processing to parse_assignment

This makes the behavior transparent to the rest of ddclient, which
will make it possible for a future commit to simplify option
processing.
This commit is contained in:
Richard Hansen 2024-06-27 00:41:45 -04:00
parent 19848852a4
commit 4c7634855b
2 changed files with 17 additions and 15 deletions

View file

@ -1652,6 +1652,15 @@ sub parse_assignment {
}
$rest = substr($rest,1);
}
if ($name =~ qr/^(.*)_env$/) {
$name = $1;
debug("Loading value for $name from environment variable $value");
if (!exists($ENV{$value})) {
warning("Environment variable '$value' not set for keyword '$name' (ignored)");
return parse_assignment($rest);
}
$value = $ENV{$value};
}
}
warning("assignment to '%s' ended with the escape character (\\)", $name) if $escape;
warning("assignment to '%s' ended with an unterminated quote (%s)", $name, $quote) if $quote;
@ -1786,21 +1795,6 @@ sub _read_config {
## verify that keywords are valid...and check the value
for my $k (keys %locals) {
# Handle '_env' keyword suffix
if ($k =~ /(.*)_env$/) {
debug("Loading value for $1 from environment variable $locals{$k}.");
if (!exists($ENV{$locals{$k}})) {
warning("Environment variable '$locals{$k}' not set for keyword '$k' (ignored)");
delete $locals{$k};
next;
}
# Set the value to the value of the environment variable
$locals{$1} = $ENV{$locals{$k}};
# Remove the '_env' suffix from the key
# TODO: Shouldn't the *_env entry be deleted from %locals?
$k = $1;
}
$locals{$k} = $passwords{$k} if defined $passwords{$k};
if (!exists $variables{'merged'}{$k}) {
warning("unrecognized keyword '%s' (ignored)", $k);

View file

@ -44,8 +44,16 @@ my @test_cases = (
tc('unquoted escaped backslash', "a=\\\\", { a => "\\" }, ""),
tc('squoted escaped squote', "a='\\''", { a => "'" }, ""),
tc('dquoted escaped dquote', "a=\"\\\"\"", { a => '"' }, ""),
tc('env: empty', "a_env=", {}, ""),
tc('env: unset', "a_env=UNSET", {}, ""),
tc('env: set', "a_env=TEST", { a => 'val' }, ""),
tc('env: single quoted', "a_env='TEST'", { a => 'val' }, ""),
);
delete($ENV{''});
delete($ENV{UNSET});
$ENV{TEST} = 'val';
for my $tc (@test_cases) {
my ($got_rest, %got_vars) = ddclient::parse_assignments($tc->{input});
subtest $tc->{name} => sub {