class Wire::Execution::LocalExecution
Able to execute commands locally supports sudo and shell wrapping
Attributes
exitstatus
the exit status of a command that we ran stdout
stdout from command as [String] stderr
stderr from command as [String]
exitstatus
the exit status of a command that we ran stdout
stdout from command as [String] stderr
stderr from command as [String]
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
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
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
# 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
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
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
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