File Coverage

lib/Rex/Interface/Exec/Base.pm
Criterion Covered Total %
statement 38 79 48.1
branch 5 20 25.0
condition 6 18 33.3
subroutine 9 14 64.2
pod 0 6 0.0
total 58 137 42.3


line stmt bran cond sub pod time code
1             #
2             # (c) Jan Gehring
3             #
4              
5             package Rex::Interface::Exec::Base;
6              
7 58     58   882 use v5.12.5;
  58         278  
8 58     58   376 use warnings;
  58         208  
  58         1711  
9 58     58   425 use Carp;
  58         220  
  58         4491  
10 58     58   447 use Rex::Helper::Run;
  58         204  
  58         7728  
11 58     58   604 use Rex::Commands::Fs;
  58         929  
  58         851  
12              
13             our $VERSION = '1.14.2.3'; # TRIAL VERSION
14              
15             sub new {
16 0     0 0 0 my $that = shift;
17 0   0     0 my $proto = ref($that) || $that;
18 0         0 my $self = {@_};
19              
20 0         0 bless( $self, $proto );
21              
22 0         0 return $self;
23             }
24              
25 0     0 0 0 sub exec { die("Must be implemented by Interface Class"); }
26              
27             sub _continuous_read {
28 5995     5995   12897 my ( $self, $line, $option ) = @_;
29 5995   50     25210 my $cb = $option->{continuous_read} || undef;
30              
31 5995 50 33     16180 if ( defined($cb) && ref($cb) eq 'CODE' ) {
32 0         0 &$cb($line);
33             }
34             }
35              
36             sub _end_if_matched {
37 5995     5995   12156 my ( $self, $line, $option ) = @_;
38 5995   50     21424 my $regex = $option->{end_if_matched} || undef;
39              
40 5995 0 33     13654 if ( defined($regex) && ref($regex) eq 'Regexp' && $line =~ m/$regex/ ) {
      33        
41 0         0 return 1;
42             }
43 5995         168767 return;
44             }
45              
46             sub execute_line_based_operation {
47 5995     5995 0 20335 my ( $self, $line, $option ) = @_;
48              
49 5995         19821 $self->_continuous_read( $line, $option );
50 5995         12726 return $self->_end_if_matched( $line, $option );
51             }
52              
53             sub can_run {
54 555     555 0 2244 my ( $self, $commands_to_check, $check_with_command ) = @_;
55              
56 555   50     3086 $check_with_command ||= "which";
57              
58 555         2440 my $exec = Rex::Interface::Exec->create;
59 555         3750 my $cache = Rex::get_cache();
60              
61 555         1305 for my $command ( @{$commands_to_check} ) {
  555         2769  
62              
63 556         6625 my $cache_key_name = $cache->gen_key_name("can_run.cmd/$command");
64 556 50       3783 if ( $cache->valid($cache_key_name) ) {
65 0         0 return $cache->get($cache_key_name);
66             }
67              
68 556         5295 my $output = Rex::Helper::Run::i_run "$check_with_command $command",
69             fail_ok => 1;
70              
71 556 100       7697 next if ( $? != 0 );
72              
73 212 50       4434 next if ( !is_file($output) );
74              
75 212         4019 $cache->set( $cache_key_name, $output );
76              
77 212         7335 return $output;
78             }
79              
80 343         16170 return undef;
81             }
82              
83             sub direct_exec {
84 0     0 0   my ( $self, $exec, $option ) = @_;
85              
86 0           Rex::Commands::profiler()->start("direct_exec: $exec");
87              
88 0           my $class_name = ref $self;
89              
90 0           Rex::Logger::debug("$class_name/executing: $exec");
91 0           my ( $out, $err ) = $self->_exec( $exec, $option );
92              
93 0           Rex::Commands::profiler()->end("direct_exec: $exec");
94              
95 0 0         Rex::Logger::debug($out) if ($out);
96 0 0         if ($err) {
97 0           Rex::Logger::debug("========= ERR ============");
98 0           Rex::Logger::debug($err);
99 0           Rex::Logger::debug("========= ERR ============");
100             }
101              
102 0 0         if (wantarray) { return ( $out, $err ); }
  0            
103              
104 0           return $out;
105             }
106              
107             sub _exec {
108 0     0     my ($self) = @_;
109 0           my $class_name = ref $self;
110 0           die("_exec method must be overwritten by class ($class_name).");
111             }
112              
113             sub shell {
114 0     0 0   my ($self) = @_;
115              
116 0           Rex::Logger::debug("Detecting shell...");
117              
118 0           my $cache = Rex::get_cache();
119 0 0         if ( $cache->valid("shell") ) {
120 0           Rex::Logger::debug( "Found shell in cache: " . $cache->get("shell") );
121 0           return Rex::Interface::Shell->create( $cache->get("shell") );
122             }
123              
124 0           my %shells = Rex::Interface::Shell->get_shell_provider;
125 0           for my $shell ( keys %shells ) {
126 0           Rex::Logger::debug( "Searching for shell: " . $shell );
127 0           $shells{$shell}->require;
128 0 0         if ( $shells{$shell}->detect($self) ) {
129 0           Rex::Logger::debug( "Found shell and using: " . $shell );
130 0           $cache->set( "shell", $shell );
131 0           return Rex::Interface::Shell->create($shell);
132             }
133             }
134              
135 0           return Rex::Interface::Shell->create("sh");
136             }
137              
138             1;