Module: Rtasklib::Execute

Extended by:
Execute
Included in:
Execute
Defined in:
lib/rtasklib/execute.rb

Overview

How to execute shell commands and capture output

Constant Summary

DEBUG =

When true writes all shell commands to STDERR

false

Instance Method Summary (collapse)

Instance Method Details

- (Object) each_popen3(program = 'task', *opts, &block) {|l, i, o, e, t| ... }

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

Parameters:

  • program (String) (defaults to: 'task')
  • opts (Array<String>)

    args to pass directly to the program

  • block (Block)

    to execute after thread is successful

Yields:

  • (l, i, o, e, t)

    a line of STDIN, STDIN, STDOUT, STDERR, and the thread to that block.



66
67
68
69
70
71
72
# 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

- (Object) handle_response(stdout, stderr, thread)

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

Raises:

  • (RuntimeError)

    if failing exit code



91
92
93
94
95
96
# 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

- (Object) popen3(program = 'task', *opts, &block) {|i, o, e, t| ... }

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

Examples:

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

Parameters:

  • program (String) (defaults to: 'task')
  • opts (Array<String>)

    args to pass directly to the program

  • block (Block)

    to execute after thread is successful

Yields:

  • (i, o, e, t)

    STDIN, STDOUT, STDERR, and the thread to that block.



31
32
33
34
35
36
37
38
39
40
# 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

- (Object) task_each_popen3(*opts, &block) {|l, i, o, e, t| ... }

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

Parameters:

  • opts (Array<String>)

    args to pass directly to the program

  • block (Block)

    to execute after thread is successful

Yields:

  • (l, i, o, e, t)

    a line of STDIN, STDIN, STDOUT, STDERR, and the thread to that block.



81
82
83
84
85
# 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

- (Object) task_popen3(*opts, &block) {|i, o, e, t| ... }

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

Examples:

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

Parameters:

  • opts (Array<String>)

    args to pass directly to the program

  • block (Block)

    to execute after thread is successful

Yields:

  • (i, o, e, t)

    STDIN, STDOUT, STDERR, and the thread to that block.



54
55
56
# File 'lib/rtasklib/execute.rb', line 54

def task_popen3 *opts, &block
  popen3('task', opts, &block)
end