Class: Wire::Execution::LocalExecution
- Inherits:
-
Object
- Object
- Wire::Execution::LocalExecution
- Defined in:
- lib/wire/execution/local_exec.rb
Overview
Able to execute commands locally supports sudo and shell wrapping
Instance Attribute Summary collapse
-
#exitstatus ⇒ Object
exitstatusthe exit status of a command that we ranstdoutstdout from command as [String]stderrstderr from command as [String]. -
#stderr ⇒ Object
exitstatusthe exit status of a command that we ranstdoutstdout from command as [String]stderrstderr from command as [String]. -
#stdout ⇒ Object
exitstatusthe exit status of a command that we ranstdoutstdout from command as [String]stderrstderr from command as [String].
Class Method Summary collapse
-
.with(command, args = nil, options = { :b_shell => true, :b_sudo => true }) {|obj| ... } ⇒ Object
block-style.
Instance Method Summary collapse
-
#construct_command ⇒ Object
constructs the single command line string from given parameters.
-
#initialize(command, args = nil, options = { :b_shell => true, :b_sudo => true }) ⇒ LocalExecution
constructor
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.
-
#run ⇒ Object
runs the command sets instance variables stdout, stderr, exitstatus.
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, = { :b_shell => true, :b_sudo => true }) @command = command @args = array_or_nil_as_str(args) @options = end |
Instance Attribute Details
#exitstatus ⇒ Object
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 |
#stderr ⇒ Object
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 |
#stdout ⇒ Object
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
63 64 65 66 67 |
# File 'lib/wire/execution/local_exec.rb', line 63 def self.with(command, args = nil, = { :b_shell => true, :b_sudo => true }) obj = LocalExecution.new command, args, yield obj end |
Instance Method Details
#construct_command ⇒ Object
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 |
#run ⇒ Object
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 |