module Rtasklib::Execute

How to execute shell commands and capture output

Constants

DEBUG

When true writes all shell commands to STDERR

Public Instance Methods

each_popen3(program='task', *opts) { |l, i, o, e, t| ... } click to toggle source

Same as Execute#popen3, but yields each line of input

@param program [String] @param opts [Array<String>] args to pass directly to the program @param block [Block] to execute after thread is successful @yield [l,i,o,e,t] a line of STDIN, STDIN, STDOUT, STDERR,

and the thread to that block.

@api public

# File lib/rtasklib/execute.rb, line 66
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) click to toggle source

Default error handling called in every popen3 call. Only executes if thread had a failing exit code

@raise [RuntimeError] if failing exit code

# File lib/rtasklib/execute.rb, line 91
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) { |i, o, e, t| ... } click to toggle source

Use Open3#popen3 to execute a unix program with an array of options and an optional block to handle the response.

@example

Execute.popen3("task", "export") do |i, o, e, t|
  # Arbitrary code to handle the response...
end

@param program [String] @param opts [Array<String>] args to pass directly to the program @param block [Block] to execute after thread is successful @yield [i,o,e,t] STDIN, STDOUT, STDERR, and the thread to that block. @api public

# File lib/rtasklib/execute.rb, line 31
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) { |l, i, o, e, t| ... } click to toggle source

Same as Execute#each_popen3, but calls it with the 'task' program

@param opts [Array<String>] args to pass directly to the program @param block [Block] to execute after thread is successful @yield [l,i,o,e,t] a line of STDIN, STDIN, STDOUT, STDERR,

and the thread to that block.

@api public

# File lib/rtasklib/execute.rb, line 81
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) click to toggle source

Same as Execute#popen3, only defaults to using the 'task' program for convenience.

@example

Execute.task_popen3("export") do |i, o, e, t|
  # Arbitrary code to handle the response...
end

@param opts [Array<String>] args to pass directly to the program @param block [Block] to execute after thread is successful @yield [i,o,e,t] STDIN, STDOUT, STDERR, and the thread to that block. @api public

# File lib/rtasklib/execute.rb, line 54
def task_popen3 *opts, &block
  popen3('task', opts, &block)
end