blib/lib/App/Netdisco/Worker/Plugin/DumpConfig.pm untested

filter: Statementstmt 0.0% 0 / 41 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 16 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 0.0% 0 / 6 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 89 CC 5 SCAR 27.7
CC
5
Cov
0%
_json_safe
30.0
BEGIN
2.0
BEGIN
2.0
CRAP 16.0
1 package App::Netdisco::Worker::Plugin::DumpConfig;
2  
3 use Dancer ':syntax';
4 use App::Netdisco::Worker::Plugin;
5 use aliased 'App::Netdisco::Worker::Status';
6  
7 use Data::Printer;
8 use Scalar::Util 'blessed';
9  
10 # domain_suffix is a compiled Regexp and url_base a URI::Based, and JSON
11 # encodes neither. Data::Printer renders them, so only the quiet path needs it.
12 sub _json_safe {
13 my $node = shift;
14 return $node if !ref $node or ref $node eq 'SCALAR';
15 return "$node" if blessed $node;
16 return [ map { _json_safe($_) } @$node ] if ref $node eq 'ARRAY';
17 return { map {($_ => _json_safe($node->{$_}))} keys %$node }
18 if ref $node eq 'HASH';
19 return undef;
20 }
21  
22 register_worker({ phase => 'main' }, sub {
23 my ($job, $workerconf) = @_;
24 my $extra = $job->extra;
25 my $print_this_instead = $job->port;
26  
27 if (! $ENV{ND2_DO_QUIET}) {
28 debug sprintf 'port: "%s"', ($print_this_instead || '');
29 debug sprintf 'subaction: "%s"', ($extra || '');
30 }
31  
32 my $key = ($print_this_instead || $extra);
33 my $CONFIG = config();
34  
35 # Job promotes an undefined subaction to empty string, so an unkeyed
36 # dumpconfig arrives here as q{} and must still dump everything.
37 my $dump = (((defined $key) and (ref $key eq q{}) and (length $key))
38 ? $CONFIG->{$key} : $CONFIG);
39 p $dump unless $ENV{ND2_DO_QUIET};
40  
41 return Status->done($ENV{ND2_DO_QUIET}
42 ? to_json(_json_safe($dump)) : 'Dumped config');
43 });
44  
45 true;