Class: Wire::Execution::LocalExecution

Inherits:
Object
  • Object
show all
Defined in:
lib/wire/execution/local_exec.rb

Overview

Able to execute commands locally supports sudo and shell wrapping

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(command, args = nil, options = { :b_shell => true, :b_sudo => true }) ⇒ LocalExecution

params:

  • command: binary to execute

  • args: optional cmd line arguments (exec array)

  • options:

    • b_shell: if true, run as /bin/sh -c ‘<command> [args]’

    • b_sudo: insert sudo if true



48
49
50
51
52
53
# File 'lib/wire/execution/local_exec.rb', line 48

def initialize(command, args = nil, options =
    { :b_shell => true, :b_sudo => true })
  @command = command
  @args = array_or_nil_as_str(args)
  @options = options
end

Instance Attribute Details

#exitstatusObject

exitstatus the exit status of a command that we ran stdout stdout from command as [String] stderr stderr from command as [String]



40
41
42
# File 'lib/wire/execution/local_exec.rb', line 40

def exitstatus
  @exitstatus
end

#stderrObject

exitstatus the exit status of a command that we ran stdout stdout from command as [String] stderr stderr from command as [String]



40
41
42
# File 'lib/wire/execution/local_exec.rb', line 40

def stderr
  @stderr
end

#stdoutObject

exitstatus the exit status of a command that we ran stdout stdout from command as [String] stderr stderr from command as [String]



40
41
42
# File 'lib/wire/execution/local_exec.rb', line 40

def stdout
  @stdout
end

Class Method Details

.with(command, args = nil, options = { :b_shell => true, :b_sudo => true }) {|obj| ... } ⇒ Object

block-style. Creates a LocalExecution object with given parameters, yields it into a given block. Params: command Command string, usually the binary args argument array options option map (b_shell, b_sudo flags) Yields

  • LocalExecution object

Yields:

  • (obj)


63
64
65
66
67
# File 'lib/wire/execution/local_exec.rb', line 63

def self.with(command, args = nil, options =
    { :b_shell => true, :b_sudo => true })
  obj = LocalExecution.new command, args, options
  yield obj
end

Instance Method Details

#construct_commandObject

constructs the single command line string from given parameters. Returns

  • Command line as [String]



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/wire/execution/local_exec.rb', line 73

def construct_command
  cmd_arr = []
  command_args = "#{@command} #{@args}".strip
  sudo_str = (@options[:b_sudo] ? 'sudo ' : '')
  if @options[:b_shell]
    cmd_arr << '/bin/sh'
    cmd_arr << '-c'

    cmd_arr << "'#{sudo_str}#{command_args}'"
  else
    cmd_arr << "#{sudo_str}#{command_args}"
  end

  cmd_arr.join(' ').strip
end

#runObject

runs the command sets instance variables stdout, stderr, exitstatus



92
93
94
95
96
97
98
99
# File 'lib/wire/execution/local_exec.rb', line 92

def run
  cmd = construct_command

  $log.debug "Executing #{cmd}"
  @stdout = `#{cmd}`
  @stderr = nil
  @exitstatus = $CHILD_STATUS.exitstatus
end