Callback/CommandRunner.pm

filter: Statementstmt 92.3% 60 / 65 Branchbran 66.6% 8 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 93.7% 15 / 16 Podpod 100% 5 / 5 totaltotal 89.8% 88 / 98 CC 7 SCAR 5.8
CC
7
Cov
88%
run_or_die
3.1
_build_callback
3.0
_condense
2.1
CRAP 1.8
« Callback/Logger.pm ↑ Summary
1 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 package IPC::Open3::Callback::CommandRunner;
5 1 $IPC::Open3::Callback::CommandRunner::VERSION = '1.19';
6 # ABSTRACT: A utility class that wraps IPC::Open3::Callback with available output buffers and an option to die on failure instead of returning exit code.
7  
8 1 ▶use Hash::Util qw(lock_keys);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use IPC::Open3::Callback;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use IPC::Open3::Callback::CommandFailedException;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
13 1 return bless( {}, shift )->_init(@_);
14 }
15  
16 6 ▶sub _build_callback {
Subroutine _build_callback: called CC=3 SCAR=11.1
17 6 my ( $self, $out_or_err, $options ) = @_;
18  
19 6 ▶ if ( defined( $options->{ $out_or_err . '_callback' } ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $$options{$out_or_err . '_callback'}) { } 0 6 6
elsif ($$options{$out_or_err . '_buffer'}) { } 3 3 6
20 0 return $options->{ $out_or_err . '_callback' };
21 }
22 elsif ( $options->{ $out_or_err . '_buffer' } ) {
23 3 $self->{ $out_or_err . '_buffer' } = ();
24 6 ▶ return sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
25 6 push( @{ $self->{ $out_or_err . '_buffer' } }, shift );
26 3 };
27 }
28 3 return;
29 }
30  
31 4 ▶sub _clear_buffers {
Subroutine _clear_buffers: called CC=1 SCAR=0.0
32 4 my ($self) = @_;
33 4 delete( $self->{out_buffer} );
34 4 delete( $self->{err_buffer} );
35 }
36  
37 3 ▶sub _condense {
Subroutine _condense: called CC=2 SCAR=7.2
38 3 my ( $self, $buffer ) = @_;
39  
40 3 ▶ if ( $self->{$buffer} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{$buffer}) 3 0 3
41 3 $self->{$buffer} = [ join( '', @{ $self->{$buffer} } ) ];
42 3 return $self->{$buffer}[0];
43 }
44  
45 0 return;
46 }
47  
48 0 ▶sub get_err_buffer {
Subroutine get_err_buffer: not called CC=1 SCAR=6.9
Pod documented
49 0 return $_[0]->_condense('err_buffer');
50 }
51  
52 1 ▶sub _init {
Subroutine _init: called CC=1 SCAR=0.0
53 1 my ( $self, @options ) = @_;
54  
55 1 $self->{command_runner} = IPC::Open3::Callback->new(@options);
56  
57 1 lock_keys( %{$self}, keys( %{$self} ), 'out_buffer', 'err_buffer' );
58  
59 1 return $self;
60 }
61  
62 3 ▶sub _options {
Subroutine _options: called CC=1 SCAR=0.0
63 3 my ( $self, %options ) = @_;
64  
65 3 $options{out_callback} = $self->_build_callback( 'out', \%options );
66 3 $options{err_callback} = $self->_build_callback( 'err', \%options );
67  
68 3 return %options;
69 }
70  
71 3 ▶sub get_out_buffer {
Subroutine get_out_buffer: called CC=1 SCAR=0.0
Pod documented
72 3 return $_[0]->_condense('out_buffer');
73 }
74  
75 1 ▶sub run {
Subroutine run: called CC=2 SCAR=7.0
Pod documented
76 1 my ( $self, @command ) = @_;
77 1 my %options = ();
78  
79 # if last arg is hashref, its command options not arg...
80 1 ▶ if ( ref( $command[-1] ) eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $command[-1] eq "HASH") 1 0 1
81 1 %options = $self->_options( %{ pop(@command) } );
82 }
83  
84 1 $self->_clear_buffers();
85  
86 1 return $self->{command_runner}->run_command( @command, \%options );
87 }
88  
89 3 ▶sub run_or_die {
Subroutine run_or_die: called CC=3 SCAR=11.2
Pod documented
90 3 my ( $self, @command ) = @_;
91 3 my %options = ();
92  
93 # if last arg is hashref, its command options not arg...
94 3 ▶ if ( ref( $command[-1] ) eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $command[-1] eq "HASH") 2 1 3
95 2 %options = $self->_options( %{ pop(@command) } );
96 }
97  
98 3 $self->_clear_buffers();
99  
100 3 my $exit_code = $self->{command_runner}->run_command( @command, \%options );
101 2 ▶ if ($exit_code) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exit_code) 0 2 2
102 0 my $exception = IPC::Open3::Callback::CommandFailedException->new(
103 \@command, $exit_code,
104 scalar( $self->get_out_buffer() ),
105 scalar( $self->get_err_buffer() )
106 );
107 0 die($exception);
108 }
109  
110 2 return $self->get_out_buffer();
111 }
112  
113 1 1;
114  
115 __END__
« Callback/Logger.pm ↑ Summary