Class: R::Command
Overview
A high level interface for executing commands.
Instance Attribute Summary collapse
-
#clearenv ⇒ true, false
If set, the executed command will not inherit environment variables from Rub.
-
#cmd ⇒ Object
readonly
Returns the value of attribute cmd.
-
#env ⇒ Object
readonly
Returns the value of attribute env.
- #mergeouts ⇒ true, false
-
#status ⇒ Object
readonly
Returns the value of attribute status.
-
#stderr ⇒ Object
readonly
Returns the value of attribute stderr.
-
#stdin ⇒ String
The string to use as input to the command.
-
#stdout ⇒ String
readonly
The output produced by the command.
Instance Method Summary collapse
-
#block ⇒ true, false
Wait for a command to finish.
-
#initialize(cmd) ⇒ Command
constructor
Create a new command to run.
-
#run ⇒ true, false
Run the command and block until completion.
-
#start ⇒ Object
Start the command.
-
#success? ⇒ true, false
Check if the command was successful.
Constructor Details
#initialize(cmd) ⇒ Command
Create a new command to run.
74 75 76 77 78 79 80 81 |
# File 'lib/rub/r/command.rb', line 74 def initialize(cmd) @env = {} @clearenv = false @mergeouts = false @cmd = cmd.map{|a| a.to_s} end |
Instance Attribute Details
#clearenv ⇒ true, false
If set, the executed command will not inherit environment variables from Rub. Only the values in #env will be present in the environment.
Defaults to false.
66 67 68 |
# File 'lib/rub/r/command.rb', line 66 def clearenv @clearenv end |
#cmd ⇒ Object (readonly)
Returns the value of attribute cmd.
29 30 31 |
# File 'lib/rub/r/command.rb', line 29 def cmd @cmd end |
#env ⇒ Object (readonly)
Returns the value of attribute env.
40 41 42 |
# File 'lib/rub/r/command.rb', line 40 def env @env end |
#mergeouts ⇒ true, false
69 70 71 |
# File 'lib/rub/r/command.rb', line 69 def mergeouts @mergeouts end |
#status ⇒ Object (readonly)
Returns the value of attribute status.
56 57 58 |
# File 'lib/rub/r/command.rb', line 56 def status @status end |
#stderr ⇒ Object (readonly)
Returns the value of attribute stderr.
49 |
# File 'lib/rub/r/command.rb', line 49 attr_reader :stdout, :stderr |
#stdin ⇒ String
Returns The string to use as input to the command.
44 45 46 |
# File 'lib/rub/r/command.rb', line 44 def stdin @stdin end |
#stdout ⇒ String (readonly)
Returns The output produced by the command.
49 50 51 |
# File 'lib/rub/r/command.rb', line 49 def stdout @stdout end |
Instance Method Details
#block ⇒ true, false
144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/rub/r/command.rb', line 144 def block @stdout = @stdoutr.read @stderr = @stderrr.read #puts "Blocking on #{@pid} #{@cmd.join' '}" pid, @status = Process.wait2 @pid #puts "Done #{@cmd.join' '}" @stdoutr.close @stderrr.close success? end |
#run ⇒ true, false
130 131 132 133 |
# File 'lib/rub/r/command.rb', line 130 def run start block end |
#start ⇒ Object
Calling this command a second time before #block returns produces undefined behaviour. A R::Command can be run multiple times but it must finish before being run again.
Start the command.
Executes the command. The command will run in the background. If you want to wait for the command to complete call #block.
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/rub/r/command.rb', line 91 def start @status = nil @stdinr, @stdinw = IO.pipe @stdoutr, @stdoutw = IO.pipe @stderrr, @stderrw = IO.pipe @stdout = "" @stderr = "" @status = nil args = [ @env, *@cmd.map{|a| a.to_s}, :unsetenv_others=>@clearenv, :in =>@stdinr, :out=>@stdoutw, :err=>(@mergeouts?@stdoutw:@stderrw), ] #p "args: #{args}" @pid = spawn(*args) @stdinr.close @stdinw.write @stdin @stdinw.close @stdoutw.close @stderrw.close @pid end |
#success? ⇒ true, false
If the command has not been executed or has not completed this returns false.
Check if the command was successful
164 165 166 |
# File 'lib/rub/r/command.rb', line 164 def success? !!( @status and @status.exitstatus == 0 ) end |