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

ferki / Rex / 15240232617

28 Feb 2025 09:08AM UTC coverage: 50.961% (+10.2%) from 40.73%
15240232617

push

github

ferki
Use placeholders for template hints

10072 of 19764 relevant lines covered (50.96%)

101.57 hits per line

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

20.37
/lib/Rex/Interface/Shell/Bash.pm
1
#
2
# (c) Jan Gehring <jan.gehring@gmail.com>
3
#
4

5
package Rex::Interface::Shell::Bash;
6

7
use v5.12.5;
4✔
8
use warnings;
4✔
9

10
our $VERSION = '9999.99.99_99'; # VERSION
11

12
use Rex::Interface::Shell::Base;
4✔
13
use base qw(Rex::Interface::Shell::Base);
4✔
14
use Data::Dumper;
4✔
15
use Net::OpenSSH::ShellQuoter;
4✔
16

17
sub new {
18
  my $class = shift;
14✔
19
  my $proto = ref($class) || $class;
14✔
20
  my $self  = $proto->SUPER::new(@_);
14✔
21

22
  bless( $self, $class );
14✔
23

24
  return $self;
14✔
25
}
26

27
sub path {
28
  my ( $self, $path ) = @_;
×
29
  $self->{path} = $path;
×
30
}
31

32
sub source_global_profile {
33
  my ( $self, $parse ) = @_;
×
34
  $self->{source_global_profile} = $parse;
×
35
}
36

37
sub source_profile {
38
  my ( $self, $parse ) = @_;
×
39
  $self->{source_profile} = $parse;
×
40
}
41

42
# sub set_env {
43
#   my ( $self, $env ) = @_;
44
#   my $cmd = undef;
45
#
46
#   die("Error: env must be a hash")
47
#     if ( ref $env ne "HASH" );
48
#
49
#   while ( my ( $k, $v ) = each($env) ) {
50
#     $cmd .= "export $k=$v; ";
51
#   }
52
#   $self->{env} = $cmd;
53
# }
54

55
sub exec {
56
  my ( $self, $cmd, $option ) = @_;
×
57

58
  if ( exists $option->{path} ) {
×
59
    $self->path( $option->{path} );
×
60
  }
61

62
  Rex::Logger::debug("Shell/Bash: Got options:");
×
63
  Rex::Logger::debug( Dumper($option) );
×
64

65
  my $complete_cmd = $cmd;
×
66

67
  # if we need to execute without an sh command,
68
  # use the format_cmd key
69
  # if ( exists $option->{no_sh} ) {
70
  #   $complete_cmd = $option->{format_cmd};
71
  # }
72

73
  if ( exists $option->{cwd} ) {
×
74
    $complete_cmd = "cd $option->{cwd} && $complete_cmd";
×
75
  }
76

77
  if ( $self->{path} && !exists $self->{__env__}->{PATH} ) {
×
78
    $complete_cmd = "PATH=$self->{path}; export PATH; $complete_cmd ";
×
79
  }
80

81
  if ( $self->{locale} && !exists $option->{no_locales} ) {
×
82
    $complete_cmd = "LC_ALL=$self->{locale} ; export LC_ALL; $complete_cmd ";
×
83
  }
84

85
  if ( $self->{source_profile} ) {
×
86
    $complete_cmd =
×
87
      "[ -r ~/.profile ] && . ~/.profile >/dev/null 2>&1 ; $complete_cmd";
88
  }
89

90
  if ( $self->{source_global_profile} ) {
×
91
    $complete_cmd = ". /etc/profile >/dev/null 2>&1 ; $complete_cmd";
×
92
  }
93

94
  if ( exists $self->{__env__} ) {
×
95
    if ( ref $self->{__env__} eq "HASH" ) {
×
96
      for my $key ( keys %{ $self->{__env__} } ) {
×
97
        my $val = $self->{__env__}->{$key};
×
98
        $val =~ s/"/\\"/gms;
×
99
        if ( exists $self->{__sudo_env__} && $self->{__sudo_env__} ) {
×
100
          $complete_cmd = " $key=\"$val\" $complete_cmd ";
×
101
        }
102
        else {
103
          $complete_cmd = " export $key=\"$val\" ; $complete_cmd ";
×
104
        }
105
      }
106
    }
107
    else {
108
      $complete_cmd = $self->{__env__} . " $complete_cmd ";
×
109
    }
110
  }
111

112
# this is due to a strange behaviour with Net::SSH2 / libssh2
113
# it may occur when you run rex inside a kvm virtualized host connecting to another virtualized vm on the same hardware
114
  if ( Rex::Config->get_sleep_hack ) {
×
115
    $complete_cmd .= " ; f=\$? ; sleep .00000001 ; exit \$f";
×
116
  }
117

118
  if ( exists $option->{preprocess_command}
×
119
    && ref $option->{preprocess_command} eq "CODE" )
120
  {
121
    $complete_cmd = $option->{preprocess_command}->($complete_cmd);
×
122
  }
123

124
  # rewrite the command again
125
  if ( exists $option->{format_cmd} ) {
×
126
    $complete_cmd =~ s/\{\{CMD\}\}/$cmd/;
×
127
  }
128

129
  if ( $self->{__inner_shell__} ) {
×
130
    my $quoter = Net::OpenSSH::ShellQuoter->quoter("sh");
×
131
    $complete_cmd = "sh -c " . $quoter->quote($complete_cmd);
×
132
  }
133

134
  if ( exists $option->{prepend_command} && $option->{prepend_command} ) {
×
135
    $complete_cmd = $option->{prepend_command} . " $complete_cmd";
×
136
  }
137

138
  return $complete_cmd;
×
139
}
140

141
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