File Coverage

lib/Rex/Interface/Exec/Base.pm
Criterion Covered Total %
statement 36 77 46.7
branch 5 20 25.0
condition 6 18 33.3
subroutine 8 13 61.5
pod 0 6 0.0
total 55 134 41.0


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   859 use v5.12.5;
  58         277  
8 58     58   408 use warnings;
  58         537  
  58         1698  
9 58     58   384 use Carp;
  58         180  
  58         4826  
10 58     58   540 use Rex::Helper::Run;
  58         183  
  58         62281  
11              
12             our $VERSION = '1.14.2.2'; # TRIAL VERSION
13              
14             sub new {
15 0     0 0 0 my $that = shift;
16 0   0     0 my $proto = ref($that) || $that;
17 0         0 my $self = {@_};
18              
19 0         0 bless( $self, $proto );
20              
21 0         0 return $self;
22             }
23              
24 0     0 0 0 sub exec { die("Must be implemented by Interface Class"); }
25              
26             sub _continuous_read {
27 5996     5996   12412 my ( $self, $line, $option ) = @_;
28 5996   50     25587 my $cb = $option->{continuous_read} || undef;
29              
30 5996 50 33     15993 if ( defined($cb) && ref($cb) eq 'CODE' ) {
31 0         0 &$cb($line);
32             }
33             }
34              
35             sub _end_if_matched {
36 5996     5996   12356 my ( $self, $line, $option ) = @_;
37 5996   50     20921 my $regex = $option->{end_if_matched} || undef;
38              
39 5996 0 33     13988 if ( defined($regex) && ref($regex) eq 'Regexp' && $line =~ m/$regex/ ) {
      33        
40 0         0 return 1;
41             }
42 5996         192772 return;
43             }
44              
45             sub execute_line_based_operation {
46 5996     5996 0 19090 my ( $self, $line, $option ) = @_;
47              
48 5996         18493 $self->_continuous_read( $line, $option );
49 5996         13208 return $self->_end_if_matched( $line, $option );
50             }
51              
52             sub can_run {
53 555     555 0 2291 my ( $self, $commands_to_check, $check_with_command ) = @_;
54              
55 555   50     1888 $check_with_command ||= "which";
56              
57 555         2405 my $exec = Rex::Interface::Exec->create;
58 555         3640 my $cache = Rex::get_cache();
59              
60 555         1236 for my $command ( @{$commands_to_check} ) {
  555         2946  
61              
62 556         7501 my $cache_key_name = $cache->gen_key_name("can_run.cmd/$command");
63 556 50       4061 if ( $cache->valid($cache_key_name) ) {
64 0         0 return $cache->get($cache_key_name);
65             }
66              
67 556         5929 my @output = Rex::Helper::Run::i_run "$check_with_command $command",
68             fail_ok => 1;
69              
70 556 100       8321 next if ( $? != 0 );
71 212 50       1602 next if ( grep { /^no $command in/ } @output ); # for solaris
  212         11650  
72              
73 212         4623 $cache->set( $cache_key_name, $output[0] );
74              
75 212         8953 return $output[0];
76             }
77              
78 343         15917 return undef;
79             }
80              
81             sub direct_exec {
82 0     0 0   my ( $self, $exec, $option ) = @_;
83              
84 0           Rex::Commands::profiler()->start("direct_exec: $exec");
85              
86 0           my $class_name = ref $self;
87              
88 0           Rex::Logger::debug("$class_name/executing: $exec");
89 0           my ( $out, $err ) = $self->_exec( $exec, $option );
90              
91 0           Rex::Commands::profiler()->end("direct_exec: $exec");
92              
93 0 0         Rex::Logger::debug($out) if ($out);
94 0 0         if ($err) {
95 0           Rex::Logger::debug("========= ERR ============");
96 0           Rex::Logger::debug($err);
97 0           Rex::Logger::debug("========= ERR ============");
98             }
99              
100 0 0         if (wantarray) { return ( $out, $err ); }
  0            
101              
102 0           return $out;
103             }
104              
105             sub _exec {
106 0     0     my ($self) = @_;
107 0           my $class_name = ref $self;
108 0           die("_exec method must be overwritten by class ($class_name).");
109             }
110              
111             sub shell {
112 0     0 0   my ($self) = @_;
113              
114 0           Rex::Logger::debug("Detecting shell...");
115              
116 0           my $cache = Rex::get_cache();
117 0 0         if ( $cache->valid("shell") ) {
118 0           Rex::Logger::debug( "Found shell in cache: " . $cache->get("shell") );
119 0           return Rex::Interface::Shell->create( $cache->get("shell") );
120             }
121              
122 0           my %shells = Rex::Interface::Shell->get_shell_provider;
123 0           for my $shell ( keys %shells ) {
124 0           Rex::Logger::debug( "Searching for shell: " . $shell );
125 0           $shells{$shell}->require;
126 0 0         if ( $shells{$shell}->detect($self) ) {
127 0           Rex::Logger::debug( "Found shell and using: " . $shell );
128 0           $cache->set( "shell", $shell );
129 0           return Rex::Interface::Shell->create($shell);
130             }
131             }
132              
133 0           return Rex::Interface::Shell->create("sh");
134             }
135              
136             1;