| 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::Command; | ||||||||||||||||||||||||||||||||
| 5 | 1 | $IPC::Open3::Callback::Command::VERSION = '1.18'; | |||||||||||||||||||||||||||||||
| 6 | # ABSTRACT: A utility class that provides subroutines for building shell command strings. | ||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use Exporter qw(import); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 9 | 1 | our @EXPORT_OK = | |||||||||||||||||||||||||||||||
| 10 | qw(batch_command command command_options cp_command mkdir_command pipe_command rm_command sed_command write_command); | ||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||
| 12 | 1 | ▶ | use File::Spec; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||
| 14 | sub batch_command { | ||||||||||||||||||||||||||||||||
| 15 | 13 | ▶ | my ( @commands, $batch_options, $command_options ); | ||||||||||||||||||||||||||||||
|
Subroutine
batch_command: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||||||
| 16 | 13 | (@commands) = @_; | |||||||||||||||||||||||||||||||
| 17 | 13 | ▶ | $command_options = pop(@commands) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 18 | if ( ref( $commands[$#commands] ) eq 'IPC::Open3::Callback::Command::CommandOptions' ); | ||||||||||||||||||||||||||||||||
| 19 | 13 | ▶ | $batch_options = pop(@commands) if ( ref( $commands[$#commands] ) eq 'HASH' ); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||
| 21 | 13 | ▶ | push( @commands, $command_options ) if ($command_options); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||
| 23 | wrap( | ||||||||||||||||||||||||||||||||
| 24 | $batch_options || {}, | ||||||||||||||||||||||||||||||||
| 25 | @commands, | ||||||||||||||||||||||||||||||||
| 26 | sub { | ||||||||||||||||||||||||||||||||
| 27 | 13 | ▶ | return @_; | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||||||
| 29 | 13 | ▶ | ); | ||||||||||||||||||||||||||||||
|
Condition100%2/2
$batch_options || {}
MC/DC100%1/1
$batch_options || {}
$batch_options
Truth table100%2/2
A
A= $batch_options
| |||||||||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||
| 32 | sub command_options { | ||||||||||||||||||||||||||||||||
| 33 | 40 | ▶ | return IPC::Open3::Callback::Command::CommandOptions->new(@_); | ||||||||||||||||||||||||||||||
|
Subroutine
command_options: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||
| 36 | sub command { | ||||||||||||||||||||||||||||||||
| 37 | wrap( | ||||||||||||||||||||||||||||||||
| 38 | {}, | ||||||||||||||||||||||||||||||||
| 39 | @_, | ||||||||||||||||||||||||||||||||
| 40 | sub { | ||||||||||||||||||||||||||||||||
| 41 | 31 | ▶ | return shift; | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||||||
| 43 | 31 | ▶ | ); | ||||||||||||||||||||||||||||||
|
Subroutine
command: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||
| 46 | sub cp_command { | ||||||||||||||||||||||||||||||||
| 47 | 13 | ▶ | my ( $source_path, $source_command_options, $destination_path, $destination_command_options, | ||||||||||||||||||||||||||||||
|
Subroutine
cp_command: called CC=14 SCAR=26.5
Pod
documented
| |||||||||||||||||||||||||||||||||
| 48 | %cp_options ); | ||||||||||||||||||||||||||||||||
| 49 | 13 | $source_path = _escape_path(shift); | |||||||||||||||||||||||||||||||
| 50 | 13 | ▶ | $source_command_options = shift | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 51 | if ( ref( $_[0] ) eq 'IPC::Open3::Callback::Command::CommandOptions' ); | ||||||||||||||||||||||||||||||||
| 52 | 13 | $destination_path = _escape_path(shift); | |||||||||||||||||||||||||||||||
| 53 | 13 | ▶ | $destination_command_options = shift | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 54 | if ( ref( $_[0] ) eq 'IPC::Open3::Callback::Command::CommandOptions' ); | ||||||||||||||||||||||||||||||||
| 55 | 13 | %cp_options = @_; | |||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||
| 57 | 13 | ▶ | $source_command_options = command_options() if ( !$source_command_options ); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 58 | 13 | ▶ | $destination_command_options = command_options() if ( !$destination_command_options ); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||
| 60 | 13 | my $source_command; | |||||||||||||||||||||||||||||||
| 61 | my $destination_command; | ||||||||||||||||||||||||||||||||
| 62 | 13 | ▶ | if ( $cp_options{file} ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||
| 64 | # is a file, so use cat | dd | ||||||||||||||||||||||||||||||||
| 65 | 6 | ▶ | if ( $cp_options{compress} ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 66 | 1 | $source_command = command( "gzip -c $source_path", $source_command_options ); | |||||||||||||||||||||||||||||||
| 67 | 1 | $destination_command = | |||||||||||||||||||||||||||||||
| 68 | pipe_command( "gunzip", "dd of=$destination_path", $destination_command_options ); | ||||||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||||||
| 70 | else { | ||||||||||||||||||||||||||||||||
| 71 | 5 | $source_command = command( "cat $source_path", $source_command_options ); | |||||||||||||||||||||||||||||||
| 72 | 5 | $destination_command = | |||||||||||||||||||||||||||||||
| 73 | command( "dd of=$destination_path", $destination_command_options ); | ||||||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||||||
| 76 | else { | ||||||||||||||||||||||||||||||||
| 77 | # is a directory, so use tar or unzip | ||||||||||||||||||||||||||||||||
| 78 | 7 | ▶ | if ( $cp_options{archive} && $cp_options{archive} eq 'zip' ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$cp_options{"archive"} and $cp_options{"archive"} eq "zip"
MC/DC50%1/2
$cp_options{"archive"} and $cp_options{"archive"} eq "zip"
$cp_options{"archive"}
$cp_options{"archive"} eq "zip"
Truth table66%2/3
A and B
A= $cp_options{"archive"}B= $cp_options{"archive"} eq "zip"
| |||||||||||||||||||||||||||||||||
| 79 | my $temp_zip = File::Spec->catfile( $destination_path, | ||||||||||||||||||||||||||||||||
| 80 | 2 | ▶ | $cp_options{unzip_temp_file} || "temp_cp_command.zip" ); | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$cp_options{'unzip_temp_file'} || 'temp_cp_command.zip'
MC/DC0%0/1
$cp_options{'unzip_temp_file'} || 'temp_cp_command.zip'
$cp_options{'unzip_temp_file'}
Truth table50%1/2
A
A= $cp_options{'unzip_temp_file'}
| |||||||||||||||||||||||||||||||||
| 81 | 2 | $source_command = | |||||||||||||||||||||||||||||||
| 82 | command( | ||||||||||||||||||||||||||||||||
| 83 | batch_command( "cd $source_path", "zip -qr - .", { subshell => "bash -c " } ), | ||||||||||||||||||||||||||||||||
| 84 | $source_command_options ); | ||||||||||||||||||||||||||||||||
| 85 | 2 | $destination_command = batch_command( | |||||||||||||||||||||||||||||||
| 86 | "dd of=$temp_zip", "unzip -qod $destination_path $temp_zip", | ||||||||||||||||||||||||||||||||
| 87 | rm_command($temp_zip), $destination_command_options | ||||||||||||||||||||||||||||||||
| 88 | ); | ||||||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||||||
| 90 | else { | ||||||||||||||||||||||||||||||||
| 91 | # default, use tar | ||||||||||||||||||||||||||||||||
| 92 | 5 | my @parts = ("tar c -C $source_path ."); | |||||||||||||||||||||||||||||||
| 93 | 5 | my @destination_parts = (); | |||||||||||||||||||||||||||||||
| 94 | 5 | ▶ | if ( $cp_options{status} ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 95 | 1 | ▶ | push( | ||||||||||||||||||||||||||||||
|
Condition33%1/3
$source_command_options && $source_command_options->get_sudo_username
MC/DC0%0/2
$source_command_options && $source_command_options->get_sudo_username
$source_command_options
$source_command_options->get_sudo_username
Truth table33%1/3
A && B
A= $source_command_optionsB= $source_command_options->get_sudo_username
| |||||||||||||||||||||||||||||||||
| 96 | @parts, | ||||||||||||||||||||||||||||||||
| 97 | join( | ||||||||||||||||||||||||||||||||
| 98 | '', | ||||||||||||||||||||||||||||||||
| 99 | 'pv -f -s `', | ||||||||||||||||||||||||||||||||
| 100 | _sudo_command( | ||||||||||||||||||||||||||||||||
| 101 | $source_command_options | ||||||||||||||||||||||||||||||||
| 102 | && $source_command_options->get_sudo_username(), | ||||||||||||||||||||||||||||||||
| 103 | pipe_command( "du -sb $source_path", 'cut -f1' ) | ||||||||||||||||||||||||||||||||
| 104 | ), | ||||||||||||||||||||||||||||||||
| 105 | '`' | ||||||||||||||||||||||||||||||||
| 106 | ) | ||||||||||||||||||||||||||||||||
| 107 | ); | ||||||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||||||
| 109 | 5 | ▶ | if ( $cp_options{compress} ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 110 | 2 | push( @parts, 'gzip' ); | |||||||||||||||||||||||||||||||
| 111 | 2 | push( @destination_parts, 'gunzip' ); | |||||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||||||
| 113 | push( | ||||||||||||||||||||||||||||||||
| 114 | 5 | ▶ | @destination_parts, | ||||||||||||||||||||||||||||||
|
Condition66%2/3
$destination_command_options && $destination_command_options->get_sudo_username
MC/DC50%1/2
$destination_command_options && $destination_command_options->get_sudo_username
$destination_command_options
$destination_command_options->get_sudo_username
Truth table66%2/3
A && B
A= $destination_command_optionsB= $destination_command_options->get_sudo_username
| |||||||||||||||||||||||||||||||||
| 115 | _sudo_command( | ||||||||||||||||||||||||||||||||
| 116 | $destination_command_options | ||||||||||||||||||||||||||||||||
| 117 | && $destination_command_options->get_sudo_username(), | ||||||||||||||||||||||||||||||||
| 118 | "tar x -C $destination_path" | ||||||||||||||||||||||||||||||||
| 119 | ) | ||||||||||||||||||||||||||||||||
| 120 | ); | ||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||
| 122 | 5 | $source_command = command( pipe_command(@parts), $source_command_options ); | |||||||||||||||||||||||||||||||
| 123 | 5 | $destination_command = command( pipe_command(@destination_parts), | |||||||||||||||||||||||||||||||
| 124 | $destination_command_options->clone( sudo_username => undef ) ); | ||||||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||
| 128 | 13 | return pipe_command( $source_command, $destination_command ); | |||||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||
| 131 | sub mkdir_command { | ||||||||||||||||||||||||||||||||
| 132 | wrap( | ||||||||||||||||||||||||||||||||
| 133 | {}, | ||||||||||||||||||||||||||||||||
| 134 | @_, | ||||||||||||||||||||||||||||||||
| 135 | sub { | ||||||||||||||||||||||||||||||||
| 136 | 1 | ▶ | return 'mkdir -p "' . join( '" "', @_ ) . '"'; | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||||||
| 138 | 1 | ▶ | ); | ||||||||||||||||||||||||||||||
|
Subroutine
mkdir_command: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||
| 141 | sub pipe_command { | ||||||||||||||||||||||||||||||||
| 142 | wrap( | ||||||||||||||||||||||||||||||||
| 143 | { command_separator => '|' }, | ||||||||||||||||||||||||||||||||
| 144 | @_, | ||||||||||||||||||||||||||||||||
| 145 | sub { | ||||||||||||||||||||||||||||||||
| 146 | 32 | ▶ | return @_; | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||||||
| 148 | 32 | ▶ | ); | ||||||||||||||||||||||||||||||
|
Subroutine
pipe_command: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||
| 151 | sub _quote_command { | ||||||||||||||||||||||||||||||||
| 152 | 32 | ▶ | my ($command) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
_quote_command: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 153 | 32 | $command =~ s/\\/\\\\/g; | |||||||||||||||||||||||||||||||
| 154 | 32 | $command =~ s/`/\\`/g; # for `command` | |||||||||||||||||||||||||||||||
| 155 | 32 | $command =~ s/"/\\"/g; | |||||||||||||||||||||||||||||||
| 156 | 32 | return "\"$command\""; | |||||||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||
| 159 | sub _escape_path { | ||||||||||||||||||||||||||||||||
| 160 | 26 | ▶ | my ($path) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
_escape_path: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 161 | 26 | $path =~ s/(['"`\s])/\\$1/g; | |||||||||||||||||||||||||||||||
| 162 | 26 | return $path; | |||||||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||
| 165 | sub rm_command { | ||||||||||||||||||||||||||||||||
| 166 | wrap( | ||||||||||||||||||||||||||||||||
| 167 | {}, | ||||||||||||||||||||||||||||||||
| 168 | @_, | ||||||||||||||||||||||||||||||||
| 169 | sub { | ||||||||||||||||||||||||||||||||
| 170 | 4 | ▶ | return 'rm -rf "' . join( '" "', @_ ) . '"'; | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||||||
| 172 | 4 | ▶ | ); | ||||||||||||||||||||||||||||||
|
Subroutine
rm_command: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||||||
| 175 | sub sed_command { | ||||||||||||||||||||||||||||||||
| 176 | wrap( | ||||||||||||||||||||||||||||||||
| 177 | {}, | ||||||||||||||||||||||||||||||||
| 178 | @_, | ||||||||||||||||||||||||||||||||
| 179 | sub { | ||||||||||||||||||||||||||||||||
| 180 | 2 | ▶ | my @args = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=9 SCAR=27.6
| |||||||||||||||||||||||||||||||||
| 181 | 2 | my $options = {}; | |||||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||||||
| 183 | 2 | ▶ | if ( ref( $args[$#args] ) eq 'HASH' ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 184 | 1 | $options = pop(@args); | |||||||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||||||
| 187 | 2 | my $command = 'sed'; | |||||||||||||||||||||||||||||||
| 188 | 2 | ▶ | $command .= ' -i' if ( $options->{in_place} ); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 189 | 2 | ▶ | if ( defined( $options->{temp_script_file} ) ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 190 | 0 | my $temp_script_file_name = $options->{temp_script_file}->filename(); | |||||||||||||||||||||||||||||||
| 191 | 0 | ▶ | print( { $options->{temp_script_file} } join( ' ', '', map {"$_;"} @args ) ) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 192 | if ( scalar(@args) ); | ||||||||||||||||||||||||||||||||
| 193 | print( | ||||||||||||||||||||||||||||||||
| 194 | 0 | { $options->{temp_script_file} } join( ' ', | |||||||||||||||||||||||||||||||
| 195 | '', | ||||||||||||||||||||||||||||||||
| 196 | 0 | map {"s/$_/$options->{replace_map}{$_}/g;"} | |||||||||||||||||||||||||||||||
| 197 | 0 | keys( %{ $options->{replace_map} } ) ) | |||||||||||||||||||||||||||||||
| 198 | 0 | ▶ | ) if ( defined( $options->{replace_map} ) ); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 199 | 0 | $options->{temp_script_file}->flush(); | |||||||||||||||||||||||||||||||
| 200 | 0 | $command .= " -f $temp_script_file_name"; | |||||||||||||||||||||||||||||||
| 201 | } | ||||||||||||||||||||||||||||||||
| 202 | else { | ||||||||||||||||||||||||||||||||
| 203 | 2 | ▶ | $command .= join( ' ', '', map {"-e '$_'"} @args ) if ( scalar(@args) ); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 204 | $command .= join( ' ', | ||||||||||||||||||||||||||||||||
| 205 | '', | ||||||||||||||||||||||||||||||||
| 206 | 1 | map {"-e 's/$_/$options->{replace_map}{$_}/g'"} | |||||||||||||||||||||||||||||||
| 207 | 1 | keys( %{ $options->{replace_map} } ) ) | |||||||||||||||||||||||||||||||
| 208 | 2 | ▶ | if ( defined( $options->{replace_map} ) ); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 209 | } | ||||||||||||||||||||||||||||||||
| 210 | 2 | ▶ | $command .= join( ' ', '', @{ $options->{files} } ) if ( $options->{files} ); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||||||
| 212 | 2 | return $command; | |||||||||||||||||||||||||||||||
| 213 | } | ||||||||||||||||||||||||||||||||
| 214 | 2 | ▶ | ); | ||||||||||||||||||||||||||||||
|
Subroutine
sed_command: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 215 | } | ||||||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||||||
| 217 | sub _sudo_command { | ||||||||||||||||||||||||||||||||
| 218 | 132 | ▶ | my ( $sudo_username, $command ) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
_sudo_command: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||
| 219 | 132 | ▶ | if ( defined($sudo_username) ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 220 | 24 | ▶ | $command = "sudo " . ( $sudo_username ? "-u $sudo_username " : '' ) . $command; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 221 | } | ||||||||||||||||||||||||||||||||
| 222 | 132 | return $command; | |||||||||||||||||||||||||||||||
| 223 | } | ||||||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||||||
| 225 | sub write_command { | ||||||||||||||||||||||||||||||||
| 226 | 5 | ▶ | my ( $filename, @lines, $write_options, $command_options ); | ||||||||||||||||||||||||||||||
|
Subroutine
write_command: called CC=10 SCAR=23.4
Pod
documented
| |||||||||||||||||||||||||||||||||
| 227 | 5 | $filename = shift; | |||||||||||||||||||||||||||||||
| 228 | 5 | @lines = @_; | |||||||||||||||||||||||||||||||
| 229 | 5 | ▶ | $command_options = pop(@lines) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 230 | if ( ref( $lines[$#lines] ) eq 'IPC::Open3::Callback::Command::CommandOptions' ); | ||||||||||||||||||||||||||||||||
| 231 | 5 | ▶ | $write_options = pop(@lines) if ( ref( $lines[$#lines] ) eq 'HASH' ); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||||||
| 233 | 5 | my $remote_command = "dd of=$filename"; | |||||||||||||||||||||||||||||||
| 234 | 5 | ▶ | if ( defined($write_options) && defined( $write_options->{mode} ) ) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition66%2/3
defined $write_options and defined $write_options->{"mode"}
MC/DC50%1/2
defined $write_options and defined $write_options->{"mode"}
defined $write_options
defined $write_options->{"mode"}
Truth table66%2/3
A and B
A= defined $write_optionsB= defined $write_options->{"mode"}
| |||||||||||||||||||||||||||||||||
| 235 | 3 | ▶ | if ( defined($command_options) ) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 236 | 3 | $remote_command = | |||||||||||||||||||||||||||||||
| 237 | batch_command( $remote_command, "chmod $write_options->{mode} $filename", | ||||||||||||||||||||||||||||||||
| 238 | $command_options ); | ||||||||||||||||||||||||||||||||
| 239 | } | ||||||||||||||||||||||||||||||||
| 240 | elsif ( defined($command_options) ) { | ||||||||||||||||||||||||||||||||
| 241 | 0 | $remote_command = | |||||||||||||||||||||||||||||||
| 242 | batch_command( $remote_command, "chmod $write_options->{mode} $filename" ); | ||||||||||||||||||||||||||||||||
| 243 | } | ||||||||||||||||||||||||||||||||
| 244 | } | ||||||||||||||||||||||||||||||||
| 245 | elsif ( defined($command_options) ) { | ||||||||||||||||||||||||||||||||
| 246 | 1 | $remote_command = command( $remote_command, $command_options ); | |||||||||||||||||||||||||||||||
| 247 | } | ||||||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||||||
| 249 | my $line_separator = | ||||||||||||||||||||||||||||||||
| 250 | ( defined($write_options) && defined( $write_options->{line_separator} ) ) | ||||||||||||||||||||||||||||||||
| 251 | ? $write_options->{line_separator} | ||||||||||||||||||||||||||||||||
| 252 | 5 | ▶ | : '\n'; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $write_options and defined $write_options->{"line_separator"}
MC/DC100%2/2
defined $write_options and defined $write_options->{"line_separator"}
defined $write_options
defined $write_options->{"line_separator"}
Truth table100%3/3
A and B
A= defined $write_optionsB= defined $write_options->{"line_separator"}
| |||||||||||||||||||||||||||||||||
| 253 | 5 | return pipe_command( 'printf "' . join( $line_separator, @lines ) . '"', $remote_command ); | |||||||||||||||||||||||||||||||
| 254 | } | ||||||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||||||
| 256 | # Handles wrapping commands with possible ssh and command prefix | ||||||||||||||||||||||||||||||||
| 257 | sub wrap { | ||||||||||||||||||||||||||||||||
| 258 | 83 | ▶ | my $wrap_options = shift; | ||||||||||||||||||||||||||||||
|
Subroutine
wrap: called CC=14 SCAR=26.4
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 259 | 83 | my $builder = pop; | |||||||||||||||||||||||||||||||
| 260 | 83 | my @args = @_; | |||||||||||||||||||||||||||||||
| 261 | 83 | my ( $ssh, $username, $hostname, $sudo_username, $pretty ); | |||||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||||||
| 263 | 83 | ▶ | if ( ref( $args[$#args] ) eq 'IPC::Open3::Callback::Command::CommandOptions' ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 264 | 40 | my $options = pop(@args); | |||||||||||||||||||||||||||||||
| 265 | 40 | ▶ | $ssh = $options->get_ssh() || 'ssh'; | ||||||||||||||||||||||||||||||
|
Condition100%2/2
$options->get_ssh || 'ssh'
MC/DC100%1/1
$options->get_ssh || 'ssh'
$options->get_ssh
Truth table100%2/2
A
A= $options->get_ssh
| |||||||||||||||||||||||||||||||||
| 266 | 40 | $username = $options->get_username(); | |||||||||||||||||||||||||||||||
| 267 | 40 | $hostname = $options->get_hostname(); | |||||||||||||||||||||||||||||||
| 268 | 40 | $sudo_username = $options->get_sudo_username(); | |||||||||||||||||||||||||||||||
| 269 | 40 | $pretty = $options->get_pretty(); | |||||||||||||||||||||||||||||||
| 270 | } | ||||||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||||||
| 272 | 83 | my $destination_command = ''; | |||||||||||||||||||||||||||||||
| 273 | 83 | ▶ | my $command_separator = $wrap_options->{command_separator} || ';'; | ||||||||||||||||||||||||||||||
|
Condition100%2/2
$wrap_options->{'command_separator'} || ';'
MC/DC100%1/1
$wrap_options->{'command_separator'} || ';'
$wrap_options->{'command_separator'}
Truth table100%2/2
A
A= $wrap_options->{'command_separator'}
| |||||||||||||||||||||||||||||||||
| 274 | 83 | my $commands = 0; | |||||||||||||||||||||||||||||||
| 275 | 83 | foreach my $command ( &$builder(@args) ) { | |||||||||||||||||||||||||||||||
| 276 | 126 | ▶ | if ( defined($command) ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 277 | 126 | ▶ | if ( $commands++ > 0 ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 278 | 43 | $destination_command .= $command_separator; | |||||||||||||||||||||||||||||||
| 279 | 43 | ▶ | if ($pretty) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 280 | 0 | $destination_command .= "\n"; | |||||||||||||||||||||||||||||||
| 281 | } | ||||||||||||||||||||||||||||||||
| 282 | } | ||||||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||||||
| 284 | 126 | $command =~ s/^(.*?[^\\]);$/$1/; # from find -exec | |||||||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||||||
| 286 | 126 | $command = _sudo_command( $sudo_username, $command ); | |||||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||||||
| 288 | 126 | $destination_command .= $command; | |||||||||||||||||||||||||||||||
| 289 | } | ||||||||||||||||||||||||||||||||
| 290 | } | ||||||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||||||
| 292 | 83 | ▶ | if ( $wrap_options->{subshell} ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 293 | 3 | $destination_command = $wrap_options->{subshell} . _quote_command($destination_command); | |||||||||||||||||||||||||||||||
| 294 | } | ||||||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||||||
| 296 | 83 | ▶ | if ( !defined($username) && !defined($hostname) ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $username or defined $hostname
MC/DC100%2/2
defined $username or defined $hostname
defined $username
defined $hostname
Truth table100%3/3
A or B
A= defined $usernameB= defined $hostname
| |||||||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||||||
| 298 | # silly to ssh to localhost as current user, so dont | ||||||||||||||||||||||||||||||||
| 299 | 54 | return $destination_command; | |||||||||||||||||||||||||||||||
| 300 | } | ||||||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||||||
| 302 | 29 | ▶ | my $userAt = | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 303 | $username | ||||||||||||||||||||||||||||||||
| 304 | ? ( ( $ssh =~ /plink(?:\.exe)?$/ ) ? "-l $username " : "$username\@" ) | ||||||||||||||||||||||||||||||||
| 305 | : ''; | ||||||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||||||
| 307 | 29 | $destination_command = _quote_command($destination_command); | |||||||||||||||||||||||||||||||
| 308 | 29 | ▶ | return "$ssh $userAt" . ( $hostname || 'localhost' ) . " $destination_command"; | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$hostname || 'localhost'
MC/DC0%0/1
$hostname || 'localhost'
$hostname
Truth table50%1/2
A
A= $hostname
| |||||||||||||||||||||||||||||||||
| 309 | } | ||||||||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||||||
| 311 | package IPC::Open3::Callback::Command::CommandOptions; | ||||||||||||||||||||||||||||||||
| 312 | 1 | $IPC::Open3::Callback::Command::CommandOptions::VERSION = '1.18'; | |||||||||||||||||||||||||||||||
| 313 | 1 | ▶ | use parent qw(Class::Accessor); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 314 | 1 | __PACKAGE__->follow_best_practice; | |||||||||||||||||||||||||||||||
| 315 | 1 | __PACKAGE__->mk_ro_accessors(qw(hostname pretty ssh sudo_username username)); | |||||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||||||
| 317 | sub new { | ||||||||||||||||||||||||||||||||
| 318 | 45 | ▶ | my ( $class, @args ) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 319 | 45 | return bless( {}, $class )->_init(@args); | |||||||||||||||||||||||||||||||
| 320 | } | ||||||||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||||||
| 322 | sub clone { | ||||||||||||||||||||||||||||||||
| 323 | 5 | ▶ | my ( $instance, %options ) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
clone: called CC=11 SCAR=30.3
| |||||||||||||||||||||||||||||||||
| 324 | 5 | ▶ | if ( exists( $instance->{hostname} ) && !exists( $options{hostname} ) ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
exists $instance->{"hostname"} and not exists $options{"hostname"}
MC/DC50%1/2
exists $instance->{"hostname"} and not exists $options{"hostname"}
exists $instance->{"hostname"}
not exists $options{"hostname"}
Truth table66%2/3
A and not(B)
A= exists $instance->{"hostname"}B= not exists $options{"hostname"}
| |||||||||||||||||||||||||||||||||
| 325 | 3 | $options{hostname} = $instance->{hostname}; | |||||||||||||||||||||||||||||||
| 326 | } | ||||||||||||||||||||||||||||||||
| 327 | 5 | ▶ | if ( exists( $instance->{ssh} ) && !exists( $options{ssh} ) ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
exists $instance->{"ssh"} and not exists $options{"ssh"}
MC/DC0%0/2
exists $instance->{"ssh"} and not exists $options{"ssh"}
exists $instance->{"ssh"}
not exists $options{"ssh"}
Truth table33%1/3
A and not(B)
A= exists $instance->{"ssh"}B= not exists $options{"ssh"}
| |||||||||||||||||||||||||||||||||
| 328 | 0 | $options{ssh} = $instance->{ssh}; | |||||||||||||||||||||||||||||||
| 329 | } | ||||||||||||||||||||||||||||||||
| 330 | 5 | ▶ | if ( exists( $instance->{username} ) && !exists( $options{username} ) ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
exists $instance->{"username"} and not exists $options{"username"}
MC/DC0%0/2
exists $instance->{"username"} and not exists $options{"username"}
exists $instance->{"username"}
not exists $options{"username"}
Truth table33%1/3
A and not(B)
A= exists $instance->{"username"}B= not exists $options{"username"}
| |||||||||||||||||||||||||||||||||
| 331 | 0 | $options{username} = $instance->{username}; | |||||||||||||||||||||||||||||||
| 332 | } | ||||||||||||||||||||||||||||||||
| 333 | 5 | ▶ | if ( exists( $instance->{sudo_username} ) && !exists( $options{sudo_username} ) ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
exists $instance->{"sudo_username"} and not exists $options{"sudo_username"}
MC/DC0%0/2
exists $instance->{"sudo_username"} and not exists $options{"sudo_username"}
exists $instance->{"sudo_username"}
not exists $options{"sudo_username"}
Truth table66%2/3
A and not(B)
A= exists $instance->{"sudo_username"}B= not exists $options{"sudo_username"}
| |||||||||||||||||||||||||||||||||
| 334 | 0 | $options{sudo_username} = $instance->{sudo_username}; | |||||||||||||||||||||||||||||||
| 335 | } | ||||||||||||||||||||||||||||||||
| 336 | 5 | ▶ | if ( exists( $instance->{pretty} ) && !exists( $options{pretty} ) ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
exists $instance->{"pretty"} and not exists $options{"pretty"}
MC/DC0%0/2
exists $instance->{"pretty"} and not exists $options{"pretty"}
exists $instance->{"pretty"}
not exists $options{"pretty"}
Truth table33%1/3
A and not(B)
A= exists $instance->{"pretty"}B= not exists $options{"pretty"}
| |||||||||||||||||||||||||||||||||
| 337 | 0 | $options{pretty} = $instance->{pretty}; | |||||||||||||||||||||||||||||||
| 338 | } | ||||||||||||||||||||||||||||||||
| 339 | 5 | return new( ref($instance), %options ); | |||||||||||||||||||||||||||||||
| 340 | } | ||||||||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||||||
| 342 | sub _init { | ||||||||||||||||||||||||||||||||
| 343 | 45 | ▶ | my ( $self, %options ) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
_init: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||||||
| 345 | 45 | ▶ | $self->{hostname} = $options{hostname} if ( defined( $options{hostname} ) ); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 346 | 45 | ▶ | $self->{ssh} = $options{ssh} if ( defined( $options{ssh} ) ); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 347 | 45 | ▶ | $self->{username} = $options{username} if ( defined( $options{username} ) ); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 348 | 45 | ▶ | $self->{sudo_username} = $options{sudo_username} if ( defined( $options{sudo_username} ) ); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 349 | 45 | ▶ | $self->{pretty} = $options{pretty} if ( defined( $options{pretty} ) ); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||||||
| 351 | 45 | return $self; | |||||||||||||||||||||||||||||||
| 352 | } | ||||||||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||||||
| 354 | 1 | 1; | |||||||||||||||||||||||||||||||
| 355 | |||||||||||||||||||||||||||||||||
| 356 | __END__ | ||||||||||||||||||||||||||||||||