Class: Wip::CommandRunner

Inherits:
Object
  • Object
show all
Defined in:
lib/wip/command_runner.rb

Overview

Executes a built command, pumping its I/O and returning the exit status.

Instance Method Summary collapse

Constructor Details

#initialize(stdin: $stdin, stdout: $stdout, stderr: $stderr, interpreter: ErrorInterpreter.new) ⇒ CommandRunner

Returns a new instance of CommandRunner.



9
10
11
12
13
14
# File 'lib/wip/command_runner.rb', line 9

def initialize(stdin: $stdin, stdout: $stdout, stderr: $stderr, interpreter: ErrorInterpreter.new)
  @stdin = stdin
  @stdout = stdout
  @stderr = stderr
  @interpreter = interpreter
end

Instance Method Details

#run(command, env: {}, interactive: false) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/wip/command_runner.rb', line 16

def run(command, env: {}, interactive: false)
  @stderr.puts "+ #{Shellwords.join(command)}" if ENV['WIP_DEBUG']
  return run_attached(command, env) if interactive

  captured = +''
  status = nil
  Open3.popen3(env, *command) do |input, output, error, wait|
    input.close
    threads = [pump(output, @stdout, captured), pump(error, @stderr, captured)]
    threads.each(&:join)
    status = wait.value
  end
  hint = @interpreter.interpret(captured)
  @stderr.puts("\n#{hint}") if !status.success? && hint
  status.exitstatus
rescue Errno::ENOENT => e
  @stderr.puts e.message
  127
end