Merge pull request #187 from rhansen/defined-or-op

Use the `//` operator to simplify code
This commit is contained in:
Sandro 2020-06-24 20:01:19 +02:00 committed by GitHub
commit 08c7e71352
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -816,7 +816,7 @@ if (opt('help')) {
} }
## read config file because 'daemon' mode may be defined there. ## read config file because 'daemon' mode may be defined there.
read_config(define($opt{'file'}, default('file')), \%config, \%globals); read_config($opt{'file'} // default('file'), \%config, \%globals);
init_config(); init_config();
test_possible_ip() if opt('query'); test_possible_ip() if opt('query');
@ -856,7 +856,7 @@ do {
printf("Help found"); printf("Help found");
} }
read_config(define($opt{'file'}, default('file')), \%config, \%globals); read_config($opt{'file'} // default('file'), \%config, \%globals);
init_config(); init_config();
read_cache(opt('cache'), \%cache); read_cache(opt('cache'), \%cache);
print_info() if opt('debug') && opt('verbose'); print_info() if opt('debug') && opt('verbose');
@ -943,8 +943,8 @@ sub update_nics {
if (exists $iplist{$use}{$arg_ip}{$arg_fw}{$arg_if}{$arg_web}{$arg_cmd}) { if (exists $iplist{$use}{$arg_ip}{$arg_fw}{$arg_if}{$arg_web}{$arg_cmd}) {
$ip = $iplist{$use}{$arg_ip}{$arg_fw}{$arg_if}{$arg_web}{$arg_cmd}; $ip = $iplist{$use}{$arg_ip}{$arg_fw}{$arg_if}{$arg_web}{$arg_cmd};
} else { } else {
$ip = get_ip($use, $h); $ip = get_ip($use, $h) // '';
if (!defined $ip || !$ip) { if (!$ip) {
warning("unable to determine IP address") warning("unable to determine IP address")
if !$daemon || opt('verbose'); if !$daemon || opt('verbose');
next; next;
@ -971,7 +971,7 @@ sub update_nics {
foreach my $h (sort keys %config) { foreach my $h (sort keys %config) {
if (!exists $examined{$h}) { if (!exists $examined{$h}) {
failed("%s was not updated because protocol %s is not supported.", failed("%s was not updated because protocol %s is not supported.",
$h, define($config{$h}{'protocol'}, '<undefined>')); $h, $config{$h}{'protocol'} // '<undefined>');
} }
} }
write_cache(opt('cache')); write_cache(opt('cache'));
@ -1022,7 +1022,7 @@ sub write_cache {
## construct the cache file. ## construct the cache file.
my $cache = ""; my $cache = "";
foreach my $h (sort keys %cache) { foreach my $h (sort keys %cache) {
my $opt = join(',', map { "$_=" . define($cache{$h}{$_},'') } sort keys %{$cache{$h}}); my $opt = join(',', map { "$_=" . ($cache{$h}{$_} // '') } sort keys %{$cache{$h}});
$cache .= sprintf "%s%s%s\n", $opt, ($opt ? ' ' : ''), $h; $cache .= sprintf "%s%s%s\n", $opt, ($opt ? ' ' : ''), $h;
} }
@ -1375,7 +1375,7 @@ sub init_config {
## first the globals... ## first the globals...
foreach my $k (keys %globals) { foreach my $k (keys %globals) {
my $def = $variables{'merged'}{$k}; my $def = $variables{'merged'}{$k};
my $ovalue = define($globals{$k}, $def->{'default'}); my $ovalue = $globals{$k} // $def->{'default'};
my $value = check_value($ovalue, $def); my $value = check_value($ovalue, $def);
if ($def->{'required'} && !defined $value) { if ($def->{'required'} && !defined $value) {
$value = default($k); $value = default($k);
@ -1404,7 +1404,7 @@ sub init_config {
foreach my $k (keys %$svars) { foreach my $k (keys %$svars) {
my $def = $svars->{$k}; my $def = $svars->{$k};
my $ovalue = define($config{$h}{$k}, $def->{'default'}); my $ovalue = $config{$h}{$k} // $def->{'default'};
my $value = check_value($ovalue, $def); my $value = check_value($ovalue, $def);
if ($def->{'required'} && !defined $value) { if ($def->{'required'} && !defined $value) {
warning("skipping host: %s: '%s=%s' is an invalid %s.", $h, $k, $ovalue, $def->{'type'}); warning("skipping host: %s: '%s=%s' is an invalid %s.", $h, $k, $ovalue, $def->{'type'});
@ -1470,7 +1470,7 @@ sub process_args {
sub test_possible_ip { sub test_possible_ip {
local $opt{'debug'} = 0; local $opt{'debug'} = 0;
printf "use=ip, ip=%s address is %s\n", opt('ip'), define(get_ip('ip'), 'NOT FOUND') printf "use=ip, ip=%s address is %s\n", opt('ip'), get_ip('ip') // 'NOT FOUND'
if defined opt('ip'); if defined opt('ip');
{ {
@ -1485,18 +1485,18 @@ sub test_possible_ip {
warning("failed to get list of interfaces") if !@ifs; warning("failed to get list of interfaces") if !@ifs;
foreach my $if (@ifs) { foreach my $if (@ifs) {
local $opt{'if'} = $if; local $opt{'if'} = $if;
printf "use=if, if=%s address is %s\n", opt('if'), define(get_ip('if'), 'NOT FOUND'); printf "use=if, if=%s address is %s\n", opt('if'), get_ip('if') // 'NOT FOUND';
} }
} }
if (opt('fw')) { if (opt('fw')) {
if (opt('fw') !~ m%/%) { if (opt('fw') !~ m%/%) {
foreach my $fw (sort keys %builtinfw) { foreach my $fw (sort keys %builtinfw) {
local $opt{'use'} = $fw; local $opt{'use'} = $fw;
printf "use=%s address is %s\n", $fw, define(get_ip($fw), 'NOT FOUND'); printf "use=%s address is %s\n", $fw, get_ip($fw) // 'NOT FOUND';
} }
} }
local $opt{'use'} = 'fw'; local $opt{'use'} = 'fw';
printf "use=fw, fw=%s address is %s\n", opt('fw'), define(get_ip(opt('fw')), 'NOT FOUND') printf "use=fw, fw=%s address is %s\n", opt('fw'), get_ip(opt('fw')) // 'NOT FOUND'
if !exists $builtinfw{opt('fw')}; if !exists $builtinfw{opt('fw')};
} }
@ -1504,14 +1504,14 @@ sub test_possible_ip {
local $opt{'use'} = 'web'; local $opt{'use'} = 'web';
foreach my $web (sort keys %builtinweb) { foreach my $web (sort keys %builtinweb) {
local $opt{'web'} = $web; local $opt{'web'} = $web;
printf "use=web, web=%s address is %s\n", $web, define(get_ip('web'), 'NOT FOUND'); printf "use=web, web=%s address is %s\n", $web, get_ip('web') // 'NOT FOUND';
} }
printf "use=web, web=%s address is %s\n", opt('web'), define(get_ip('web'), 'NOT FOUND') printf "use=web, web=%s address is %s\n", opt('web'), get_ip('web') // 'NOT FOUND'
if !exists $builtinweb{opt('web')}; if !exists $builtinweb{opt('web')};
} }
if (opt('cmd')) { if (opt('cmd')) {
local $opt{'use'} = 'cmd'; local $opt{'use'} = 'cmd';
printf "use=cmd, cmd=%s address is %s\n", opt('cmd'), define(get_ip('cmd'), 'NOT FOUND'); printf "use=cmd, cmd=%s address is %s\n", opt('cmd'), get_ip('cmd') // 'NOT FOUND';
} }
exit 0 unless opt('debug'); exit 0 unless opt('debug');
} }
@ -1528,7 +1528,7 @@ sub test_geturl {
password => opt('password'), password => opt('password'),
}); });
print "URL $url\n"; print "URL $url\n";
print defined($reply) ? $reply : "<undefined>\n"; print $reply // "<undefined>\n";
exit; exit;
} }
###################################################################### ######################################################################
@ -1713,10 +1713,7 @@ sub opt {
my $v = shift; my $v = shift;
my $h = shift; my $h = shift;
return $config{$h}{$v} if defined($h) && defined($config{$h}{$v}); return $config{$h}{$v} if defined($h) && defined($config{$h}{$v});
return $opt{$v} if defined $opt{$v}; return $opt{$v} // $globals{$v} // default($v);
return $globals{$v} if defined $globals{$v};
return default($v) if defined default($v);
return undef;
} }
sub min { sub min {
my $min = shift; my $min = shift;
@ -1733,21 +1730,12 @@ sub max {
return $max; return $max;
} }
###################################################################### ######################################################################
## define
######################################################################
sub define {
foreach (@_) {
return $_ if defined $_;
}
return undef;
}
######################################################################
## ynu ## ynu
###################################################################### ######################################################################
sub ynu { sub ynu {
my ($value, $yes, $no, $undef) = @_; my ($value, $yes, $no, $undef) = @_;
return $no if !defined($value) || !$value; return $no if !($value // '');
return $yes if $value eq '1'; return $yes if $value eq '1';
foreach (qw(yes true)) { foreach (qw(yes true)) {
return $yes if $_ =~ /^$value/i; return $yes if $_ =~ /^$value/i;
@ -2126,8 +2114,8 @@ sub geturl {
alarm(opt('timeout')) if opt('timeout') > 0; alarm(opt('timeout')) if opt('timeout') > 0;
while ($_ = <$sd>) { while ($_ = <$sd>) {
$0 = sprintf("%s - read from %s", $program, $peer_port_ipv); $0 = sprintf("%s - read from %s", $program, $peer_port_ipv);
verbose("RECEIVE:", "%s", define($_, "<undefined>")); verbose("RECEIVE:", "%s", $_ // "<undefined>");
$reply .= $_ if defined $_; $reply .= $_ // '';
} }
if (opt('timeout') > 0) { if (opt('timeout') > 0) {
alarm(0); alarm(0);
@ -2139,7 +2127,7 @@ sub geturl {
warning("TIMEOUT: %s after %s seconds", $to, opt('timeout')); warning("TIMEOUT: %s after %s seconds", $to, opt('timeout'));
$reply = ''; $reply = '';
} }
$reply = '' if !defined $reply; $reply //= '';
} }
} }
$0 = sprintf("%s - closed %s", $program, $peer_port_ipv); $0 = sprintf("%s - closed %s", $program, $peer_port_ipv);
@ -2318,11 +2306,11 @@ sub get_ip {
} else { } else {
warning("found neither ipv4 nor ipv6 address"); warning("found neither ipv4 nor ipv6 address");
} }
if (($use ne 'ip') && (define($ip, '') eq '0.0.0.0')) { if ($use ne 'ip' && ($ip // '') eq '0.0.0.0') {
$ip = undef; $ip = undef;
} }
debug("get_ip: using %s, %s reports %s", $use, $arg, define($ip, "<undefined>")); debug("get_ip: using %s, %s reports %s", $use, $arg, $ip // "<undefined>");
return $ip; return $ip;
} }
@ -2540,7 +2528,7 @@ sub nic_updateable {
($cache{$host}{'mtime'} ? prettytime($cache{$host}{'mtime'}) : '<never>'), ($cache{$host}{'mtime'} ? prettytime($cache{$host}{'mtime'}) : '<never>'),
prettyinterval($config{$host}{'min-interval'}) prettyinterval($config{$host}{'min-interval'})
) )
if opt('verbose') || !define($cache{$host}{'warned-min-interval'}, 0); if opt('verbose') || !($cache{$host}{'warned-min-interval'} // 0);
$cache{$host}{'warned-min-interval'} = $now; $cache{$host}{'warned-min-interval'} = $now;
@ -2554,7 +2542,7 @@ sub nic_updateable {
($cache{$host}{'atime'} ? prettytime($cache{$host}{'atime'}) : '<never>'), ($cache{$host}{'atime'} ? prettytime($cache{$host}{'atime'}) : '<never>'),
prettyinterval($config{$host}{'min-error-interval'}) prettyinterval($config{$host}{'min-error-interval'})
) )
if opt('verbose') || !define($cache{$host}{'warned-min-error-interval'}, 0); if opt('verbose') || !($cache{$host}{'warned-min-error-interval'} // 0);
$cache{$host}{'warned-min-error-interval'} = $now; $cache{$host}{'warned-min-error-interval'} = $now;
@ -2579,7 +2567,7 @@ sub nic_updateable {
success("%s: skipped: IP address was already set to %s.", $host, $ip) success("%s: skipped: IP address was already set to %s.", $host, $ip)
if opt('verbose'); if opt('verbose');
} }
$config{$host}{'status'} = define($cache{$host}{'status'}, ''); $config{$host}{'status'} = $cache{$host}{'status'} // '';
$config{$host}{'update'} = $update; $config{$host}{'update'} = $update;
if ($update) { if ($update) {
$config{$host}{'status'} = 'noconnect'; $config{$host}{'status'} = 'noconnect';
@ -2681,8 +2669,8 @@ sub nic_dyndns1_update {
url => $url, url => $url,
login => $config{$h}{'login'}, login => $config{$h}{'login'},
password => $config{$h}{'password'}, password => $config{$h}{'password'},
}); }) // '';
if (!defined($reply) || !$reply) { if (!$reply) {
failed("updating %s: Could not connect to %s.", $h, $config{$h}{'server'}); failed("updating %s: Could not connect to %s.", $h, $config{$h}{'server'});
next; next;
} }
@ -2849,8 +2837,8 @@ sub nic_dyndns2_update {
url => $url, url => $url,
login => $config{$h}{'login'}, login => $config{$h}{'login'},
password => $config{$h}{'password'}, password => $config{$h}{'password'},
}); }) // '';
if (!defined($reply) || !$reply) { if (!$reply) {
failed("updating %s: Could not connect to %s.", $hosts, $config{$h}{'server'}); failed("updating %s: Could not connect to %s.", $hosts, $config{$h}{'server'});
last; last;
} }
@ -2959,8 +2947,8 @@ sub nic_noip_update {
url => $url, url => $url,
login => $config{$h}{'login'}, login => $config{$h}{'login'},
password => $config{$h}{'password'}, password => $config{$h}{'password'},
}); }) // '';
if (!defined($reply) || !$reply) { if (!$reply) {
failed("updating %s: Could not connect to %s.", $hosts, $config{$h}{'server'}); failed("updating %s: Could not connect to %s.", $hosts, $config{$h}{'server'});
last; last;
} }
@ -3099,8 +3087,8 @@ sub nic_dslreports1_update {
url => $url, url => $url,
login => $config{$h}{'login'}, login => $config{$h}{'login'},
password => $config{$h}{'password'}, password => $config{$h}{'password'},
}); }) // '';
if (!defined($reply) || !$reply) { if (!$reply) {
failed("updating %s: Could not connect to %s.", $h, $config{$h}{'server'}); failed("updating %s: Could not connect to %s.", $h, $config{$h}{'server'});
next; next;
} }
@ -3179,8 +3167,8 @@ sub nic_hammernode1_update {
url => $url, url => $url,
login => $config{$h}{'login'}, login => $config{$h}{'login'},
password => $config{$h}{'password'}, password => $config{$h}{'password'},
}); }) // '';
if (!defined($reply) || !$reply) { if (!$reply) {
failed("updating %s: Could not connect to %s.", $h, $config{$h}{'server'}); failed("updating %s: Could not connect to %s.", $h, $config{$h}{'server'});
last; last;
} }
@ -3274,8 +3262,8 @@ sub nic_zoneedit1_update {
url => $url, url => $url,
login => $config{$h}{'login'}, login => $config{$h}{'login'},
password => $config{$h}{'password'}, password => $config{$h}{'password'},
}); }) // '';
if (!defined($reply) || !$reply) { if (!$reply) {
failed("updating %s: Could not connect to %s.", $hosts, $config{$h}{'server'}); failed("updating %s: Could not connect to %s.", $hosts, $config{$h}{'server'});
last; last;
} }
@ -3429,8 +3417,8 @@ sub nic_easydns_update {
url => $url, url => $url,
login => $config{$h}{'login'}, login => $config{$h}{'login'},
password => $config{$h}{'password'}, password => $config{$h}{'password'},
}); }) // '';
if (!defined($reply) || !$reply) { if (!$reply) {
failed("updating %s: Could not connect to %s.", $hosts, $config{$h}{'server'}); failed("updating %s: Could not connect to %s.", $hosts, $config{$h}{'server'});
last; last;
} }
@ -3543,8 +3531,8 @@ sub nic_namecheap_update {
$url .= "&ip="; $url .= "&ip=";
$url .= $ip if $ip; $url .= $ip if $ip;
my $reply = geturl({ proxy => opt('proxy'), url => $url }); my $reply = geturl({ proxy => opt('proxy'), url => $url }) // '';
if (!defined($reply) || !$reply) { if (!$reply) {
failed("updating %s: Could not connect to %s.", $h, $config{$h}{'server'}); failed("updating %s: Could not connect to %s.", $h, $config{$h}{'server'});
last; last;
} }