Module: TinyCI::Subprocesses
Overview
Methods for executing subprocesses in various ways and collecting the results.
Defined Under Namespace
Classes: SubprocessError
Instance Method Summary collapse
-
#execute(*command, label: nil) ⇒ String
Synchronously execute a command as a subprocess and return the output.
-
#execute_pipe(*commands, label: nil) ⇒ String
Synchronously execute a chain multiple commands piped into each other as a subprocess and return the output.
-
#execute_stream(*command, label: nil, pwd: nil) ⇒ TrueClass
Synchronously execute a command as a subprocess and and stream the output asynchronously to
STDOUT.
Instance Method Details
#execute(*command, label: nil) ⇒ String
Synchronously execute a command as a subprocess and return the output.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/tinyci/subprocesses.rb', line 15 def execute(*command, label: nil) output, status = Open3.capture2(*command.flatten) log_debug caller[0] log_debug "CMD: #{command.join(' ')}" log_debug "OUT: #{output}" unless status.success? log_error output raise SubprocessError.new(label, command.join(' '), status.to_i) end output.chomp end |
#execute_pipe(*commands, label: nil) ⇒ String
Synchronously execute a chain multiple commands piped into each other as a subprocess and return the output.
38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/tinyci/subprocesses.rb', line 38 def execute_pipe(*commands, label: nil) stdout, waiters = Open3.pipeline_r(*commands) output = stdout.read waiters.each_with_index do |waiter, i| status = waiter.value unless status.success? log_error output raise SubprocessError.new(label, commands[i].join(' '), status.to_i) end end output.chomp end |
#execute_stream(*command, label: nil, pwd: nil) ⇒ TrueClass
Synchronously execute a command as a subprocess and and stream the output
asynchronously to STDOUT
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/tinyci/subprocesses.rb', line 62 def execute_stream(*command, label: nil, pwd: nil) opts = {} opts[:chdir] = pwd unless pwd.nil? Open3.popen2e(command.join(' '), opts) do |stdin, stdout_and_stderr, wait_thr| stdin.close Thread.new do stdout_and_stderr.each_line do |l| log_info l.chomp $stdout.flush end end unless wait_thr.value.success? raise SubprocessError.new(label, command.join(' '), wait_thr.value) end stdout_and_stderr.close end true end |