Class: VagrantPlugins::ProviderZone::Util::Subprocess

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-zones/util/subprocess.rb

Overview

This shiny device polishes bared foos

Instance Method Summary collapse

Constructor Details

#initialize(cmd, &_block) ⇒ Subprocess

Returns a new instance of Subprocess.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/vagrant-zones/util/subprocess.rb', line 10

def initialize(cmd, &_block)
  Open3.popen3(cmd) do |_stdin, stdout, stderr, thread|
    # read each stream from a new thread
    { :out => stdout, :err => stderr }.each do |key, stream|
      Thread.new do
        until (line = stream.gets).nil?
          # yield the block depending on the stream
          if key == :out
            yield line, nil, thread if block_given?
          elsif block_given?
            yield nil, line, thread
          end
        end
      end
    end
    thread.join # don't exit until the external process is done
  end
end