| 1 | package App::NDTools::NDTool; | ||||||||||
| 2 | |||||||||||
| 3 | 16 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 16 | ▶ | use warnings FATAL => 'all'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | 16 | ▶ | use App::NDTools::INC; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 16 | ▶ | use App::NDTools::Slurp qw(s_dump s_load); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 16 | ▶ | use Encode::Locale; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 16 | ▶ | use Encode qw(decode); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 16 | ▶ | use Getopt::Long qw(GetOptionsFromArray :config bundling noignore_case); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 16 | ▶ | use Log::Log4Cli; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 16 | ▶ | use Struct::Path 0.80 qw(path); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | |||||||||||
| 14 | 16 | our $VERSION = '0.33'; | |||||||||
| 15 | |||||||||||
| 16 | sub arg_opts { | ||||||||||
| 17 | 181 | ▶ | my $self = shift; | ||||||||
|
Subroutine
arg_opts: called CC=1 SCAR=0.4
Pod
undocumented
| |||||||||||
| 18 | |||||||||||
| 19 | return ( | ||||||||||
| 20 | 'dump-opts' => \$self->{OPTS}->{'dump-opts'}, | ||||||||||
| 21 | 'help|h' => sub { | ||||||||||
| 22 | 0 | ▶ | $self->{OPTS}->{help} = 1; | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 23 | 0 | die "!FINISH"; | |||||||||
| 24 | }, | ||||||||||
| 25 | 'ifmt=s' => \$self->{OPTS}->{ifmt}, | ||||||||||
| 26 | 'ofmt=s' => \$self->{OPTS}->{ofmt}, | ||||||||||
| 27 | 'pretty!' => \$self->{OPTS}->{pretty}, | ||||||||||
| 28 | 'verbose|v:+' => \$Log::Log4Cli::LEVEL, | ||||||||||
| 29 | 'version|V' => sub { | ||||||||||
| 30 | 3 | ▶ | $self->{OPTS}->{version} = 1; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 31 | 3 | die "!FINISH"; | |||||||||
| 32 | }, | ||||||||||
| 33 | 181 | ); | |||||||||
| 34 | } | ||||||||||
| 35 | |||||||||||
| 36 | sub check_args { | ||||||||||
| 37 | 67 | ▶ | my $self = shift; | ||||||||
|
Subroutine
check_args: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||
| 38 | |||||||||||
| 39 | 67 | ▶ | die_fatal 'At least one argument expected', 1 unless (@_); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 40 | |||||||||||
| 41 | 66 | return $self; | |||||||||
| 42 | } | ||||||||||
| 43 | |||||||||||
| 44 | sub configure { | ||||||||||
| 45 | 106 | ▶ | my $self = shift; | ||||||||
|
Subroutine
configure: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 46 | |||||||||||
| 47 | 106 | return $self->check_args(@{$self->{ARGV}}); | |||||||||
| 48 | } | ||||||||||
| 49 | |||||||||||
| 50 | sub defaults { | ||||||||||
| 51 | return { | ||||||||||
| 52 | 108 | ▶ | 'ofmt' => 'JSON', | ||||||||
|
Subroutine
defaults: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 53 | 'pretty' => 1, | ||||||||||
| 54 | 'verbose' => $Log::Log4Cli::LEVEL, | ||||||||||
| 55 | }; | ||||||||||
| 56 | } | ||||||||||
| 57 | |||||||||||
| 58 | sub dump_opts { | ||||||||||
| 59 | 1 | ▶ | my $self = shift; | ||||||||
|
Subroutine
dump_opts: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 60 | |||||||||||
| 61 | 1 | delete $self->{OPTS}->{'dump-opts'}; | |||||||||
| 62 | 1 | s_dump(\*STDOUT, undef, undef, $self->{OPTS}); | |||||||||
| 63 | } | ||||||||||
| 64 | |||||||||||
| 65 | sub grep { | ||||||||||
| 66 | 15 | ▶ | my ($self, $spaths, @structs) = @_; | ||||||||
|
Subroutine
grep: called CC=8 SCAR=20.8
Pod
undocumented
| |||||||||||
| 67 | 15 | my @out; | |||||||||
| 68 | |||||||||||
| 69 | 15 | for my $struct (@structs) { | |||||||||
| 70 | 15 | my (%map_idx, $path, $ref, $grepped); | |||||||||
| 71 | |||||||||||
| 72 | 15 | for (@{$spaths}) { | |||||||||
| 73 | 19 | my @found = eval { path($struct, $_, deref => 1, paths => 1) }; | |||||||||
| 74 | |||||||||||
| 75 | 19 | ▶ | while (($path, $ref) = splice @found, 0, 2) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 76 | # remap array's indexes | ||||||||||
| 77 | 29 | my $map_key = ""; | |||||||||
| 78 | 29 | my $map_path = []; | |||||||||
| 79 | |||||||||||
| 80 | 29 | for my $step (@{$path}) { | |||||||||
| 81 | 84 | ▶ | if (ref $step eq 'ARRAY') { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 82 | 42 | $map_key .= "[]"; | |||||||||
| 83 | 42 | ▶ | unless (exists $map_idx{$map_key}->{$step->[0]}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 84 | $map_idx{$map_key}->{$step->[0]} = | ||||||||||
| 85 | 30 | keys %{$map_idx{$map_key}}; | |||||||||
| 86 | } | ||||||||||
| 87 | |||||||||||
| 88 | 42 | push @{$map_path}, [$map_idx{$map_key}->{$step->[0]}]; | |||||||||
| 89 | } else { # HASH | ||||||||||
| 90 | 42 | $map_key .= "{$step->{K}->[0]}"; | |||||||||
| 91 | 42 | push @{$map_path}, $step; | |||||||||
| 92 | } | ||||||||||
| 93 | } | ||||||||||
| 94 | |||||||||||
| 95 | 29 | path($grepped, $map_path, assign => $ref, expand => 1); | |||||||||
| 96 | } | ||||||||||
| 97 | } | ||||||||||
| 98 | |||||||||||
| 99 | 15 | ▶ | push @out, $grepped if (defined $grepped); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 100 | } | ||||||||||
| 101 | |||||||||||
| 102 | 15 | return @out; | |||||||||
| 103 | } | ||||||||||
| 104 | |||||||||||
| 105 | sub load_struct { | ||||||||||
| 106 | 269 | ▶ | my ($self, $uri, $fmt) = @_; | ||||||||
|
Subroutine
load_struct: called CC=1 SCAR=1.2
Pod
undocumented
| |||||||||||
| 107 | |||||||||||
| 108 | 269 | ▶ | log_trace { ref $uri ? "Reading from STDIN" : "Loading '$uri'" }; | ||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=13.0
Branch: 1 branch
| |||||||||||
| 109 | 269 | s_load($uri, $fmt); | |||||||||
| 110 | } | ||||||||||
| 111 | |||||||||||
| 112 | sub new { | ||||||||||
| 113 | 181 | ▶ | my $self = bless {}, shift; | ||||||||
|
Subroutine
new: called CC=6 SCAR=19.6
Pod
undocumented
| |||||||||||
| 114 | |||||||||||
| 115 | 181 | $self->{OPTS} = $self->defaults(); | |||||||||
| 116 | $self->{ARGV} = | ||||||||||
| 117 | 181 | ▶ | [ map { decode(locale => "$_", Encode::FB_CROAK) } @_ ? @_ : @ARGV ]; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 118 | |||||||||||
| 119 | 181 | $self->{TTY} = -t STDOUT; | |||||||||
| 120 | |||||||||||
| 121 | 181 | ▶ | unless (GetOptionsFromArray ($self->{ARGV}, $self->arg_opts)) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 122 | 0 | $self->usage; | |||||||||
| 123 | 0 | die_fatal "Unsupported opts used", 1; | |||||||||
| 124 | } | ||||||||||
| 125 | |||||||||||
| 126 | 181 | ▶ | if ($self->{OPTS}->{help}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 127 | 0 | $self->usage; | |||||||||
| 128 | 0 | die_info, 0; | |||||||||
| 129 | } | ||||||||||
| 130 | |||||||||||
| 131 | 181 | ▶ | if ($self->{OPTS}->{version}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 132 | 3 | print $self->VERSION . "\n"; | |||||||||
| 133 | 3 | die_info, 0; | |||||||||
| 134 | } | ||||||||||
| 135 | |||||||||||
| 136 | 178 | $self->configure(); | |||||||||
| 137 | |||||||||||
| 138 | 175 | ▶ | if ($self->{OPTS}->{'dump-opts'}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 139 | 0 | $self->dump_opts(); | |||||||||
| 140 | 0 | die_info, 0; | |||||||||
| 141 | } | ||||||||||
| 142 | |||||||||||
| 143 | 175 | return $self; | |||||||||
| 144 | } | ||||||||||
| 145 | |||||||||||
| 146 | sub usage { | ||||||||||
| 147 | 0 | ▶ | require Pod::Usage; | ||||||||
|
Subroutine
usage: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 148 | 0 | Pod::Usage::pod2usage( | |||||||||
| 149 | -exitval => 'NOEXIT', | ||||||||||
| 150 | -output => \*STDERR, | ||||||||||
| 151 | -sections => 'SYNOPSIS|OPTIONS|EXAMPLES', | ||||||||||
| 152 | -verbose => 99 | ||||||||||
| 153 | ); | ||||||||||
| 154 | } | ||||||||||
| 155 | |||||||||||
| 156 | 16 | 1; # End of App::NDTools::NDTool | |||||||||