Class: Arver::CommandWrapper

Inherits:
Object
  • Object
show all
Defined in:
lib/arver/command_wrapper.rb

Direct Known Subclasses

SSHCommandWrapper

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#arguments_arrayObject

Returns the value of attribute arguments_array.



3
4
5
# File 'lib/arver/command_wrapper.rb', line 3

def arguments_array
  @arguments_array
end

#commandObject

Returns the value of attribute command.



3
4
5
# File 'lib/arver/command_wrapper.rb', line 3

def command
  @command
end

#outputObject

Returns the value of attribute output.



3
4
5
# File 'lib/arver/command_wrapper.rb', line 3

def output
  @output
end

#return_valueObject

Returns the value of attribute return_value.



3
4
5
# File 'lib/arver/command_wrapper.rb', line 3

def return_value
  @return_value
end

Class Method Details

.create(cmd, args = []) ⇒ Object



5
6
7
8
9
10
# File 'lib/arver/command_wrapper.rb', line 5

def self.create( cmd, args = [] )
  c = CommandWrapper.new
  c.command= cmd
  c.arguments_array= args
  c
end

.shellescape(str) ⇒ Object

copy from shellwords.rb



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/arver/command_wrapper.rb', line 17

def self.shellescape(str)
  str = str.to_s

  # An empty argument will be skipped, so return empty quotes.
  return "''" if str.empty?

  str = str.dup

  # Treat multibyte characters as is.  It is caller's responsibility
  # to encode the string in the right encoding for the shell
  # environment.
  str.gsub!(/([^A-Za-z0-9_\-.,:\/@\n])/, "\\\\\\1")

  # A LF cannot be escaped with a backslash because a backslash + LF
  # combo is regarded as line continuation and simply ignored.
  str.gsub(/\n/, "'\n'")
end

Instance Method Details

#escaped_commandObject



35
36
37
# File 'lib/arver/command_wrapper.rb', line 35

def escaped_command
  "#{shellescape(command)} " + arguments_array.collect{|c| shellescape(c)}.join(" ")
end

#execute(input = "") ⇒ Object



39
40
41
42
# File 'lib/arver/command_wrapper.rb', line 39

def execute( input = "" )
  Arver::Log.trace( "** Execute: "+self.escaped_command )
  self.run( escaped_command, input )
end

#run(command, input) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/arver/command_wrapper.rb', line 49

def run( command, input )
  if( Arver::RuntimeConfig.instance.test_mode || Arver::RuntimeConfig.instance.dry_run )
    self.output= ""
    self.return_value= 0
  else
    IO.popen( command, "w+") do |pipe|
      pipe.puts( input ) unless input.empty?
      pipe.close_write
      self.output= pipe.read
    end
    self.return_value= $?.exitstatus
  end
  self.success?
end

#shellescape(str) ⇒ Object



12
13
14
# File 'lib/arver/command_wrapper.rb', line 12

def shellescape(str)
  CommandWrapper.shellescape(str)
end

#success?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/arver/command_wrapper.rb', line 45

def success?
  return_value == 0
end