| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package ETL::Yertl::Command::yq; | 
| 2 |  |  |  |  |  |  | our $VERSION = '0.035'; | 
| 3 |  |  |  |  |  |  | # ABSTRACT: Filter and construct documents using a mini-language | 
| 4 |  |  |  |  |  |  |  | 
| 5 | 7 |  |  | 7 |  | 569910 | use ETL::Yertl; | 
|  | 7 |  |  |  |  | 18 |  | 
|  | 7 |  |  |  |  | 35 |  | 
| 6 | 7 |  |  | 7 |  | 1885 | use ETL::Yertl::Util qw( load_module ); | 
|  | 7 |  |  |  |  | 17 |  | 
|  | 7 |  |  |  |  | 336 |  | 
| 7 | 7 |  |  | 7 |  | 38 | use boolean qw( :all ); | 
|  | 7 |  |  |  |  | 21 |  | 
|  | 7 |  |  |  |  | 31 |  | 
| 8 | 7 |  |  | 7 |  | 685 | use Module::Runtime qw( use_module ); | 
|  | 7 |  |  |  |  | 23 |  | 
|  | 7 |  |  |  |  | 60 |  | 
| 9 |  |  |  |  |  |  | our $VERBOSE = $ENV{YERTL_VERBOSE} // 0; | 
| 10 |  |  |  |  |  |  |  | 
| 11 |  |  |  |  |  |  | sub is_empty { | 
| 12 | 0 |  | 0 | 0 | 0 | 0 | return !$_[0] || ref $_[0] eq 'empty'; | 
| 13 |  |  |  |  |  |  | } | 
| 14 |  |  |  |  |  |  |  | 
| 15 |  |  |  |  |  |  | sub main { | 
| 16 | 0 |  |  | 0 | 0 | 0 | my $class = shift; | 
| 17 |  |  |  |  |  |  |  | 
| 18 | 0 |  |  |  |  | 0 | my %opt; | 
| 19 | 0 | 0 |  |  |  | 0 | if ( ref $_[-1] eq 'HASH' ) { | 
| 20 | 0 |  |  |  |  | 0 | %opt = %{ pop @_ }; | 
|  | 0 |  |  |  |  | 0 |  | 
| 21 |  |  |  |  |  |  | } | 
| 22 |  |  |  |  |  |  |  | 
| 23 | 0 |  |  |  |  | 0 | my ( $filter, @files ) = @_; | 
| 24 |  |  |  |  |  |  |  | 
| 25 | 0 | 0 |  |  |  | 0 | die "Must give a filter\n" unless $filter; | 
| 26 |  |  |  |  |  |  |  | 
| 27 | 0 | 0 |  |  |  | 0 | push @files, "-" unless @files; | 
| 28 | 0 |  |  |  |  | 0 | for my $file ( @files ) { | 
| 29 |  |  |  |  |  |  | # We're doing a similar behavior to <>, but manually for easier testing. | 
| 30 | 0 |  |  |  |  | 0 | my $fh; | 
| 31 | 0 | 0 |  |  |  | 0 | if ( $file eq '-' ) { | 
| 32 |  |  |  |  |  |  | # Use the existing STDIN so tests can fake it | 
| 33 | 0 |  |  |  |  | 0 | $fh = \*STDIN; | 
| 34 |  |  |  |  |  |  | } | 
| 35 |  |  |  |  |  |  | else { | 
| 36 | 0 | 0 |  |  |  | 0 | unless ( open $fh, '<', $file ) { | 
| 37 | 0 |  |  |  |  | 0 | warn "Could not open file '$file' for reading: $!\n"; | 
| 38 | 0 |  |  |  |  | 0 | next; | 
| 39 |  |  |  |  |  |  | } | 
| 40 |  |  |  |  |  |  | } | 
| 41 |  |  |  |  |  |  |  | 
| 42 | 0 |  |  |  |  | 0 | my $in_fmt = load_module( format => 'default' )->new( input => $fh ); | 
| 43 | 0 |  |  |  |  | 0 | my $scope = {}; | 
| 44 | 0 |  |  |  |  | 0 | for my $doc ( $in_fmt->read ) { | 
| 45 | 0 |  |  |  |  | 0 | my @output = $class->filter( $filter, $doc, $scope ); | 
| 46 | 0 |  |  |  |  | 0 | $class->write( \@output, \%opt ); | 
| 47 |  |  |  |  |  |  | } | 
| 48 |  |  |  |  |  |  |  | 
| 49 |  |  |  |  |  |  | # Finish the scope, cleaning up any collections | 
| 50 | 0 |  |  |  |  | 0 | $class->write( [ $class->finish( $scope ) ], \%opt ); | 
| 51 |  |  |  |  |  |  | } | 
| 52 |  |  |  |  |  |  | } | 
| 53 |  |  |  |  |  |  |  | 
| 54 |  |  |  |  |  |  | sub write { | 
| 55 | 0 |  |  | 0 | 0 | 0 | my ( $class, $docs, $opt ) = @_; | 
| 56 |  |  |  |  |  |  |  | 
| 57 | 0 | 0 |  |  |  | 0 | if ( $opt->{xargs} ) { | 
| 58 | 0 |  |  |  |  | 0 | print "$_\n" for grep { defined } @$docs; | 
|  | 0 |  |  |  |  | 0 |  | 
| 59 | 0 |  |  |  |  | 0 | return; | 
| 60 |  |  |  |  |  |  | } | 
| 61 |  |  |  |  |  |  |  | 
| 62 | 0 |  |  |  |  | 0 | my $out_fmt = load_module( format => 'default' )->new; | 
| 63 | 0 |  |  |  |  | 0 | for my $doc ( @$docs ) { | 
| 64 | 0 | 0 |  |  |  | 0 | next if is_empty( $doc ); | 
| 65 | 0 | 0 |  |  |  | 0 | if ( isTrue( $doc ) ) { | 
|  |  | 0 |  |  |  |  |  | 
| 66 | 0 |  |  |  |  | 0 | print $out_fmt->write( "true" ); | 
| 67 |  |  |  |  |  |  | } | 
| 68 |  |  |  |  |  |  | elsif ( isFalse( $doc ) ) { | 
| 69 | 0 |  |  |  |  | 0 | print $out_fmt->write( "false" ); | 
| 70 |  |  |  |  |  |  | } | 
| 71 |  |  |  |  |  |  | else { | 
| 72 | 0 |  |  |  |  | 0 | print $out_fmt->write( $doc ); | 
| 73 |  |  |  |  |  |  | } | 
| 74 |  |  |  |  |  |  | } | 
| 75 |  |  |  |  |  |  | } | 
| 76 |  |  |  |  |  |  |  | 
| 77 |  |  |  |  |  |  | $ENV{YQ_CLASS} ||= 'ETL::Yertl::Command::yq::Regex'; | 
| 78 |  |  |  |  |  |  | use_module( $ENV{YQ_CLASS} ); | 
| 79 |  |  |  |  |  |  | { | 
| 80 | 7 |  |  | 7 |  | 3567 | no strict 'refs'; | 
|  | 7 |  |  |  |  | 16 |  | 
|  | 7 |  |  |  |  | 182 |  | 
| 81 | 7 |  |  | 7 |  | 35 | no warnings 'once'; | 
|  | 7 |  |  |  |  | 13 |  | 
|  | 7 |  |  |  |  | 1253 |  | 
| 82 |  |  |  |  |  |  | *filter = *{ $ENV{YQ_CLASS} . "::filter" }; | 
| 83 |  |  |  |  |  |  | } | 
| 84 |  |  |  |  |  |  |  | 
| 85 |  |  |  |  |  |  | sub finish { | 
| 86 | 4 |  |  | 4 | 0 | 12635 | my ( $class, $scope ) = @_; | 
| 87 | 4 | 100 |  |  |  | 23 | if ( $scope->{sort} ) { | 
|  |  | 100 |  |  |  |  |  | 
| 88 | 1 |  |  |  |  | 2 | return map { $_->[1] } sort { $a->[0] cmp $b->[0] } @{ $scope->{sort} }; | 
|  | 3 |  |  |  |  | 22 |  | 
|  | 3 |  |  |  |  | 8 |  | 
|  | 1 |  |  |  |  | 20 |  | 
| 89 |  |  |  |  |  |  | } | 
| 90 |  |  |  |  |  |  | elsif ( $scope->{group_by} ) { | 
| 91 | 2 |  |  |  |  | 8 | return $scope->{group_by}; | 
| 92 |  |  |  |  |  |  | } | 
| 93 | 1 |  |  |  |  | 3 | return; | 
| 94 |  |  |  |  |  |  | } | 
| 95 |  |  |  |  |  |  |  | 
| 96 |  |  |  |  |  |  | 1; | 
| 97 |  |  |  |  |  |  |  | 
| 98 |  |  |  |  |  |  | __END__ |