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 14     14   8984 use strict;
  14         34  
  14         409  
3 14     14   86 use warnings;
  14         27  
  14         340  
4 14     14   63 use autodie;
  14         24  
  14         74  
5              
6 14     14   67162 use App::RL -command;
  14         37  
  14         127  
7 14     14   4160 use App::RL::Common;
  14         30  
  14         328  
8              
9 14     14   63 use constant abstract => 'extract some records from YAML file';
  14         27  
  14         7216  
10              
11             sub opt_spec {
12 5     5 1 28 return ( [ "outfile|o=s", "Output filename. [stdout] for screen." ], { show_defaults => 1, } );
13             }
14              
15             sub usage_desc {
16 5     5 1 38035 return "runlist some [options] ";
17             }
18              
19             sub description {
20 1     1 1 510 my $desc;
21 1         4 $desc .= ucfirst(abstract) . ".\n";
22 1         3 return $desc;
23             }
24              
25             sub validate_args {
26 4     4 1 2293 my ( $self, $opt, $args ) = @_;
27              
28 4 100       8 if ( @{$args} != 2 ) {
  4         15  
29 2         4 my $message = "This command need two input files.\n\tIt found";
30 2         5 $message .= sprintf " [%s]", $_ for @{$args};
  2         7  
31 2         6 $message .= ".\n";
32 2         10 $self->usage_error($message);
33             }
34 2         4 for ( @{$args} ) {
  2         5  
35 3 50       68 next if lc $_ eq "stdin";
36 3 100       14 if ( !Path::Tiny::path($_)->is_file ) {
37 1         105 $self->usage_error("The input file [$_] doesn't exist.");
38             }
39             }
40              
41 1 50       67 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 7 my ( $self, $opt, $args ) = @_;
48              
49             #----------------------------#
50             # Loading
51             #----------------------------#
52 1         7 my $yml = YAML::Syck::LoadFile( $args->[0] );
53 1         252 my $all_name_set = Set::Scalar->new;
54 1         200 for my $n ( @{ App::RL::Common::read_names( $args->[1] ) } ) {
  1         10  
55 3         116 $all_name_set->insert($n);
56             }
57              
58 1         47 my $out_ref = {};
59 1         2 for my $key ( keys %{$yml} ) {
  1         4  
60 5 100       23 if ( $all_name_set->has($key) ) {
61 3         35 $out_ref->{$key} = $yml->{$key};
62             }
63             }
64              
65             #----------------------------#
66             # Output
67             #----------------------------#
68 1         10 my $out_fh;
69 1 50       5 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         2 print {$out_fh} YAML::Syck::Dump($out_ref);
  1         5  
77              
78 1         109 close $out_fh;
79             }
80              
81             1;