Method: RSCM::CommandLine.execute
- Defined in:
- lib/rscm/command_line.rb
.execute(cmd, options = {}, &proc) ⇒ Object
Executes cmd. If the :stdout and :stderr options are specified, a line consisting of a prompt (including cmd) will be appended to the respective output streams will be appended to those files, followed by the output itself. Example:
CommandLine.execute("echo hello world", {:stdout => "stdout.log", :stderr => "stderr.log"})
will result in the following being written to stdout.log:
/Users/aslakhellesoy/scm/buildpatterns/repos/damagecontrol/trunk aslakhellesoy$ echo hello world
hello world
-and to stderr.log:
/Users/aslakhellesoy/scm/buildpatterns/repos/damagecontrol/trunk aslakhellesoy$ echo hello world
If a block is passed, the stdout io will be yielded to it (as with IO.popen). In this case the output will not be written to the stdout file (even if it’s specified):
/Users/aslakhellesoy/scm/buildpatterns/repos/damagecontrol/trunk aslakhellesoy$ echo hello world
[output captured and therefore not logged]
If the exitstatus of the command is different from the value specified by the :exitstatus option (which defaults to 0) then an ExecutionError is raised, its message containing the last 400 bytes of stderr (provided :stderr was specified)
You can also specify the :dir option, which will cause the command to be executed in that directory (default is current directory).
You can also specify a hash of environment variables in :env, which will add additional environment variables to the default environment.
Finally, you can specify several commands within one by separating them with ‘&&’ (as you would in a shell). This will result in several lines to be appended to the log (as if you had executed the commands separately).
See the unit test for more examples.
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/rscm/command_line.rb', line 57 def execute(cmd, ={}, &proc) raise "Can't have newline in cmd" if cmd =~ /\n/ = { :dir => Dir.pwd, :env => {}, :mode => 'r', :exitstatus => 0 }.merge() [:stdout] = File.([:stdout]) if [:stdout] [:stderr] = File.([:stderr]) if [:stderr] if [:dir].nil? e(cmd, , &proc) else Dir.chdir([:dir]) do e(cmd, , &proc) end end end |