Class: ConcurrentPipeline::Shell

Inherits:
Object
  • Object
show all
Defined in:
lib/concurrent_pipeline/shell.rb

Defined Under Namespace

Classes: Result

Constant Summary collapse

Error =
Class.new(StandardError)

Class Method Summary collapse

Class Method Details

.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

.run!Object



15
16
17
18
19
20
# File 'lib/concurrent_pipeline/shell.rb', line 15

def run!(...)
  # only returns stdout just because.
  run(...)
    .tap { raise "command failed: \n#{_1.inspect}" unless _1.success? }
    .stdout
end