Module: ShellClient
- Included in:
- BashBuildActions::Bash, GitVcsClient::Git
- Defined in:
- lib/plugins/clients/shell_client.rb
Overview
Manages interaction with the command line
Instance Method Summary collapse
-
#run(cmd, &_block) ⇒ Object
rubocop:disable Metrics/MethodLength, Metrics/AbcSize:.
Instance Method Details
#run(cmd, &_block) ⇒ Object
rubocop:disable Metrics/MethodLength, Metrics/AbcSize:
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/plugins/clients/shell_client.rb', line 7 def run(cmd, &_block) result = OpenStruct.new stdout: [], stderr: [], stdin: [] threads = [] Open3.popen3(cmd) do |stdin, stdout, stderr, thread| threads << Thread.new do until (line_out = stdout.gets).nil? line_out = line_out.strip # strip! causes race conditions result[:stdout] << line_out yield line_out, nil, thread if block_given? end end threads << Thread.new do until (line_err = stderr.gets).nil? line_err = line_err.strip # strip! causes race conditions result[:stderr] << line_err yield nil, line_err, thread if block_given? end end # STDIN thread will be killed not joined Thread.new do while thread.alive? line_in = $stdin.gets result[:stdin] << line_in.strip stdin.puts line_in end end threads << thread threads.each(&:join) result[:exitstatus] = thread.value.exitstatus result[:pid] = thread.value.pid result[:success?] = thread.value.success? end result end |