Class: Bookbinder::Sheller

Inherits:
Object
  • Object
show all
Defined in:
lib/bookbinder/sheller.rb

Defined Under Namespace

Classes: DevNull

Constant Summary collapse

ShelloutFailure =
Class.new(RuntimeError)

Instance Method Summary collapse

Instance Method Details

#get_stdout(command) ⇒ Object



46
47
48
49
50
# File 'lib/bookbinder/sheller.rb', line 46

def get_stdout(command)
  out = StringIO.new
  run_command(command, out: out)
  out.tap(&:rewind).read.chomp
end

#run_command(*command) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/bookbinder/sheller.rb', line 15

def run_command(*command)
  out, err =
    if Hash === command.last
      command.last.values_at(:out, :err)
    else
      [DevNull.new, DevNull.new]
    end

  env_vars, executable =
    if Hash === command.first
      command[0..1]
    else
      [{}, command[0]]
    end

  exit_status = nil
  Open3.popen3(env_vars, executable) do |stdin, stdout, stderr, wait_thr|
    t = Thread.new do
      stdout.each do |line|
        out.puts(line)
      end
    end
    stderr.each do |line|
      err.puts(line)
    end
    t.join
    exit_status = wait_thr.value
  end
  exit_status
end