File Coverage

blib/lib/App/RL/Command/some.pm
Criterion Covered Total %
statement 54 56 96.4
branch 9 12 75.0
condition n/a
subroutine 11 11 100.0
pod 5 5 100.0
total 79 84 94.0


line stmt bran cond sub pod time code
1             package App::RL::Command::some;
2 13     13   8320 use strict;
  13         30  
  13         397  
3 13     13   62 use warnings;
  13         30  
  13         300  
4 13     13   57 use autodie;
  13         26  
  13         72  
5              
6 13     13   62647 use App::RL -command;
  13         31  
  13         433  
7 13     13   4043 use App::RL::Common;
  13         29  
  13         312  
8              
9 13     13   56 use constant abstract => 'extract some records from YAML file';
  13         27  
  13         6773  
10              
11             sub opt_spec {
12 5     5 1 37 return ( [ "outfile|o=s", "Output filename. [stdout] for screen." ], { show_defaults => 1, } );
13             }
14              
15             sub usage_desc {
16 5     5 1 58788 return "runlist some [options] ";
17             }
18              
19             sub description {
20 1     1 1 1081 my $desc;
21 1         4 $desc .= ucfirst(abstract) . ".\n";
22 1         5 return $desc;
23             }
24              
25             sub validate_args {
26 4     4 1 11758 my ( $self, $opt, $args ) = @_;
27              
28 4 100       16 if ( @{$args} != 2 ) {
  4         20  
29 2         4 my $message = "This command need two input files.\n\tIt found";
30 2         4 $message .= sprintf " [%s]", $_ for @{$args};
  2         9  
31 2         5 $message .= ".\n";
32 2         12 $self->usage_error($message);
33             }
34 2         9 for ( @{$args} ) {
  2         9  
35 3 50       128 next if lc $_ eq "stdin";
36 3 100       19 if ( !Path::Tiny::path($_)->is_file ) {
37 1         169 $self->usage_error("The input file [$_] doesn't exist.");
38             }
39             }
40              
41 1 50       72 if ( !exists $opt->{outfile} ) {
42 0         0 $opt->{outfile} = Path::Tiny::path( $args->[0] )->absolute . ".list.yml";
43             }
44             }
45              
46             sub execute {
47 1     1 1 12 my ( $self, $opt, $args ) = @_;
48              
49             #----------------------------#
50             # Loading
51             #----------------------------#
52 1         8 my $yml = YAML::Syck::LoadFile( $args->[0] );
53 1         418 my $all_name_set = Set::Scalar->new;
54 1         294 for my $n ( @{ App::RL::Common::read_names( $args->[1] ) } ) {
  1         9  
55 3         213 $all_name_set->insert($n);
56             }
57              
58 1         84 my $out_ref = {};
59 1         4 for my $key ( keys %{$yml} ) {
  1         6  
60 5 100       68 if ( $all_name_set->has($key) ) {
61 3         52 $out_ref->{$key} = $yml->{$key};
62             }
63             }
64              
65             #----------------------------#
66             # Output
67             #----------------------------#
68 1         4 my $out_fh;
69 1 50       9 if ( lc( $opt->{outfile} ) eq "stdout" ) {
70 1         5 $out_fh = *STDOUT;
71             }
72             else {
73 0         0 open $out_fh, ">", $opt->{outfile};
74             }
75              
76 1         6 print {$out_fh} YAML::Syck::Dump($out_ref);
  1         10  
77              
78 1         405 close $out_fh;
79             }
80              
81             1;