Class: Pamyu::Executor

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

Instance Method Summary collapse

Constructor Details

#initialize(command, out, err) ⇒ Executor

Returns a new instance of Executor.



5
6
7
8
9
# File 'lib/pamyu/executor.rb', line 5

def initialize(command, out, err)
  @command = command
  @out = out
  @err = err
end

Instance Method Details

#executeObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/pamyu/executor.rb', line 11

def execute
  status = nil
  Open3.popen3(*@command) do |stdin, stdout, stderr, wait_thr|
    iomap = { stdout => @out, stderr => @err }
    stdin.write($stdin.read) unless $stdin.isatty
    stdin.close_write
    begin
      loop do
        IO.select([stdout, stderr]).flatten.compact.each do |io|
          io.each do |line|
            iomap[io].write line
          end
        end
        break if stdout.eof? && stderr.eof?
      end
    rescue EOFError
    end
    status = wait_thr.value
  end
  status
end