class Wire::Execution::LocalExecution

Able to execute commands locally supports sudo and shell wrapping

Attributes

exitstatus[RW]

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

stderr[RW]

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

stdout[RW]

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

Public Class Methods

new(command, args = nil, options = { :b_shell => true, :b_sudo => true }) click to toggle source

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

# 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
with(command, args = nil, options = { :b_shell => true, :b_sudo => true }) { |obj| ... } click to toggle source

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

# 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

Public Instance Methods

construct_command() click to toggle source

constructs the single command line string from given parameters. Returns

  • Command line as [String]

# 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
run() click to toggle source

runs the command sets instance variables stdout, stderr, exitstatus

# 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

Private Instance Methods

array_or_nil_as_str(array_or_nil) click to toggle source

converts given array_or_nil object to identity if array or an empty array if nil

# File lib/wire/execution/local_exec.rb, line 105
def array_or_nil_as_str(array_or_nil)
  (array_or_nil || []).join(' ')
end