Method: ConcurrentPipeline::Shell.run

Defined in:
lib/concurrent_pipeline/shell.rb

.run(command) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/concurrent_pipeline/shell.rb', line 22

def run(command)
  Open3.popen3(command) do |_in, stdout, stderr, wait_thr|
    process_stdout = []
    stdout_thr = Thread.new do
      while line = stdout.gets&.chomp
        yield(:stdout, line) if block_given?
        process_stdout << line
      end
    end

    process_stderr = []
    stderr_thr = Thread.new do
      while line = stderr.gets&.chomp
        yield(:stderr, line) if block_given?
        process_stderr << line
      end
    end

    [
      stderr_thr,
      stdout_thr,
    ].each(&:join)

    Result.new(
      command: command,
      success: wait_thr.value.success?,
      stdout: process_stdout.join("\n"),
      stderr: process_stderr.join("\n"),
    )
  end
end