Improve readability by moving code out of unnecessary blocks
Invert conditions and move out unnecessary blocks to reduce indentation and make it easier to see control flow.
This commit is contained in:
parent
160344514f
commit
5c38af2ed5
1 changed files with 55 additions and 59 deletions
114
ddclient.in
114
ddclient.in
|
@ -1577,10 +1577,9 @@ sub read_cache {
|
||||||
%opt = %saved;
|
%opt = %saved;
|
||||||
|
|
||||||
for my $h (keys %cache) {
|
for my $h (keys %cache) {
|
||||||
if (exists $config->{$h}) {
|
next if !exists($config->{$h});
|
||||||
for (qw(atime mtime wtime ip status)) {
|
for (qw(atime mtime wtime ip status)) {
|
||||||
$config->{$h}{$_} = $cache{$h}{$_} if exists $cache{$h}{$_};
|
$config->{$h}{$_} = $cache{$h}{$_} if exists $cache{$h}{$_};
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1764,65 +1763,63 @@ sub _read_config {
|
||||||
# Handle '_env' keyword suffix
|
# Handle '_env' keyword suffix
|
||||||
if ($k =~ /(.*)_env$/) {
|
if ($k =~ /(.*)_env$/) {
|
||||||
debug("Loading value for $1 from environment variable $locals{$k}.");
|
debug("Loading value for $1 from environment variable $locals{$k}.");
|
||||||
if (exists($ENV{$locals{$k}})) {
|
if (!exists($ENV{$locals{$k}})) {
|
||||||
# Set the value to the value of the environment variable
|
|
||||||
$locals{$1} = $ENV{$locals{$k}};
|
|
||||||
# Remove the '_env' suffix from the key
|
|
||||||
$k = $1;
|
|
||||||
} else {
|
|
||||||
warning("Environment variable '$locals{$k}' not set for keyword '$k' (ignored)");
|
warning("Environment variable '$locals{$k}' not set for keyword '$k' (ignored)");
|
||||||
delete $locals{$k};
|
delete $locals{$k};
|
||||||
next;
|
next;
|
||||||
}
|
}
|
||||||
|
# Set the value to the value of the environment variable
|
||||||
|
$locals{$1} = $ENV{$locals{$k}};
|
||||||
|
# Remove the '_env' suffix from the key
|
||||||
|
$k = $1;
|
||||||
}
|
}
|
||||||
|
|
||||||
$locals{$k} = $passwords{$k} if defined $passwords{$k};
|
$locals{$k} = $passwords{$k} if defined $passwords{$k};
|
||||||
if (!exists $variables{'merged'}{$k}) {
|
if (!exists $variables{'merged'}{$k}) {
|
||||||
warning("unrecognized keyword '%s' (ignored)", $k);
|
warning("unrecognized keyword '%s' (ignored)", $k);
|
||||||
delete $locals{$k};
|
delete $locals{$k};
|
||||||
} else {
|
next;
|
||||||
my $def = $variables{'merged'}{$k};
|
|
||||||
my $value = check_value($locals{$k}, $def);
|
|
||||||
if (!defined($value)) {
|
|
||||||
warning("Invalid Value for keyword '%s' = '%s'", $k, $locals{$k});
|
|
||||||
delete $locals{$k};
|
|
||||||
} else {
|
|
||||||
$locals{$k} = $value;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
my $def = $variables{'merged'}{$k};
|
||||||
|
my $value = check_value($locals{$k}, $def);
|
||||||
|
if (!defined($value)) {
|
||||||
|
warning("Invalid Value for keyword '%s' = '%s'", $k, $locals{$k});
|
||||||
|
delete $locals{$k};
|
||||||
|
next;
|
||||||
|
}
|
||||||
|
$locals{$k} = $value;
|
||||||
}
|
}
|
||||||
|
%passwords = ();
|
||||||
if (exists($locals{'host'})) {
|
if (exists($locals{'host'})) {
|
||||||
$args[0] = @args ? "$args[0],$locals{host}" : "$locals{host}";
|
$args[0] = @args ? "$args[0],$locals{host}" : "$locals{host}";
|
||||||
}
|
}
|
||||||
## accumulate globals
|
## accumulate globals
|
||||||
if ($#args < 0) {
|
if ($#args < 0) {
|
||||||
map { $globals{$_} = $locals{$_} } keys %locals;
|
map { $globals{$_} = $locals{$_} } keys %locals;
|
||||||
|
next;
|
||||||
}
|
}
|
||||||
|
|
||||||
## process this host definition
|
## process this host definition
|
||||||
if (@args) {
|
my ($host, $login, $password) = @args;
|
||||||
my ($host, $login, $password) = @args;
|
|
||||||
|
|
||||||
## add in any globals..
|
## add in any globals..
|
||||||
%locals = (%globals, %locals);
|
%locals = (%globals, %locals);
|
||||||
|
|
||||||
## override login and password if specified the old way.
|
## override login and password if specified the old way.
|
||||||
$locals{'login'} = $login if defined $login;
|
$locals{'login'} = $login if defined $login;
|
||||||
$locals{'password'} = $password if defined $password;
|
$locals{'password'} = $password if defined $password;
|
||||||
|
|
||||||
## allow {host} to be a comma separated list of hosts
|
## allow {host} to be a comma separated list of hosts
|
||||||
for my $h (split_by_comma($host)) {
|
for my $h (split_by_comma($host)) {
|
||||||
if ($config{$h}) {
|
if ($config{$h}) {
|
||||||
## host already defined, merging configs
|
## host already defined, merging configs
|
||||||
$config{$h} = {%locals, %{$config{$h}}};
|
$config{$h} = {%locals, %{$config{$h}}};
|
||||||
} else {
|
} else {
|
||||||
## save a copy of the current globals
|
## save a copy of the current globals
|
||||||
$config{$h} = {%locals};
|
$config{$h} = {%locals};
|
||||||
$config{$h}{'host'} = $h;
|
$config{$h}{'host'} = $h;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
%passwords = ();
|
|
||||||
}
|
}
|
||||||
close(FD);
|
close(FD);
|
||||||
|
|
||||||
|
@ -1978,29 +1975,28 @@ sub init_config {
|
||||||
if (!exists($services{$proto})) {
|
if (!exists($services{$proto})) {
|
||||||
warning("skipping host: %s: unrecognized protocol '%s'", $h, $proto);
|
warning("skipping host: %s: unrecognized protocol '%s'", $h, $proto);
|
||||||
delete $config{$h};
|
delete $config{$h};
|
||||||
|
next;
|
||||||
} else {
|
|
||||||
my $svars = $services{$proto}{'variables'};
|
|
||||||
my $conf = {'protocol' => $proto};
|
|
||||||
|
|
||||||
for my $k (keys %$svars) {
|
|
||||||
# Make sure any _env suffixed variables look at their original entry
|
|
||||||
$k = $1 if $k =~ /^(.*)_env$/;
|
|
||||||
|
|
||||||
my $def = $svars->{$k};
|
|
||||||
my $ovalue = $config{$h}{$k} // $def->{'default'};
|
|
||||||
my $value = check_value($ovalue, $def);
|
|
||||||
if ($def->{'required'} && !defined $value) {
|
|
||||||
warning("skipping host: %s: '%s=%s' is an invalid %s.", $h, $k, $ovalue, $def->{'type'});
|
|
||||||
delete $config{$h};
|
|
||||||
next HOST;
|
|
||||||
}
|
|
||||||
$conf->{$k} = $value;
|
|
||||||
|
|
||||||
}
|
|
||||||
$config{$h} = $conf;
|
|
||||||
$config{$h}{'cacheable'} = [ @{$services{$proto}{'cacheable'}} ];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
my $svars = $services{$proto}{'variables'};
|
||||||
|
my $conf = {'protocol' => $proto};
|
||||||
|
|
||||||
|
for my $k (keys %$svars) {
|
||||||
|
# Make sure any _env suffixed variables look at their original entry
|
||||||
|
$k = $1 if $k =~ /^(.*)_env$/;
|
||||||
|
|
||||||
|
my $def = $svars->{$k};
|
||||||
|
my $ovalue = $config{$h}{$k} // $def->{'default'};
|
||||||
|
my $value = check_value($ovalue, $def);
|
||||||
|
if ($def->{'required'} && !defined $value) {
|
||||||
|
warning("skipping host: %s: '%s=%s' is an invalid %s.", $h, $k, $ovalue, $def->{'type'});
|
||||||
|
delete $config{$h};
|
||||||
|
next HOST;
|
||||||
|
}
|
||||||
|
$conf->{$k} = $value;
|
||||||
|
}
|
||||||
|
$config{$h} = $conf;
|
||||||
|
$config{$h}{'cacheable'} = [ @{$services{$proto}{'cacheable'}} ];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue