module Commandline
Public Instance Methods
capture_output(io, silent: true)
click to toggle source
# File lib/utils/commandline.rb, line 8 def capture_output(io, silent: true) StringIO.new.tap do |store| Thread.new do while (line = io.getc) store.write(line.dup) print line unless silent || ENV['SILENT'] end end end end
execute(*commands, fail_message:, pass_message:)
click to toggle source
TODO: - think about how to bring the run and execute methods together or give more differentiating names
# File lib/utils/commandline.rb, line 34 def execute(*commands, fail_message:, pass_message:) fail = false commands.each do |command| result = run command say result.stdout next unless result.error? fail = true say result.stderr say error fail_message end say ok pass_message unless fail end
run(command, dir: nil, silent: true)
click to toggle source
# File lib/utils/commandline.rb, line 19 def run(command, dir: nil, silent: true) options = {} options[:chdir] = dir if dir stdin, stdout, stderr, thread = Open3.popen3("bash -lc '#{command}'", options) stderr_output = capture_output(stderr, silent: silent) stdout_output = capture_output(stdout, silent: silent) wait_for_thread(thread) [stdin, stdout, stderr].each(&:close) Return.new(stdout: stdout_output.string, stderr: stderr_output.string, exit_code: thread.value.exitstatus) end
Private Instance Methods
wait_for_thread(thread)
click to toggle source
# File lib/utils/commandline.rb, line 50 def wait_for_thread(thread) sleep 1 while thread.alive? end