diff options
author | Max Lincoln <max@devopsy.com> | 2014-07-17 13:00:39 -0400 |
---|---|---|
committer | Max Lincoln <max@devopsy.com> | 2014-07-17 13:12:42 -0400 |
commit | ef704af76fa6d2512782824c7c3ba52a9deac3ab (patch) | |
tree | 6dcd987d970cf6521ba64bbc6ced950cd6966163 /lib | |
parent | 5ce6ead83be1a46241db53e401f44c9a40f598ce (diff) | |
download | mixlib-shellout-ef704af76fa6d2512782824c7c3ba52a9deac3ab.tar.gz |
Simplify live_stream vs live_stdout vs live_stderr based on PR comments
Diffstat (limited to 'lib')
-rw-r--r-- | lib/mixlib/shellout.rb | 45 | ||||
-rw-r--r-- | lib/mixlib/shellout/unix.rb | 4 | ||||
-rw-r--r-- | lib/mixlib/shellout/windows.rb | 2 |
3 files changed, 25 insertions, 26 deletions
diff --git a/lib/mixlib/shellout.rb b/lib/mixlib/shellout.rb index a633819..6261c21 100644 --- a/lib/mixlib/shellout.rb +++ b/lib/mixlib/shellout.rb @@ -53,13 +53,13 @@ module Mixlib # to determine if the command was successful. Normally set via options to new attr_accessor :valid_exit_codes - # When live_stream is set, stdout and stderr of the subprocess will be - # copied to it as the subprocess is running. The stderr will also be copied, - # unless live_stderr_stream is set to nil or a different object. For example, - # if live_stream is set to STDOUT, the command's output will be echoed to STDOUT. - attr_accessor :live_stream + # When live_stdout is set, the stdout of the subprocess will be copied to it + # as the subprocess is running. + attr_accessor :live_stdout - attr_writer :live_stderr_stream + # When live_stderr is set, the stderr of the subprocess will be copied to it + # as the subprocess is running. + attr_accessor :live_stderr # ShellOut will push data from :input down the stdin of the subprocss. # Normally set via options passed to new. @@ -150,7 +150,7 @@ module Mixlib # cmd.run_command # etc. def initialize(*command_args) @stdout, @stderr = '', '' - @live_stream = nil + @live_stdout = @live_stderr = nil @input = nil @log_level = :debug @log_tag = nil @@ -166,18 +166,16 @@ module Mixlib @command = command_args.size == 1 ? command_args.first : command_args end - # When live_stderr_stream is set, the stderr of the subprocess will be - # copied to it as the subprocess is running. For example, if live_stderr_stream is - # set to STDERR, the command's output will be echoed to STDERR. The default - # value is to match live_stream, so setting live_stream to STDOUT will also - # set live_stderr_stream to STDOUT. If you only want the stdout of the subprocess - # copied, then you should explicitly set live_stderr_stream to nil. - def live_stderr_stream - # We can't use ||= because it would override an explicit nil - unless defined?(@live_stderr_stream) - @live_stderr_stream = live_stream - end - @live_stderr_stream + # Returns the stream that both is being used by both live_stdout and live_stderr, or nil + def live_stream + live_stdout == live_stderr ? live_stdout : nil + end + + # A shortcut for setting both live_stdout and live_stderr, so that both the + # stdout and stderr from the subprocess will be copied to the same stream as + # the subprocess is running. + def live_stream=(stream) + @live_stdout = @live_stderr = stream end # Set the umask that the subprocess will have. If given as a string, it @@ -303,10 +301,11 @@ module Mixlib when 'returns' self.valid_exit_codes = Array(setting) when 'live_stream' - self.live_stream = setting - when 'live_stderr_stream' - puts "Setting to #{setting}" - self.live_stderr_stream = setting + self.live_stdout = self.live_stderr = setting + when 'live_stdout' + self.live_stdout = setting + when 'live_stderr' + self.live_stderr = setting when 'input' self.input = setting when 'logger' diff --git a/lib/mixlib/shellout/unix.rb b/lib/mixlib/shellout/unix.rb index 249c3d4..be2e66a 100644 --- a/lib/mixlib/shellout/unix.rb +++ b/lib/mixlib/shellout/unix.rb @@ -278,7 +278,7 @@ module Mixlib def read_stdout_to_buffer while chunk = child_stdout.read_nonblock(READ_SIZE) @stdout << chunk - @live_stream << chunk if @live_stream + @live_stdout << chunk if @live_stdout end rescue Errno::EAGAIN rescue EOFError @@ -288,7 +288,7 @@ module Mixlib def read_stderr_to_buffer while chunk = child_stderr.read_nonblock(READ_SIZE) @stderr << chunk - live_stderr_stream << chunk if live_stderr_stream + @live_stderr << chunk if @live_stderr end rescue Errno::EAGAIN rescue EOFError diff --git a/lib/mixlib/shellout/windows.rb b/lib/mixlib/shellout/windows.rb index 62adc37..2ae0256 100644 --- a/lib/mixlib/shellout/windows.rb +++ b/lib/mixlib/shellout/windows.rb @@ -167,7 +167,7 @@ module Mixlib begin next_chunk = stderr_read.readpartial(READ_SIZE) @stderr << next_chunk - @live_stderr_stream << next_chunk if @live_stderr_stream + @live_stderr << next_chunk if @live_stderr rescue EOFError stderr_read.close open_streams.delete(stderr_read) |