Module: FlazmRubyHelpers::Os

Defined in:
lib/flazm_ruby_helpers/os.rb

Class Method Summary collapse

Class Method Details

.exec(command, logger: nil, stream_log: false) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/flazm_ruby_helpers/os.rb', line 8

def self.exec(command, logger: nil, stream_log: false)
  logger ||= Logger.new(STDOUT)
  logger.info("Command: #{command}")
  output = [] ; threads = [] ; status = nil
  Open3.popen3(command) do |_stdin, stdout, stderr, wait_thr|
    { out: stdout, err: stderr }.each do |_key, stream|
      threads << Thread.new do
        until (raw_line = stream.gets).nil?
          output << raw_line.to_s
          logger.info(raw_line.to_s) if stream_log
        end
      end
    end
    threads.each(&:join)
    status = wait_thr.value.success?
  end
  return output, status
end