blib/lib/MOP4Import/Base/CLI_JSON.pm untested

filter: Statementstmt 0.0% 0 / 397 Branchbran 0.0% 0 / 134 Conditioncond 0.0% 0 / 84 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 62 Podpod 0.0% 0 / 46 totaltotal 0.0% 0 / 723 CC 67 SCAR 29.9
CC
67
Cov
0%
_cli_xargs
72.0
cli_write_fh
56.0
declare_output_format
42.0
CRAP 19.9
1 #!/usr/bin/env perl
2 package MOP4Import::Base::CLI_JSON;
3 use strict;
4 use warnings;
5 use Carp ();
6  
7 use constant DEBUG => $ENV{DEBUG_MOP4IMPORT};
8 BEGIN {
9 print STDERR "Using (file '" . __FILE__ . "')\n"
10 , (DEBUG >= 3 ? Carp::longmess("callstack:") : ())
11 if DEBUG and DEBUG >= 2
12 }
13  
14 use MOP4Import::Base::CLI -as_base
15 , [constant => parse_opts__preserve_hyphen => 1]
16 , [fields =>
17 , ['help' => doc => "show this help message", json_type => 'string']
18 , ['quiet' => doc => 'to be (somewhat) quiet', json_type => 'int']
19 , ['scalar' => doc => "evaluate methods in scalar context", json_type => 'bool']
20 , ['output' => default => 'ndjson'
21 , doc => "choose output serializer (ndjson/json/tsv/dump)"
22 , json_type => 'string'
23 ]
24 , ['flatten'
25 , json_type => 'bool'
26 ]
27 , ['undef-as' => default => 'null'
28 , doc => "serialize undef as this value. used in tsv output"
29 , json_type => 'string'
30 ]
31 , ['no-exit-code'
32 , doc => "exit with 0(EXIT_SUCCESS) even when result was falsy/empty"
33 , json_type => 'bool'
34 ]
35 , ['binary' => default => 0, doc => "keep STDIN/OUT/ERR binary friendly"
36 , json_type => 'bool'
37 ]
38 ];
39  
40 use JSON::MaybeXS;
41 use MOP4Import::Base::JSON -as_base;
42  
43 use MOP4Import::Opts;
44 use MOP4Import::Util qw/lexpand globref take_locked_opts_of lock_keys_as/;
45  
46 use File::Spec ();
47  
48 use open ();
49  
50 sub cli_precmd {
51 (my MY $self) = @_;
52 #
53 # cli_precmd() may be called from $class->cmd_help.
54 #
55 unless (ref $self and $self->{binary}) {
56 'open'->import(qw/:locale :std/);
57 }
58 }
59  
60 #
61 # Replace parse_opts to use parse_json_opts
62 #
63 sub cli_parse_opts {
64 my ($pack, $list, $result, $opt_alias, $converter, %opts) = @_;
65  
66 MOP4Import::Util::parse_json_opts($pack, $list, $result, $opt_alias);
67 }
68  
69 sub cli_eval {
70 (my MY $self, my ($script, @args)) = @_;
71 my $pack = ref $self;
72 my $sub = do {
73 local $@;
74 my $code = eval qq{package $pack; use strict; sub {my \$self = shift; $script\n}};
75 die $@ if $@;
76 $code;
77 };
78 $sub->($self, @args);
79 }
80  
81 sub cli_invoke {
82 (my MY $self, my ($method, @args)) = @_;
83  
84 $self->cli_precmd($method);
85  
86 my $sub = $self->can($method)
87 or Carp::croak "No such method: $method";
88  
89 my $list = $self->cli_invoke_sub($sub, $self, @args);
90  
91 $self->cli_exit_for_result($list) unless $self->{'no-exit-code'};
92 }
93  
94 sub cli_invoke_sub {
95 (my MY $self, my ($sub, $receiver, @args)) = @_;
96  
97 my @res;
98 if ($self->{scalar}) {
99 $res[0] = $sub->($receiver, @args);
100 } else {
101 @res = $sub->($receiver, @args);
102 }
103  
104 $self->cli_output(\@res) unless $self->{quiet};
105  
106 \@res;
107 }
108  
109 #
110 # cli_output($list) -- Output abstraction (yield).
111 #
112 sub cli_output :method {
113 (my MY $self, my ($list)) = @_;
114  
115 unless ($self->{scalar} ? $list->[0] : @$list) {
116 return;
117 }
118  
119 my $emitter = ref $self->{output} eq 'CODE' ? $self->{output} : sub {
120 $self->cli_write_fh(\*STDOUT, @_);
121 };
122  
123 if ($self->{scalar}) {
124 $emitter->($_) for @$list;
125 } else {
126 $emitter->($list);
127 }
128 }
129  
130 #
131 # Gather output from cli_output
132 #
133 sub cli_capture_output {
134 (my MY $self, my ($subOrArrayOrString, @args)) = @_;
135 my @result;
136 local $self->{output} = sub {
137 push @result, \@_;
138 };
139 $self->cli_apply($subOrArrayOrString, @args);
140 @result;
141 }
142  
143 sub cli_examine_result {
144 (my MY $self, my $list) = @_;
145 if ($self->{scalar}) {
146 $list->[0];
147 } else {
148 @$list;
149 }
150 }
151  
152 #
153 # exit code handling
154 #
155 sub cli_exit_for_result {
156 (my MY $self, my $list) = @_;
157  
158 exit($self->cli_examine_result($list) ? 0 : 1);
159 }
160  
161 #========================================
162  
163 sub cli_array :method {
164 (my MY $self, my @args) = @_;
165 \@args;
166 }
167  
168 sub cli_list :method {
169 (my MY $self, my @args) = @_;
170 @args;
171 }
172  
173 sub cli_object :method {
174 (my MY $self, my %args) = @_;
175 \%args;
176 }
177  
178 sub cli_identity :method {
179 (my MY $self, my ($thing)) = @_;
180 $thing;
181 }
182  
183 sub cli_map_apply :method {
184 (my MY $self, my ($subOrArray, @args)) = @_;
185 map {
186 $self->cli_apply($subOrArray, $_);
187 } @args;
188 }
189  
190 sub cli_grep_apply :method {
191 (my MY $self, my ($subOrArray, @args)) = @_;
192 grep {
193 $self->cli_apply($subOrArray, $_);
194 } @args;
195 }
196  
197 # XXX: How about cli_reduce_apply?
198  
199 sub cli_apply :method {
200 (my MY $self, my ($subOrArrayOrString, @args)) = @_;
201 if (not defined $subOrArrayOrString) {
202 Carp::croak "undefined sub for cli_apply";
203 } elsif (ref $subOrArrayOrString eq 'CODE') {
204 $subOrArrayOrString->(@args);
205 } elsif (not ref $subOrArrayOrString or ref $subOrArrayOrString eq 'ARRAY') {
206 my ($meth, @opts) = lexpand($subOrArrayOrString);
207 if (my $sub = $self->can("cmd_$meth")) {
208 $sub->($self, @opts, @args);
209 } else {
210 $self->$meth(@opts, @args);
211 }
212 } else {
213 Carp::croak "Invalid argument for cli_apply: "
214 . MOP4Import::Util::terse_dump($subOrArrayOrString);
215 }
216 }
217  
218 sub cli_precheck_apply {
219 (my MY $self, my ($subOrArrayOrString)) = @_;
220 if (not defined $subOrArrayOrString) {
221 Carp::croak "undefined sub for cli_apply";
222 } elsif (ref $subOrArrayOrString eq 'CODE') {
223 1;
224 } else {
225 if (not ref $subOrArrayOrString or ref $subOrArrayOrString eq 'ARRAY') {
226 my ($meth, @opts) = lexpand($subOrArrayOrString);
227 return if $self->can("cmd_$meth") || $self->can($meth);
228 }
229 Carp::croak "Invalid argument for cli_apply: "
230 . MOP4Import::Util::terse_dump($subOrArrayOrString);
231 }
232 }
233  
234 use MOP4Import::Types
235 cliopts__xargs => [[fields => qw/null slurp single json decode/]];
236  
237 sub cli_xargs_json :method {
238 (my MY $self, my (@args)) = @_;
239 my cliopts__xargs $opts = $self->take_locked_opts_of(
240 cliopts__xargs, \@args, {0 => 'null', input => 'decode'},
241 );
242 $opts->{decode} //= (($opts->{json} //=1) ? 'json' : '');
243 $self->_cli_xargs($opts, @args);
244 }
245  
246 BEGIN {
247 my ($packSuffix) = do {
248 if ($] >= 5.022) {
249 'compat_double_diamond';
250 } else {
251 'compat_double_diamond_5_20';
252 }
253 };
254 (my $dir = __FILE__) =~ s,/?[^/]+\z,,;
255  
256 my $fn = File::Spec->rel2abs($dir) . "/../Util/$packSuffix.pm";
257 if (-r __FILE__ and not -r $fn) {
258 die "Can't load $fn";
259 }
260 do $fn;
261 my $pack = 'MOP4Import::Util::'.$packSuffix;
262 $pack->import;
263 print STDERR "compat_diamond is loaded from $fn\n" if DEBUG and DEBUG >= 2;
264 }
265  
266 sub _cli_xargs {
267 (my MY $self, my (@args)) = @_;
268 my cliopts__xargs $opts = $self->take_locked_opts_of(
269 cliopts__xargs, \@args, {0 => 'null'},
270 );
271 my ($subOrArray, @restPrefix) = @args;
272 $self->cli_precheck_apply($subOrArray);
273  
274 local $/ = $opts->{null} ? "\0" : "\n";
275 local *ARGV;
276 if ($opts->{slurp} || $opts->{single}) {
277 my @all = $self->cli_slurp_xargs($opts);
278 $self->cli_apply(
279 $subOrArray, @restPrefix,
280 ($opts->{single} ? \@all : @all)
281 );
282 } else {
283 my $decoder = defined $opts->{decode}
284 ? $self->cli_decoder_from($opts->{decode}) : undef;
285 local $_;
286 if (not ref $subOrArray and $self->can("cmd_$subOrArray")) {
287 while (defined($_ = $self->cli_compat_diamond)) {
288 chomp;
289 $self->cli_apply(
290 $subOrArray, @restPrefix,
291 ($decoder ? $decoder->($_) : $_)
292 )
293 }
294 $self->{'no-exit-code'} = 1;
295 ();
296 } else {
297 my @result;
298 while (defined($_ = $self->cli_compat_diamond)) {
299 chomp;
300 # XXX: yield...
301 push @result, $self->cli_apply(
302 $subOrArray, @restPrefix,
303 ($decoder ? $decoder->($_) : $_)
304 )
305 }
306 @result;
307 }
308 }
309 }
310  
311 sub cli_slurp_xargs_json {
312 (my MY $self, my (@args)) = @_;
313 my cliopts__xargs $opts = $self->take_locked_opts_of(
314 cliopts__xargs, \@args, {0 => 'null'},
315 );
316 $opts->{decode} //= (($opts->{json} //=1) ? 'json' : '');
317 $self->cli_slurp_xargs($opts, @args);
318 }
319  
320 sub cli_slurp_xargs {
321 (my MY $self, my (@args)) = @_;
322 my cliopts__xargs $opts = $self->take_locked_opts_of(
323 cliopts__xargs, \@args, {0 => 'null'},
324 );
325  
326 local @ARGV = @args;
327 my $decoder = defined $opts->{decode}
328 ? $self->cli_decoder_from($opts->{decode}) : undef;
329  
330 map {
331 $decoder ? $decoder->($_) : $_
332 } $self->cli_compat_diamond
333 }
334  
335 sub cli_decoder_from {
336 (my MY $self, my ($formatSpec, @rest)) = @_;
337 my ($format, @opts) = lexpand($formatSpec);
338 my $sub = $self->can("cli_decoder_from__$format")
339 or Carp::croak "Unknown decorder is requested: $format";
340 $sub->($self, @opts, @rest);
341 }
342  
343 #
344 # pass-through decoder.
345 #
346 sub cli_decoder_from__ {
347 sub {$_[0]}
348 }
349  
350 #
351 # json decoder
352 #
353 sub cli_decoder_from__json {
354 (my MY $self, my @opts) = @_;
355 my $decoder = $self->cli_json_decoder(qw/allow_nonref/, @opts);
356 sub {
357 my ($str) = @_;
358 Encode::_utf8_off($str);
359 $decoder->decode($str);
360 }
361 }
362  
363 #========================================
364  
365 sub declare_output_format {
366 (my $myPack, my Opts $opts, my ($formatName, $sub)) = m4i_args(@_);
367 my $encoderFuncName = "cli_encoder_to__$formatName";
368 my $writeFuncName = "cli_write_fh_as_$formatName";
369 my $outputFuncName = "cli_output_as_$formatName";
370 if (ref $sub eq 'CODE') {
371 *{globref($opts->{destpkg}, $writeFuncName)} = $sub;
372 *{globref($opts->{destpkg}, $outputFuncName)} = sub {
373 shift->$writeFuncName(\*STDOUT, $_[0]);
374 };
375 } elsif (not defined $sub) {
376 if ($opts->{destpkg}->can($writeFuncName)) {
377 *{globref($opts->{destpkg}, $outputFuncName)} = sub {
378 shift->$writeFuncName(\*STDOUT, $_[0]);
379 };
380 }
381 elsif ($opts->{destpkg}->can($encoderFuncName)) {
382 *{globref($opts->{destpkg}, $outputFuncName)} = sub {
383 shift->$encoderFuncName(\*STDOUT)->($_[0]);
384 };
385  
386 unless ($opts->{destpkg}->can($writeFuncName)) {
387 *{globref($opts->{destpkg}, $writeFuncName)} = sub {
388 shift->$encoderFuncName(shift)->(\@_);
389 };
390 }
391 }
392 else {
393 Carp::croak "output_format $formatName doesn't have method '$writeFuncName'";
394 }
395 } else {
396 Carp::croak "Invalid argument for output_format: "
397 . MOP4Import::Util::terse_dump($sub);
398 }
399 }
400  
401 # cli_write_fh($outFH, @output) -- Output format abstraction
402 # In cli_run context, cli_write_fh is called from cli_output($list) and
403 # used as cli_write_fh(\*STDOUT, $list).
404 # But in general, cli_write_fh can process multiple arguments at once.
405 #
406  
407 sub cli_write_fh {
408 (my MY $self, my ($outFH, @args)) = @_;
409  
410 my ($outputFmt, @opts) = lexpand($self->{'output'});
411  
412 if (my $sub = $self->can("cli_encoder_to__$outputFmt")) {
413 my $encoder = $sub->($self, $outFH, @opts);
414 $encoder->($_) for ($self->{flatten} ? (map {
415 (defined $_ && ref $_ eq 'ARRAY') ? @$_ : $_
416 } @args) : @args);
417 }
418 elsif ($sub = $self->can("cli_write_fh_as_".$outputFmt)) {
419 $sub->($self, $outFH, $self->{flatten} ? (map {
420 (defined $_ && ref $_ eq 'ARRAY') ? @$_ : $_
421 } @args) : @args);
422 }
423 else {
424 Carp::croak("Unknown output format: $self->{'output'}");
425 }
426 }
427  
428 sub cli_json { JSON() }
429  
430 sub cli_decode_json {
431 (my MY $self, my $string) = @_;
432 $self->cli_decoder_from__json->($string);
433 }
434  
435 sub cli_encode_json {
436 (my MY $self, my ($obj, $json_type)) = @_;
437 my $json = $self->SUPER::cli_encode_json($obj, $json_type);
438 Encode::_utf8_on($json) unless $self->{binary};
439 $json;
440 }
441  
442 sub cli_json_encoder {
443 (my MY $self) = @_;
444 my $js = $self->SUPER::cli_json_encoder;
445 $js->utf8 unless $self->{binary};
446 $js;
447 }
448  
449 sub cli_json_decoder {
450 (my MY $self, my @opts) = @_;
451 my $js = JSON()->new->relaxed;
452 $js->utf8 unless $self->{binary};
453 foreach my $opt (@opts) {
454 my ($method, @args) = lexpand($opt);
455 $js->$method(@args);
456 }
457 $js;
458 }
459  
460 #----------------------------------------
461  
462 sub cli_encode_as {
463 (my MY $self, my ($outputSpec, @items)) = @_;
464 my ($outputFmt, $layer, @opts) = lexpand($outputSpec);
465 $outputFmt //= '';
466 # Allow $layer to be a HASH
467 if (defined $layer and ref $layer eq 'HASH') {
468 my %opts = %$layer;
469 $layer = delete $opts{layer};
470 unshift @opts, %opts;
471 }
472 $layer //= '';
473 my $buffer = "";
474 {
475 open my $outFH, ">$layer", \$buffer;
476 if (my $sub = $self->can("cli_encoder_to__$outputFmt")) {
477 my $encoder = $sub->($self, $outFH, @opts);
478 foreach my $item (@items) {
479 $encoder->($item);
480 }
481 }
482 elsif ($sub = $self->can("cli_write_fh_as_$outputFmt")) {
483 $sub->($self, $outFH, \@items);
484 }
485 else {
486 Carp::croak "Unknown output format: '$outputFmt'";
487 }
488 }
489 $buffer;
490 }
491  
492 MY->declare_output_format(MY, 'ndjson');
493 sub cli_write_fh_as_ndjson {
494 (my MY $self, my ($outFH, @tables)) = @_;
495 foreach my $table (@tables) {
496 foreach my $item (ref $table eq 'ARRAY' ? @$table : $table) {
497 print $outFH ((ref $item ? $self->cli_encode_json($item) : $item // $self->{'undef-as'} // ''), "\n");
498 }
499 }
500 }
501  
502 MY->declare_output_format(MY, 'json');
503 sub cli_write_fh_as_json {
504 (my MY $self, my ($outFH, @args)) = @_;
505 foreach my $item (@args) {
506 print $outFH $self->cli_encode_json($item), "\n";
507 }
508 }
509  
510 MY->declare_output_format(MY, 'yaml');
511 sub cli_write_fh_as_yaml {
512 (my MY $self, my ($outFH, @args)) = @_;
513 require YAML::Syck;
514 print $outFH YAML::Syck::Dump(@args);
515 }
516  
517 MY->declare_output_format(MY, 'dump');
518 sub cli_write_fh_as_dump {
519 (my MY $self, my ($outFH, @args)) = @_;
520 foreach my $item (@args) {
521 my $dumper = Data::Dumper->new($item)
522 ->Terse(1)
523 ->Sortkeys(1)
524 ->Indent(1)
525 ->Deparse(1);
526 if (my $sub = $dumper->can("Trailingcomma")) {
527 $sub->($dumper, 1);
528 }
529  
530 print $outFH $dumper->Dump;
531 }
532 }
533  
534 MY->declare_output_format(MY, 'raw');
535 sub cli_write_fh_as_raw {
536 (my MY $self, my ($outFH, @tables)) = @_;
537 foreach my $table (@tables) {
538 foreach my $item (ref $table eq 'ARRAY' ? @$table : $table) {
539 print $outFH $item;
540 }
541 }
542 }
543  
544 MY->declare_output_format(MY, 'tsv');
545 sub cli_write_fh_as_tsv {
546 (my MY $self, my ($outFH, @tables)) = @_;
547 foreach my $table (@tables) {
548 foreach my $rec (@$table) {
549 print $outFH join("\t", map {
550 if (not defined $_) {
551 $self->{'undef-as'};
552 } elsif (ref $_) {
553 $self->cli_encode_json($_);
554 } else {
555 my $cp = $_;
556 $cp =~ s/[\t\n]+/ /g;
557 $cp
558 }
559 } ref $rec eq 'ARRAY' ? @$rec : $rec), "\n";
560 }
561 }
562 }
563  
564 #========================================
565  
566 sub cli_create_from_file :method {
567 my ($class, $configFn, @moreOpts) = @_;
568 my $realConfigFn = File::Spec->rel2abs($configFn);
569 my $oldcwd = $ENV{PWD} || do {require Cwd; Cwd::getcwd()};
570 my $realDir = File::Basename::dirname($realConfigFn);
571 chdir($realDir)
572 or Carp::croak "Can't chdir to $realDir: $!";
573  
574 # Read $configFn with scalar context.
575 my $opts = $class->cli_read_file($realConfigFn);
576  
577 my $object = (ref $class || $class)->new(
578 ref $opts eq 'HASH' ? %$opts : @$opts,
579 @moreOpts
580 );
581 chdir($oldcwd)
582 or Carp::croak "Can't chdir back to $oldcwd: $!";
583 $object;
584 }
585  
586 sub cli_read_file :method {
587 my ($classOrObj, $fileNameSpec, %moreOpts) = @_;
588 my ($fileName, %opts) = lexpand($fileNameSpec);
589 my ($ftype) = $fileName =~ m{\.(\w+)$};
590 $ftype //= "";
591  
592 my $sub = $classOrObj->can("cli_read_file__$ftype")
593 or Carp::croak "Unsupported file type '$ftype': $fileName";
594  
595 $sub->($classOrObj, $fileName, %opts, %moreOpts);
596 }
597  
598 # No filename extension => read entire content except last \n.
599 sub cli_read_file__ {
600 my ($classOrObj, $fileName) = @_;
601 open my $fh, '<:utf8', $fileName
602 or Carp::croak "Can't open $fileName: $!";
603 my $all = do {local $/; <$fh>};
604 chomp($all);
605 $all;
606 }
607  
608 # .txt => array of lines
609 sub cli_read_file__txt {
610 my ($classOrObj, $fileName) = @_;
611 my $all = $classOrObj->cli_read_file__($fileName);
612 my @list = split "\n", $all;
613 wantarray ? @list : \@list;
614 }
615  
616 # .yml
617 *cli_read_file__yaml = *cli_read_file__yml;*cli_read_file__yaml = *cli_read_file__yml;
618 sub cli_read_file__yml {
619 my ($classOrObj, $fileName) = @_;
620 require YAML::Syck;
621 YAML::Syck::LoadFile($fileName);
622 }
623  
624 # .json
625 sub cli_read_file__json {
626 my ($classOrObj, $fileName, %opts) = @_;
627 my $allow_comments = delete $opts{allow_comments};
628 open my $fh, '<', $fileName
629 or Carp::croak "Can't open $fileName: $!";
630 my $all = do {local $/; <$fh>};
631 unless (defined $all) {
632 Carp::croak "Can't read $fileName: $!";
633 }
634 if ($allow_comments) {
635 require MOP4Import::Util::CommentedJson;
636 local $@;
637 eval {
638 $all = MOP4Import::Util::CommentedJson->strip_json_comments($all);
639 };
640 if ($@) {
641 Carp::carp "Can't strip comment in $fileName: $@";
642 }
643 }
644 local $@;
645 my @result;
646 eval {
647 @result = $classOrObj->cli_json_decoder->incr_parse($all);
648 };
649 if ($@) {
650 Carp::croak "decode_json failed in $fileName: $@";
651 }
652 @result >= 2 ? \@result : $result[0];
653 }
654  
655 MY->cli_run(\@ARGV) unless caller;
656  
657 1;