Class: Blubber::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/blubber/runner.rb

Instance Method Summary collapse

Constructor Details

#initialize(logger:) ⇒ Runner

Returns a new instance of Runner.



5
6
7
# File 'lib/blubber/runner.rb', line 5

def initialize(logger:)
  @logger = logger
end

Instance Method Details

#run(*cmd) ⇒ Object



9
10
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
# File 'lib/blubber/runner.rb', line 9

def run(*cmd)
  logger.info "Running #{cmd}"
  # see: http://stackoverflow.com/a/1162850/83386
  Open3.popen3(cmd.join(' ')) do |_stdin, stdout, stderr, thread|
    # read each stream from a new thread
    { out: stdout, err: stderr }.each do |key, stream|
      Thread.new do
        begin
          until (line = stream.gets).nil?
            # yield the block depending on the stream
            if key == :out
              logger.info line.strip
              yield line, nil, thread if block_given?
            else
              logger.error line.strip
              yield nil, line, thread if block_given?
            end
          end
        rescue IOError
        end
      end
    end

    thread.join
    thread.value.exitstatus
  end
end