Module: Commandline
- Included in:
- Docker, Exercise::Instructions
- Defined in:
- lib/utils/commandline.rb,
lib/utils/commandline/output.rb,
lib/utils/commandline/return.rb
Defined Under Namespace
Modules: Output Classes: Return
Instance Method Summary collapse
- #capture_output(io, silent: true) ⇒ Object
-
#execute(*commands, fail_message:, pass_message:) ⇒ Object
TODO: - think about how to bring the run and execute methods together or give more differentiating names.
- #run(command, dir: nil, silent: true) ⇒ Object
Instance Method Details
#capture_output(io, silent: true) ⇒ Object
8 9 10 11 12 13 14 15 16 17 |
# 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:) ⇒ Object
TODO: - think about how to bring the run and execute methods together or give more differentiating names
34 35 36 37 38 39 40 41 42 43 44 45 46 |
# 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 end say ok unless fail end |
#run(command, dir: nil, silent: true) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/utils/commandline.rb', line 19 def run(command, dir: nil, silent: true) = {} [:chdir] = dir if dir stdin, stdout, stderr, thread = Open3.popen3("bash -lc '#{command}'", ) 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 |