Method: AppleBot::Shell#command

Defined in:
lib/applebot/shell.rb

#command(sys_command, verbose, format) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/applebot/shell.rb', line 3

def command(sys_command, verbose, format)
  output = []
  Open3.popen3(sys_command) do |stdin, stdout, stderr, wait_thr|
    while line = (stdout.gets || stderr.gets)
      output << line
      if verbose === true
        puts_format_line(line, format)
      end
    end
    exit_status = wait_thr.value
    output << JSON.generate(process_status: exit_status.inspect, thread: wait_thr.inspect)
    if exit_status.termsig
      raise SignalTermination.new(exit_status)
    end
    unless exit_status.success?
      raise AppleBotError.for_output(output)
    end
  end

  output.select {|o|
    o.include?('"result":')
  }.last
end