• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

RexOps / Rex / 12855644858

19 Jan 2025 05:20PM UTC coverage: 50.654% (+0.05%) from 50.608%
12855644858

push

github

ferki
WIP: alternative quoting

1 of 1 new or added line in 1 file covered. (100.0%)

13 existing lines in 1 file now uncovered.

9954 of 19651 relevant lines covered (50.65%)

98.99 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

65.22
/lib/Rex/Interface/Exec/Base.pm
1
#
2
# (c) Jan Gehring <jan.gehring@gmail.com>
3
#
4

5
package Rex::Interface::Exec::Base;
6

7
use v5.12.5;
68✔
8
use warnings;
68✔
9
use Carp;
68✔
10
use Rex::Helper::Run;
68✔
11
use Rex::Commands::Fs;
68✔
12
use Rex::Interface::Shell;
68✔
13

14
our $VERSION = '9999.99.99_99'; # VERSION
15

16
sub new {
17
  my $that  = shift;
×
18
  my $proto = ref($that) || $that;
×
UNCOV
19
  my $self  = {@_};
×
20

UNCOV
21
  bless( $self, $proto );
×
22

UNCOV
23
  return $self;
×
24
}
25

UNCOV
26
sub exec { die("Must be implemented by Interface Class"); }
×
27

28
sub _continuous_read {
29
  my ( $self, $line, $option ) = @_;
4,440✔
30
  my $cb = $option->{continuous_read} || undef;
4,440✔
31

32
  if ( defined($cb) && ref($cb) eq 'CODE' ) {
4,440✔
UNCOV
33
    &$cb($line);
×
34
  }
35
}
36

37
sub _end_if_matched {
38
  my ( $self, $line, $option ) = @_;
4,440✔
39
  my $regex = $option->{end_if_matched} || undef;
4,440✔
40

41
  if ( defined($regex) && ref($regex) eq 'Regexp' && $line =~ m/$regex/ ) {
4,440✔
UNCOV
42
    return 1;
×
43
  }
44
  return;
4,440✔
45
}
46

47
sub execute_line_based_operation {
48
  my ( $self, $line, $option ) = @_;
4,440✔
49

50
  $self->_continuous_read( $line, $option );
4,440✔
51
  return $self->_end_if_matched( $line, $option );
4,440✔
52
}
53

54
sub can_run {
55
  my ( $self, $commands_to_check, $check_with_command ) = @_;
586✔
56

57
  $check_with_command ||= "which";
586✔
58

59
  my $exec  = Rex::Interface::Exec->create;
586✔
60
  my $cache = Rex::get_cache();
586✔
61

62
  for my $command ( @{$commands_to_check} ) {
586✔
63

64
    my $cache_key_name = $cache->gen_key_name("can_run.cmd/$command");
587✔
65
    if ( $cache->valid($cache_key_name) ) {
587✔
66
      return $cache->get($cache_key_name);
4✔
67
    }
68

69
    my @output = Rex::Helper::Run::i_run "$check_with_command $command",
583✔
70
      fail_ok => 1;
71

72
    next if ( $? != 0 );
583✔
73

74
    next if ( !is_file( $output[0] ) );
569✔
75

76
    $cache->set( $cache_key_name, $output[0] );
569✔
77

78
    return $output[0];
569✔
79
  }
80

81
  return undef;
13✔
82
}
83

84
sub direct_exec {
85
  my ( $self, $exec, $option ) = @_;
12✔
86

87
  Rex::Commands::profiler()->start("direct_exec: $exec");
12✔
88

89
  my $class_name = ref $self;
12✔
90

91
  Rex::Logger::debug("$class_name/executing: $exec");
12✔
92
  my ( $out, $err ) = $self->_exec( $exec, $option );
12✔
93

UNCOV
94
  Rex::Commands::profiler()->end("direct_exec: $exec");
×
95

96
  Rex::Logger::debug($out) if ($out);
×
97
  if ($err) {
×
98
    Rex::Logger::debug("========= ERR ============");
×
99
    Rex::Logger::debug($err);
×
UNCOV
100
    Rex::Logger::debug("========= ERR ============");
×
101
  }
102

UNCOV
103
  if (wantarray) { return ( $out, $err ); }
×
104

UNCOV
105
  return $out;
×
106
}
107

108
sub _exec {
109
  my ($self) = @_;
×
110
  my $class_name = ref $self;
×
UNCOV
111
  die("_exec method must be overwritten by class ($class_name).");
×
112
}
113

114
sub shell {
115
  my ($self) = @_;
12✔
116

117
  Rex::Logger::debug("Detecting shell...");
12✔
118

119
  my $cache = Rex::get_cache();
12✔
120
  if ( $cache->valid("shell") ) {
12✔
121
    Rex::Logger::debug( "Found shell in cache: " . $cache->get("shell") );
×
UNCOV
122
    return Rex::Interface::Shell->create( $cache->get("shell") );
×
123
  }
124

125
  my %shells = Rex::Interface::Shell->get_shell_provider;
12✔
126
  for my $shell ( keys %shells ) {
12✔
127
    Rex::Logger::debug( "Searching for shell: " . $shell );
96✔
128
    $shells{$shell}->require;
96✔
129
    if ( $shells{$shell}->detect($self) ) {
96✔
130
      Rex::Logger::debug( "Found shell and using: " . $shell );
×
131
      $cache->set( "shell", $shell );
×
UNCOV
132
      return Rex::Interface::Shell->create($shell);
×
133
    }
134
  }
135

136
  return Rex::Interface::Shell->create("sh");
12✔
137
}
138

139
1;
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc