lib/bolt/shell/bash.rb in bolt-3.6.0 vs lib/bolt/shell/bash.rb in bolt-3.6.1

- old
+ new

@@ -394,11 +394,16 @@ write_stream = in_buffer.empty? ? [] : [inp] # See if there's a sudo prompt if use_sudo ready_read = select([err], nil, nil, timeout * 5) - read_streams[err] << check_sudo(err, inp, options[:stdin]) if ready_read + to_print = check_sudo(err, inp, options[:stdin]) if ready_read + unless to_print.nil? + log_stream(to_print, 'err') + read_streams[err] << to_print + result_output.merged_output << to_print + end end # True while the process is running or waiting for IO input while t.alive? # See if we can read from out or err, or write to in @@ -410,18 +415,11 @@ to_print = if use_sudo check_sudo(stream, inp, options[:stdin]) else stream.readpartial(CHUNK_SIZE) end - - if !to_print.chomp.empty? && @stream_logger - formatted = to_print.lines.map do |msg| - "[#{@target.safe_name}] #{stream_name}: #{msg.chomp}" - end.join("\n") - @stream_logger.warn(formatted) - end - + log_stream(to_print, stream_name) read_streams[stream] << to_print result_output.merged_output << to_print rescue EOFError end @@ -456,11 +454,17 @@ end end # Read any remaining data in the pipe. Do not wait for # EOF in case the pipe is inherited by a child process. read_streams.each do |stream, _| - loop { read_streams[stream] << stream.read_nonblock(CHUNK_SIZE) } + stream_name = stream == out ? 'out' : 'err' + loop { + to_print = stream.read_nonblock(CHUNK_SIZE) + log_stream(to_print, stream_name) + read_streams[stream] << to_print + result_output.merged_output << to_print + } rescue Errno::EAGAIN, EOFError end result_output.stdout << read_streams[out] result_output.stderr << read_streams[err] result_output.exit_code = t.value.respond_to?(:exitstatus) ? t.value.exitstatus : t.value @@ -486,9 +490,18 @@ raise end def sudo_prompt '[sudo] Bolt needs to run as another user, password: ' + end + + private def log_stream(to_print, stream_name) + if !to_print.chomp.empty? && @stream_logger + formatted = to_print.lines.map do |msg| + "[#{@target.safe_name}] #{stream_name}: #{msg.chomp}" + end.join("\n") + @stream_logger.warn(formatted) + end end end end end