From 4c7634855b6f702e4134a10966899455b59d600d Mon Sep 17 00:00:00 2001 From: Richard Hansen Date: Thu, 27 Jun 2024 00:41:45 -0400 Subject: [PATCH] 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. --- ddclient.in | 24 +++++++++--------------- t/parse_assignments.pl | 8 ++++++++ 2 files changed, 17 insertions(+), 15 deletions(-) diff --git a/ddclient.in b/ddclient.in index 2b18017..380a617 100755 --- a/ddclient.in +++ b/ddclient.in @@ -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); diff --git a/t/parse_assignments.pl b/t/parse_assignments.pl index d595459..d71f4a8 100644 --- a/t/parse_assignments.pl +++ b/t/parse_assignments.pl @@ -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 {