File Coverage

blib/lib/Sys/Cmd/Template.pm
Criterion Covered Total %
statement 37 81 45.6
branch 3 34 8.8
condition 1 5 20.0
subroutine 10 13 76.9
pod 4 4 100.0
total 55 137 40.1


line stmt bran cond sub pod time code
1             package Sys::Cmd::Template;
2 1     1   13840 use strict;
  1         2  
  1         25  
3 1     1   5 use warnings;
  1         1  
  1         27  
4 1     1   16 use 5.006;
  1         3  
5 1     1   3 use Carp qw/croak confess/;
  1         1  
  1         64  
6 1     1   805 use Exporter::Tidy default => [qw/cmd_template/];
  1         8  
  1         5  
7 1     1   427 use File::Spec::Functions qw/splitdir/;
  1         571  
  1         57  
8 1     1   368 use File::Which;
  1         650  
  1         46  
9 1     1   404 use Sys::Cmd;
  1         3  
  1         5  
10 1     1   37 use Sys::Cmd::Mo qw/is default/;
  1         1  
  1         2  
11              
12             our $VERSION = '0.85.4';
13             our $CONFESS;
14              
15             sub cmd_template {
16 1     1 1 6 my @cmd = grep { ref $_ ne 'HASH' } @_;
  1         3  
17              
18 1         2 my $bin = $cmd[0];
19 1 50       3 defined $bin || confess '$cmd must be defined';
20              
21 1 50 33     24 if ( !-f $bin and splitdir($bin) < 2 ) {
22 1         11 $cmd[0] = which($bin);
23             }
24              
25 1         204 my $opts = ( grep { ref $_ eq 'HASH' } @_ )[0];
  1         3  
26              
27 1 50       5 my %args = $opts ? %$opts : ();
28 1         3 $args{cmd} = \@cmd;
29              
30 1         13 return Sys::Cmd::Template->new(%args);
31             }
32              
33             has 'cmd' => (
34             is => 'rw',
35             isa => sub { ref $_[0] eq 'ARRAY' || confess "cmd must be ARRAYREF" },
36             default => sub { [] },
37             );
38              
39             has 'dir' => (
40             is => 'rw',
41             predicate => 'have_dir',
42             );
43              
44             has 'encoding' => (
45             is => 'rw',
46             predicate => 'have_encoding',
47             );
48              
49             has 'env' => (
50             is => 'rw',
51             isa => sub { ref $_[0] eq 'HASH' || confess "env must be HASHREF" },
52             predicate => 'have_env',
53             );
54              
55             has 'input' => (
56             is => 'rw',
57             predicate => 'have_input',
58             );
59              
60             sub run {
61 0     0 1   my $self = shift;
62 0           my $proc = $self->spawn(@_);
63 0           my @out = $proc->stdout->getlines;
64 0           my @err = $proc->stderr->getlines;
65              
66 0           $proc->close;
67              
68 0 0         if ( $proc->exit != 0 ) {
69 0 0         confess( join( '', @err ) . 'Command exited with value ' . $proc->exit )
70             if $CONFESS;
71 0           croak( join( '', @err ) . 'Command exited with value ' . $proc->exit );
72             }
73              
74 0 0         if (wantarray) {
75 0           return @out;
76             }
77             else {
78 0           return join( '', @out );
79             }
80             }
81              
82             sub runx {
83 0     0 1   my $self = shift;
84 0           my $proc = $self->spawn(@_);
85 0           my @out = $proc->stdout->getlines;
86 0           my @err = $proc->stderr->getlines;
87              
88 0           $proc->close;
89              
90 0 0         if ( $proc->exit != 0 ) {
91 0 0         confess( join( '', @err ) . 'Command exited with value ' . $proc->exit )
92             if $CONFESS;
93 0           croak( join( '', @err ) . 'Command exited with value ' . $proc->exit );
94             }
95              
96 0 0         if (wantarray) {
97 0           return @out, @err;
98             }
99             else {
100 0           return join( '', @out, @err );
101             }
102             }
103              
104             sub spawn {
105 0     0 1   my $self = shift;
106 0           my %args = ( cmd => [ @{ $self->cmd }, grep { ref $_ ne 'HASH' } @_ ], );
  0            
  0            
107              
108 0   0       my $opts = ( grep { ref $_ eq 'HASH' } @_ )[0] || {};
109              
110 0 0         if ( exists $opts->{dir} ) {
    0          
111 0           $args{dir} = $opts->{dir};
112             }
113             elsif ( $self->have_dir ) {
114 0           $args{dir} = $self->dir;
115             }
116              
117 0 0         if ( exists $opts->{encoding} ) {
    0          
118 0           $args{encoding} = $opts->{encoding};
119             }
120             elsif ( $self->have_encoding ) {
121 0           $args{encoding} = $self->encoding;
122             }
123              
124 0 0         if ( $self->have_env ) {
125 0           $args{env} = { %{ $self->env } };
  0            
126             }
127              
128 0 0         if ( exists $opts->{env} ) {
129 0           while ( my ( $key, $val ) = each %{ $opts->{env} } ) {
  0            
130 0           $args{env}->{$key} = $val;
131             }
132             }
133              
134 0 0         if ( exists $opts->{input} ) {
    0          
135 0           $args{input} = $opts->{input};
136             }
137             elsif ( $self->have_input ) {
138 0           $args{input} = $self->input;
139             }
140              
141 0           return Sys::Cmd->new(%args);
142             }
143              
144             1;
145              
146             __END__