File Coverage

blib/lib/App/RL/Command/merge.pm
Criterion Covered Total %
statement 50 52 96.1
branch 7 10 70.0
condition n/a
subroutine 11 11 100.0
pod 6 6 100.0
total 74 79 93.6


line stmt bran cond sub pod time code
1             package App::RL::Command::merge;
2 15     15   16516 use strict;
  15         469  
  15         610  
3 15     15   106 use warnings;
  15         39  
  15         964  
4 15     15   126 use autodie;
  15         36  
  15         138  
5              
6 15     15   98477 use App::RL -command;
  15         1290  
  15         2487  
7 15     15   8128 use App::RL::Common;
  15         2486  
  15         14640  
8              
9             sub abstract {
10 2     2 1 91 return 'merge runlist yaml files';
11             }
12              
13             sub opt_spec {
14 5     5 1 79 return ( [ "outfile|o=s", "output filename. [stdout] for screen" ], { show_defaults => 1, } );
15             }
16              
17             sub usage_desc {
18 5     5 1 46006 return "runlist merge [options] ";
19             }
20              
21             sub description {
22 1     1 1 945 my $desc;
23 1         5 $desc .= ucfirst(abstract) . ".\n";
24 1         3 return $desc;
25             }
26              
27             sub validate_args {
28 4     4 1 2902 my ( $self, $opt, $args ) = @_;
29              
30 4 100       10 if ( !@{$args} ) {
  4         17  
31 1         3 my $message = "This command need one or more input files.\n\tIt found";
32 1         3 $message .= sprintf " [%s]", $_ for @{$args};
  1         4  
33 1         4 $message .= ".\n";
34 1         9 $self->usage_error($message);
35             }
36 3         7 for ( @{$args} ) {
  3         9  
37 5 50       171 next if lc $_ eq "stdin";
38 5 100       21 if ( !Path::Tiny::path($_)->is_file ) {
39 1         137 $self->usage_error("The input file [$_] doesn't exist.");
40             }
41             }
42              
43 2 50       114 if ( !exists $opt->{outfile} ) {
44 0         0 $opt->{outfile} = Path::Tiny::path( $args->[0] )->absolute . ".merge.yml";
45             }
46             }
47              
48             sub execute {
49 2     2 1 23 my ( $self, $opt, $args ) = @_;
50              
51 2         8 my $out_fh;
52 2 50       13 if ( lc( $opt->{outfile} ) eq "stdout" ) {
53 2         13 $out_fh = *STDOUT;
54             }
55             else {
56 0         0 open $out_fh, ">", $opt->{outfile};
57             }
58              
59 2         8 my $master = {};
60 2         7 for my $file ( @{$args} ) {
  2         13  
61 4         20 my $basename = Path::Tiny::path($file)->basename( ".yaml", ".yml" );
62 4         489 my $dir = Path::Tiny::path($file)->parent->stringify;
63 4         415 my ($word) = split /[^\w-]+/, $basename;
64              
65 4         20 my $content = YAML::Syck::LoadFile($file);
66 4         966 $master->{$word} = $content;
67             }
68 2         7 print {$out_fh} YAML::Syck::Dump($master);
  2         15  
69              
70 2         223 close $out_fh;
71             }
72              
73             1;