Module: Rtasklib::Execute
Overview
How to execute shell commands and capture output
Constant Summary collapse
- DEBUG =
false
Instance Method Summary collapse
-
#each_popen3(program = 'task', *opts, &block) {|l, i, o, e, t| ... } ⇒ Object
Same as Execute#popen3, but yields each line of input.
-
#handle_response(stdout, stderr, thread) ⇒ Object
Default error handling called in every popen3 call.
-
#popen3(program = 'task', *opts, &block) {|i, o, e, t| ... } ⇒ Object
Use Open3#popen3 to execute a unix program with an array of options and an optional block to handle the response.
-
#task_each_popen3(*opts, &block) {|l, i, o, e, t| ... } ⇒ Object
Same as Execute#each_popen3, but calls it with the ‘task’ program.
-
#task_popen3(*opts, &block) {|i, o, e, t| ... } ⇒ Object
Same as Execute#popen3, only defaults to using the ‘task’ program for convenience.
Instance Method Details
#each_popen3(program = 'task', *opts, &block) {|l, i, o, e, t| ... } ⇒ Object
Same as Execute#popen3, but yields each line of input
69 70 71 72 73 74 75 |
# File 'lib/rtasklib/execute.rb', line 69 def each_popen3 program='task', *opts, &block popen3(program, *opts) do |i, o, e, t| o.each_line do |l| yield(l, i, o, e, t) end end end |
#handle_response(stdout, stderr, thread) ⇒ Object
Default error handling called in every popen3 call. Only executes if thread had a failing exit code
94 95 96 97 98 99 |
# File 'lib/rtasklib/execute.rb', line 94 def handle_response stdout, stderr, thread unless thread.value.success? dump = "#{thread.value} \n Stderr: #{stderr.read} \n Stdout: #{stdout.read} \n" raise dump end end |
#popen3(program = 'task', *opts, &block) {|i, o, e, t| ... } ⇒ Object
Use Open3#popen3 to execute a unix program with an array of options and an optional block to handle the response.
34 35 36 37 38 39 40 41 42 43 |
# File 'lib/rtasklib/execute.rb', line 34 def popen3 program='task', *opts, &block execute = opts.unshift(program) execute = execute.join(" ") warn execute if DEBUG Open3.popen3(execute) do |i, o, e, t| handle_response(o, e, t) yield(i, o, e, t) if block_given? end end |
#task_each_popen3(*opts, &block) {|l, i, o, e, t| ... } ⇒ Object
Same as Execute#each_popen3, but calls it with the ‘task’ program
84 85 86 87 88 |
# File 'lib/rtasklib/execute.rb', line 84 def task_each_popen3 *opts, &block each_popen3("task", *opts) do |l, i, o, e, t| yield(l, i, o, e, t) end end |
#task_popen3(*opts, &block) {|i, o, e, t| ... } ⇒ Object
Same as Execute#popen3, only defaults to using the ‘task’ program for convenience.
57 58 59 |
# File 'lib/rtasklib/execute.rb', line 57 def task_popen3 *opts, &block popen3('task', opts, &block) end |