Class: FPM::Dockery::Utils::Subprocess
- Inherits:
-
Object
- Object
- FPM::Dockery::Utils::Subprocess
- Extended by:
- Logging
- Defined in:
- lib/fpm/dockery/utils.rb
Overview
A moderately decent way of dealing with running commands and coping with stdout/stderr
Class Method Summary collapse
Methods included from Logging
Class Method Details
.run(cmd, &block) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/fpm/dockery/utils.rb', line 9 def self.run(cmd, &block) info "Running command '#{cmd}'" log_command = "#{cmd.split(' ')[0]}" # see: http://stackoverflow.com/a/1162850/83386 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? do # yield the block depending on the stream if key == :out info("[#{log_command}] [stdout] #{line.chomp}") unless line.nil? else info("[#{log_command}] [stderr] #{line.chomp}") unless line.nil? end end end end thread.join # don't exit until the external process is done return thread.value end end |