| 1 | package Data::NestedKey::CLI; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use Carp; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use CLI::Simple::Constants qw(:booleans :chars); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use CLI::Simple::Utils qw(slurp); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use Data::NestedKey; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | 2 | ▶ | use English qw(no_match_vars); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | 2 | ▶ | use File::Basename qw(fileparse dirname); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | 2 | ▶ | use File::Temp qw(tempfile); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | 2 | ▶ | use JSON; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | 2 | ▶ | use List::Util qw(none pairs); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 15 | 2 | ▶ | use YAML::XS; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||
| 17 | 2 | ▶ | use parent qw(CLI::Simple); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||
| 19 | 2 | ▶ | caller or exit __PACKAGE__->main(); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||
| 21 | ######################################################################## | ||||||||||||||||||||||||||||||||||||||||||
| 22 | 4 | ▶ | sub _write_file { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_write_file: called CC=19 SCAR=32.4
| |||||||||||||||||||||||||||||||||||||||||||
| 23 | ######################################################################## | ||||||||||||||||||||||||||||||||||||||||||
| 24 | 4 | my ( $self, $dnk ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||
| 26 | 4 | my $infile = $self->get_infile; | |||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||
| 28 | 4 | ▶ | die "ERROR: --replace and --backup require an input filename\n" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not defined $infile or $infile eq $Data::NestedKey::CLI::DASH
MC/DC50%1/2
not defined $infile or $infile eq $Data::NestedKey::CLI::DASH
not defined $infile
$infile eq $Data::NestedKey::CLI::DASH
Truth table66%2/3
not(A) or B
A= not defined $infileB= $infile eq $Data::NestedKey::CLI::DASH
| |||||||||||||||||||||||||||||||||||||||||||
| 29 | if !defined $infile || $infile eq $DASH; | ||||||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||||
| 31 | 3 | my $content = $self->_serialize($dnk); | |||||||||||||||||||||||||||||||||||||||||
| 32 | 3 | my $dir = dirname($infile); | |||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||
| 34 | # Create the replacement next to the original so rename stays on the | ||||||||||||||||||||||||||||||||||||||||||
| 35 | # same filesystem. Do not disturb the input if serialization fails. | ||||||||||||||||||||||||||||||||||||||||||
| 36 | 3 | my ( $fh, $temporary ) = tempfile( '.dnk-XXXXXX', DIR => $dir, UNLINK => 0 ); | |||||||||||||||||||||||||||||||||||||||||
| 37 | 3 | my $backup_file; | |||||||||||||||||||||||||||||||||||||||||
| 38 | 3 | my @original_stat = stat($infile); | |||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||
| 40 | 3 | ▶ | die sprintf "ERROR: could not stat %s: %s\n", $infile, $OS_ERROR | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 41 | if !@original_stat; | ||||||||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||||||
| 43 | 3 | my $ok = eval { | |||||||||||||||||||||||||||||||||||||||||
| 44 | 3 | ▶ | print {$fh} $content | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 45 | or die sprintf "ERROR: could not write %s: %s\n", $temporary, $OS_ERROR; | ||||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||
| 47 | 3 | ▶ | close $fh | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 48 | or die sprintf "ERROR: could not close %s: %s\n", $temporary, $OS_ERROR; | ||||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||
| 50 | 3 | ▶ | chmod( $original_stat[2] & oct('07777'), $temporary ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 51 | or die sprintf "ERROR: could not preserve permissions on %s: %s\n", $temporary, $OS_ERROR; | ||||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||
| 53 | 3 | ▶ | if ( my $backup = $self->get_backup ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 54 | 2 | my ( $name, $path ) = fileparse( $infile, qr/[.][^.]+$/xsm ); | |||||||||||||||||||||||||||||||||||||||||
| 55 | 2 | $backup_file = sprintf '%s%s.%s', $path, $name, $backup; | |||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||
| 57 | 2 | ▶ | die sprintf "ERROR: backup already exists: %s\n", $backup_file | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
-e $backup_file or -l $backup_file
MC/DC50%1/2
-e $backup_file or -l $backup_file
-e $backup_file
-l $backup_file
Truth table66%2/3
A or B
A= -e $backup_fileB= -l $backup_file
| |||||||||||||||||||||||||||||||||||||||||||
| 58 | if -e $backup_file || -l $backup_file; | ||||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||
| 60 | 1 | ▶ | rename $infile, $backup_file | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 61 | or die sprintf "ERROR: could not create backup %s: %s\n", $backup_file, $OS_ERROR; | ||||||||||||||||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||||
| 64 | 2 | ▶ | rename $temporary, $infile | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 65 | or die sprintf "ERROR: could not replace %s: %s\n", $infile, $OS_ERROR; | ||||||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||||||
| 67 | 2 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 68 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||||||
| 70 | 3 | ▶ | if ( !$ok ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 71 | 1 | my $error = $EVAL_ERROR; | |||||||||||||||||||||||||||||||||||||||||
| 72 | 1 | ▶ | close $fh if defined fileno($fh); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 73 | 1 | ▶ | unlink $temporary if -e $temporary; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||
| 75 | 1 | ▶ | if ( defined $backup_file && -e $backup_file && !-e $infile ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
defined $backup_file and -e $backup_file and not -e $infile
defined $backup_file and -e $backup_file
MC/DC0%0/3
defined $backup_file and -e $backup_file and not -e $infile
defined $backup_file
-e $backup_file
not -e $infile
Truth table25%1/4
A and B and not(C)
A= defined $backup_fileB= -e $backup_fileC= not -e $infile
| |||||||||||||||||||||||||||||||||||||||||||
| 76 | 0 | ▶ | rename $backup_file, $infile | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 77 | or die sprintf "ERROR: %s; could not restore backup %s: %s\n", $error, $backup_file, $OS_ERROR; | ||||||||||||||||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||||||
| 80 | 1 | die $error; | |||||||||||||||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||
| 83 | 2 | return; | |||||||||||||||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||
| 86 | ######################################################################## | ||||||||||||||||||||||||||||||||||||||||||
| 87 | 0 | ▶ | sub init { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
init: not called CC=12 SCAR=50.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 88 | ######################################################################## | ||||||||||||||||||||||||||||||||||||||||||
| 89 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||||
| 91 | 0 | my @args = $self->get_args; | |||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||
| 93 | 0 | my $command = $self->command; | |||||||||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||||||||
| 95 | 0 | ▶ | if ( !@args && !$self->commands->{$command} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
@args || $self->commands->{$command}
MC/DC0%0/2
@args || $self->commands->{$command}
@args
$self->commands->{$command}
Truth table0%0/3
A || B
A= @argsB= $self->commands->{$command}
| |||||||||||||||||||||||||||||||||||||||||||
| 96 | 0 | $self->command('get'); | |||||||||||||||||||||||||||||||||||||||||
| 97 | 0 | $self->command_args( [$command] ); | |||||||||||||||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||||||||||||||||
| 99 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 100 | die "ERROR: Unknown command ($command)\n" | ||||||||||||||||||||||||||||||||||||||||||
| 101 | 0 | ▶ | if !$self->commands->{$command}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||||||||||
| 104 | 0 | my $format = $self->get_format; | |||||||||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||||||||
| 106 | die sprintf "ERROR: not a valid input format - %s (must be json, yaml, yml, or xml)\n", $format | ||||||||||||||||||||||||||||||||||||||||||
| 107 | 0 | ▶ | if $format && none { $_ eq lc $format } qw(json yaml yml xml); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
Condition0%0/3
$format and &none(sub {
$_ eq lc $format;
}
, "json", "yaml", "yml", "xml")
MC/DC0%0/2
$format and &none(sub {
$_ eq lc $format;
}
, "json", "yaml", "yml", "xml")
$format
&none(sub {
$_ eq lc $format;
}
, "json", "yaml", "yml", "xml")
Truth table0%0/3
A and B
A= $formatB= &none(sub {
$_ eq lc $format;
}
, "json", "yaml", "yml", "xml")
| |||||||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||||||
| 109 | 0 | my $output_format = $self->get_output_format; | |||||||||||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||||||||||||
| 111 | die sprintf "ERROR: not a valid output format - %s (must be json, yaml, yml, or xml)\n", $output_format | ||||||||||||||||||||||||||||||||||||||||||
| 112 | 0 | ▶ | if $output_format && none { $_ eq lc $output_format } qw(json yaml yml xml); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
Condition0%0/3
$output_format and &none(sub {
$_ eq lc $output_format;
}
, "json", "yaml", "yml", "xml")
MC/DC0%0/2
$output_format and &none(sub {
$_ eq lc $output_format;
}
, "json", "yaml", "yml", "xml")
$output_format
&none(sub {
$_ eq lc $output_format;
}
, "json", "yaml", "yml", "xml")
Truth table0%0/3
A and B
A= $output_formatB= &none(sub {
$_ eq lc $output_format;
}
, "json", "yaml", "yml", "xml")
| |||||||||||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||||||||||
| 114 | 0 | my $infile = $self->get_infile; | |||||||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||||||||
| 116 | 0 | my $fh = eval { | |||||||||||||||||||||||||||||||||||||||||
| 117 | 0 | ▶ | return \*STDIN | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not $infile or $infile eq $Data::NestedKey::CLI::DASH
MC/DC0%0/2
not $infile or $infile eq $Data::NestedKey::CLI::DASH
not $infile
$infile eq $Data::NestedKey::CLI::DASH
Truth table0%0/3
not(A) or B
A= not $infileB= $infile eq $Data::NestedKey::CLI::DASH
| |||||||||||||||||||||||||||||||||||||||||||
| 118 | if !$infile || $infile eq $DASH; | ||||||||||||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||||||||||||
| 120 | 0 | ▶ | open my $handle, '<', $infile | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 121 | or die "ERROR: could not open $infile for reading\n$OS_ERROR"; | ||||||||||||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||||||||||||
| 123 | 0 | return $handle; | |||||||||||||||||||||||||||||||||||||||||
| 124 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||||||||||
| 126 | 0 | ▶ | die "ERROR: $EVAL_ERROR" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 127 | if !$fh; | ||||||||||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||||||||||
| 129 | 0 | $self->set_fh($fh); | |||||||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||||||||
| 131 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||||||||||||
| 134 | ######################################################################## | ||||||||||||||||||||||||||||||||||||||||||
| 135 | 0 | ▶ | sub _xml_data { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_xml_data: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||||||||||||
| 136 | ######################################################################## | ||||||||||||||||||||||||||||||||||||||||||
| 137 | 0 | my ( $self, $data ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||||||||||||
| 139 | 0 | ▶ | eval { require Bedrock::XML; 1 } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 140 | or die "ERROR: XML support requires the optional Bedrock::XML module: $EVAL_ERROR"; | ||||||||||||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||||||||||||
| 142 | 0 | my $xml = Bedrock::XML->newFromString($data); | |||||||||||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||||||||||||
| 144 | 0 | ▶ | die "ERROR: unable to parse Bedrock XML input\n" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 145 | if !$xml; | ||||||||||||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||||||||||||
| 147 | 0 | return $xml->devolve; | |||||||||||||||||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||||||||||||
| 150 | ######################################################################## | ||||||||||||||||||||||||||||||||||||||||||
| 151 | 7 | ▶ | sub _serialize { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_serialize: called CC=6 SCAR=22.9
| |||||||||||||||||||||||||||||||||||||||||||
| 152 | ######################################################################## | ||||||||||||||||||||||||||||||||||||||||||
| 153 | 7 | my ( $self, $dnk ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||||||||||||
| 155 | 7 | ▶ | my $format = lc( $self->get_output_format // $self->{input_format} // 'json' ); | ||||||||||||||||||||||||||||||||||||||||
|
Condition60%3/5
($self->get_output_format // $self->{"input_format"}) // "json"
$self->get_output_format // $self->{"input_format"}
MC/DC0%0/2
($self->get_output_format // $self->{"input_format"}) // "json"
$self->get_output_format
$self->{"input_format"}
Truth table66%2/3
A // B
A= $self->get_output_formatB= $self->{"input_format"}
| |||||||||||||||||||||||||||||||||||||||||||
| 156 | 7 | ▶ | $format = 'yaml' if $format eq 'yml'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||||||||||||||
| 158 | 7 | ▶ | if ( $format eq 'xml' ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 159 | 0 | ▶ | eval { require Bedrock::XML; 1 } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 160 | or die "ERROR: XML output requires the optional Bedrock::XML module: $EVAL_ERROR"; | ||||||||||||||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||||||||||||
| 162 | 0 | return Bedrock::XML::writeXML( $self->get_data ); | |||||||||||||||||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||||||||||||
| 165 | 7 | local $Data::NestedKey::FORMAT = uc $format; | |||||||||||||||||||||||||||||||||||||||||
| 166 | 7 | return $dnk->as_string; | |||||||||||||||||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||||||||||||||
| 169 | ######################################################################## | ||||||||||||||||||||||||||||||||||||||||||
| 170 | 12 | ▶ | sub _fetch_data { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_fetch_data: called CC=12 SCAR=27.5
| |||||||||||||||||||||||||||||||||||||||||||
| 171 | ######################################################################## | ||||||||||||||||||||||||||||||||||||||||||
| 172 | 12 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||||||||||||||||
| 174 | 12 | my $data = slurp( $self->get_fh ); | |||||||||||||||||||||||||||||||||||||||||
| 175 | 12 | my $format = $self->get_format; | |||||||||||||||||||||||||||||||||||||||||
| 176 | 12 | my $infile = $self->get_infile; | |||||||||||||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||||||||||||||
| 178 | # File extensions and recognizable Bedrock XML markup select XML first. | ||||||||||||||||||||||||||||||||||||||||||
| 179 | # Otherwise, prefer JSON to YAML (JSON is also valid YAML). | ||||||||||||||||||||||||||||||||||||||||||
| 180 | 12 | ▶ | my $looks_xml = ( defined $infile && $infile =~ /[.]xml\z/ixsm ) | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%3/6
defined $infile && $infile =~ /[.]xml\z/imsx || $data =~ /\A\s*(?:<\?xml[^>]*>\s*)?<(?:object|array)\b/msx
defined $infile and $infile =~ /[.]xml\z/imsx
MC/DC0%0/3
defined $infile && $infile =~ /[.]xml\z/imsx || $data =~ /\A\s*(?:<\?xml[^>]*>\s*)?<(?:object|array)\b/msx
defined $infile
$infile =~ /[.]xml\z/imsx
$data =~ /\A\s*(?:<\?xml[^>]*>\s*)?<(?:object|array)\b/msx
Truth table40%2/5
A and B || C
A= defined $infileB= $infile =~ /[.]xml\z/imsxC= $data =~ /\A\s*(?:<\?xml[^>]*>\s*)?<(?:object|array)\b/msx
| |||||||||||||||||||||||||||||||||||||||||||
| 181 | || $data =~ /\A\s*(?:<\?xml[^>]*>\s*)?<(?:object|array)\b/xsm; | ||||||||||||||||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||||||||||||||||
| 183 | 12 | ▶ | my @formats = $format ? ( lc $format ) : $looks_xml ? qw(xml) : qw(json yaml); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 184 | 12 | my @errors; | |||||||||||||||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||||||||||||||||
| 186 | 12 | foreach my $candidate (@formats) { | |||||||||||||||||||||||||||||||||||||||||
| 187 | 12 | my $obj = eval { | |||||||||||||||||||||||||||||||||||||||||
| 188 | 12 | ▶ | return decode_json($data) if $candidate eq 'json'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 189 | 0 | ▶ | return $self->_xml_data($data) if $candidate eq 'xml'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 190 | 0 | return YAML::XS::Load($data); | |||||||||||||||||||||||||||||||||||||||||
| 191 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||||||||||||||||
| 193 | 12 | ▶ | if ($EVAL_ERROR) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 194 | 1 | push @errors, sprintf '%s: %s', $candidate, $EVAL_ERROR; | |||||||||||||||||||||||||||||||||||||||||
| 195 | 1 | next; | |||||||||||||||||||||||||||||||||||||||||
| 196 | } | ||||||||||||||||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||||||||||||||||
| 198 | 11 | ▶ | die sprintf "ERROR: %s input must contain a hash or array\n", $candidate | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
ref $obj ne "HASH" and ref $obj ne "ARRAY"
MC/DC0%0/2
ref $obj ne "HASH" and ref $obj ne "ARRAY"
ref $obj ne "HASH"
ref $obj ne "ARRAY"
Truth table33%1/3
A and B
A= ref $obj ne "HASH"B= ref $obj ne "ARRAY"
| |||||||||||||||||||||||||||||||||||||||||||
| 199 | if ref($obj) ne 'HASH' && ref($obj) ne 'ARRAY'; | ||||||||||||||||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||||||||||||||||
| 201 | 11 | ▶ | $self->{input_format} = $candidate eq 'yml' ? 'yaml' : $candidate; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 202 | 11 | $Data::NestedKey::FORMAT = uc $self->{input_format}; | |||||||||||||||||||||||||||||||||||||||||
| 203 | 11 | $self->set_data($obj); | |||||||||||||||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||||||||||||||||
| 205 | 11 | return Data::NestedKey->new($obj); | |||||||||||||||||||||||||||||||||||||||||
| 206 | } | ||||||||||||||||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||||||||||||||||
| 208 | 1 | die sprintf "ERROR: unable to deserialize input\n%s", join( q{}, @errors ); | |||||||||||||||||||||||||||||||||||||||||
| 209 | } | ||||||||||||||||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||||||||||||||||
| 211 | ######################################################################## | ||||||||||||||||||||||||||||||||||||||||||
| 212 | 8 | ▶ | sub cmd_set { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
cmd_set: called CC=6 SCAR=18.1
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 213 | ######################################################################## | ||||||||||||||||||||||||||||||||||||||||||
| 214 | 8 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||||||||||||||||
| 216 | 8 | my (@key_values) = $self->get_args; | |||||||||||||||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||||||||||||||||
| 218 | 8 | ▶ | die "ERROR: usage: dnk set key value ...\n" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not @key_values or @key_values % 2
MC/DC0%0/2
not @key_values or @key_values % 2
not @key_values
@key_values % 2
Truth table33%1/3
not(A) or B
A= not @key_valuesB= @key_values % 2
| |||||||||||||||||||||||||||||||||||||||||||
| 219 | if !@key_values || @key_values % 2; | ||||||||||||||||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||||||||||||||||
| 221 | 8 | my $dnk = $self->_fetch_data; | |||||||||||||||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||||||||||||||||
| 223 | 8 | foreach my $p ( pairs @key_values ) { | |||||||||||||||||||||||||||||||||||||||||
| 224 | 8 | $dnk->set( @{$p} ); | |||||||||||||||||||||||||||||||||||||||||
| 225 | } | ||||||||||||||||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||||||||||||||||
| 227 | 8 | ▶ | if ( $self->get_replace || $self->get_backup ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$self->get_replace or $self->get_backup
MC/DC100%2/2
$self->get_replace or $self->get_backup
$self->get_replace
$self->get_backup
Truth table100%3/3
A or B
A= $self->get_replaceB= $self->get_backup
| |||||||||||||||||||||||||||||||||||||||||||
| 228 | 4 | $self->_write_file($dnk); | |||||||||||||||||||||||||||||||||||||||||
| 229 | } | ||||||||||||||||||||||||||||||||||||||||||
| 230 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 231 | 4 | print {*STDOUT} $self->_serialize($dnk); | |||||||||||||||||||||||||||||||||||||||||
| 232 | } | ||||||||||||||||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||||||||||||||||
| 234 | 6 | return $SUCCESS; | |||||||||||||||||||||||||||||||||||||||||
| 235 | } | ||||||||||||||||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||||||||||||||||
| 237 | ######################################################################## | ||||||||||||||||||||||||||||||||||||||||||
| 238 | 2 | ▶ | sub cmd_get { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
cmd_get: called CC=9 SCAR=24.4
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 239 | ######################################################################## | ||||||||||||||||||||||||||||||||||||||||||
| 240 | 2 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||||||||||||||||
| 242 | 2 | my ($filter) = $self->get_args; | |||||||||||||||||||||||||||||||||||||||||
| 243 | 2 | ▶ | $filter //= $PERIOD; | ||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$filter //= $Data::NestedKey::CLI::PERIOD
MC/DC0%0/2
$filter //= $Data::NestedKey::CLI::PERIOD
$filter
$Data::NestedKey::CLI::PERIOD
Truth table66%2/3
A //= B
A= $filterB= $Data::NestedKey::CLI::PERIOD
| |||||||||||||||||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||||||||||||||||
| 245 | 2 | my $dnk = $self->_fetch_data; | |||||||||||||||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||||||||||||||||
| 247 | 2 | ▶ | die "ERROR: unable to deserialize data\n" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 248 | if !$dnk; | ||||||||||||||||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||||||||||||||||
| 250 | 2 | ▶ | if ( $filter =~ /^[.].+/xsm ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 251 | 0 | $filter =~ s/^[.](.*)$/$1/xsm; | |||||||||||||||||||||||||||||||||||||||||
| 252 | } | ||||||||||||||||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||||||||||||||||
| 254 | 2 | ▶ | my $elem = $filter eq $PERIOD ? $self->get_data : $dnk->get($filter); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||||||||||||||||
| 256 | 2 | ▶ | if ( !defined $elem && !$self->get_error_on_undefined ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition33%1/3
defined $elem || $self->get_error_on_undefined
MC/DC0%0/2
defined $elem || $self->get_error_on_undefined
defined $elem
$self->get_error_on_undefined
Truth table33%1/3
A || B
A= defined $elemB= $self->get_error_on_undefined
| |||||||||||||||||||||||||||||||||||||||||||
| 257 | 0 | $elem = q{}; | |||||||||||||||||||||||||||||||||||||||||
| 258 | } | ||||||||||||||||||||||||||||||||||||||||||
| 259 | elsif ( !defined $elem ) { | ||||||||||||||||||||||||||||||||||||||||||
| 260 | 0 | die "ERROR: $filter is undefined\n"; | |||||||||||||||||||||||||||||||||||||||||
| 261 | } | ||||||||||||||||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||||||||||||||||
| 263 | 2 | ▶ | print {*STDOUT} ref $elem ? JSON->new->pretty->encode($elem) : $elem; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||||||||||||||||
| 265 | 2 | return $SUCCESS; | |||||||||||||||||||||||||||||||||||||||||
| 266 | } | ||||||||||||||||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||||||||||||||||
| 268 | ######################################################################## | ||||||||||||||||||||||||||||||||||||||||||
| 269 | 0 | ▶ | sub main { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
main: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 270 | ######################################################################## | ||||||||||||||||||||||||||||||||||||||||||
| 271 | 0 | my @option_specs = qw( | |||||||||||||||||||||||||||||||||||||||||
| 272 | help|h | ||||||||||||||||||||||||||||||||||||||||||
| 273 | backup|b=s | ||||||||||||||||||||||||||||||||||||||||||
| 274 | replace|r | ||||||||||||||||||||||||||||||||||||||||||
| 275 | infile|i=s | ||||||||||||||||||||||||||||||||||||||||||
| 276 | format|f=s | ||||||||||||||||||||||||||||||||||||||||||
| 277 | output-format|o=s | ||||||||||||||||||||||||||||||||||||||||||
| 278 | error_on_undefined|u | ||||||||||||||||||||||||||||||||||||||||||
| 279 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||||||||||||||||
| 281 | 0 | my %commands = ( | |||||||||||||||||||||||||||||||||||||||||
| 282 | get => \&cmd_get, | ||||||||||||||||||||||||||||||||||||||||||
| 283 | set => \&cmd_set, | ||||||||||||||||||||||||||||||||||||||||||
| 284 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||||||||||||||||
| 286 | 0 | my @extra_options = qw(fh data dnk); | |||||||||||||||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||||||||||||||||
| 288 | 0 | my $cli = __PACKAGE__->new( | |||||||||||||||||||||||||||||||||||||||||
| 289 | commands => \%commands, | ||||||||||||||||||||||||||||||||||||||||||
| 290 | option_specs => \@option_specs, | ||||||||||||||||||||||||||||||||||||||||||
| 291 | extra_options => \@extra_options, | ||||||||||||||||||||||||||||||||||||||||||
| 292 | validate_command => $FALSE, | ||||||||||||||||||||||||||||||||||||||||||
| 293 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||||||||||||||||
| 295 | 0 | return $cli->run; | |||||||||||||||||||||||||||||||||||||||||
| 296 | } | ||||||||||||||||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||||||||||||||||
| 298 | 2 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||||||||||||||||
| 300 | __END__ | ||||||||||||||||||||||||||||||||||||||||||