Module: Tetra::ProcessRunner

Includes:
Logging
Included in:
Bash, Git, Tar, Unzip
Defined in:
lib/tetra/facades/process_runner.rb

Overview

runs programs in subprocesses

Instance Method Summary collapse

Methods included from Logging

#log

Instance Method Details

#run(commandline, echo: false, stdin_data: nil) ⇒ Object

runs a noninteractive executable and returns its output as a string raises ExecutionFailed if the exit status is not 0 optionally echoes the executable's output/error to standard output/error



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/tetra/facades/process_runner.rb', line 11

def run(commandline, echo: false, stdin_data: nil)
  log.debug "running `#{commandline}`"

  out_buffer = StringIO.new
  err_buffer = StringIO.new
  # Flatten handles nested arrays, Compact removes nils
  cmd_args = Array(commandline).flatten.compact.map(&:to_s)

  exit_status = spawn_and_capture(cmd_args, stdin_data, echo, out_buffer, err_buffer)

  out = out_buffer.string
  err = err_buffer.string
  raise_execution_failed(commandline, exit_status, out, err) unless exit_status.success?

  out
end

#run_interactive(command) ⇒ Object

runs an interactive executable in a subshell

Raises:



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/tetra/facades/process_runner.rb', line 29

def run_interactive(command)
  log.debug "running interactive `#{command}`"

  # system() passes control to the subprocess
  cmd_args = Array(command).flatten.compact.map(&:to_s)
  success = system(*cmd_args)

  log.debug "`#{command}` exited with success #{success}"

  raise ExecutionFailed.new(command, $CHILD_STATUS.exitstatus, nil, nil) unless success
end