Class: PackRb::Executor

Inherits:
Object
  • Object
show all
Defined in:
lib/pack_rb/executor.rb

Class Method Summary collapse

Class Method Details

.run_cmd_stream_output(cmd, tpl) ⇒ Array

popen3 wrapper to simultaneously stream command output to the appropriate file descriptor, and capture it.

Parameters:

  • cmd (String)

    command to run

  • tpl (String)

    the template content

Returns:

  • (Array)
    • stdout [String], stderr [String], exit code [Fixnum]



11
12
13
14
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
45
# File 'lib/pack_rb/executor.rb', line 11

def self.run_cmd_stream_output(cmd, tpl)
  all_out = ''
  all_err = ''
  exit_status = nil
  Open3.popen3(cmd) do |stdin, stdout, stderr, wait_thread|
    stdin.write(tpl)
    stdin.close_write
    begin
      files = [stdout, stderr]
      until files.find { |f| !f.eof }.nil?
        ready = IO.select(files)
        next unless ready
        readable = ready[0]
        readable.each do |f|
          begin
            data = f.read_nonblock(512)
            if f.fileno == stdout.fileno
              puts data
              all_out << data
            else
              STDERR.puts data
              all_err << data
            end
          rescue EOFError
            nil
          end
        end
      end
    rescue IOError => e
      STDERR.puts "IOError: #{e}"
    end
    exit_status = wait_thread.value.exitstatus
  end
  [all_out, all_err, exit_status]
end