Skip to content

Commit

Permalink
Introduced clear_stdout_lines and clear_stderr_lines on Command
Browse files Browse the repository at this point in the history
This commit removes duplication in the SimpleText and Pretty formatters by better encapsulating the logic to split and clear stdout/stderr buffers on the Command object.
  • Loading branch information
robd committed Apr 22, 2015
1 parent e4f6ca2 commit cd3cdad
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 26 deletions.
8 changes: 8 additions & 0 deletions lib/sshkit/command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,14 @@ def failure?
end
alias :failed? :failure?

def clear_stdout_lines
@stdout.lines.tap { @stdout.clear }
end

def clear_stderr_lines
@stderr.lines.tap { @stderr.clear }
end

def exit_status=(new_exit_status)
@finished_at = Time.now
@exit_status = new_exit_status
Expand Down
22 changes: 8 additions & 14 deletions lib/sshkit/formatters/pretty.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,16 @@ def write_command(command)
end

if SSHKit.config.output_verbosity == Logger::DEBUG
unless command.stdout.empty?
command.stdout.lines.each do |line|
original_output << "%6s %s" % [level(Logger::DEBUG),
uuid(command) + c.green("\t" + line)]
original_output << "\n" unless line[-1] == "\n"
end
command.stdout = ''
command.clear_stdout_lines.each do |line|
original_output << "%6s %s" % [level(Logger::DEBUG),
uuid(command) + c.green("\t" + line)]
original_output << "\n" unless line[-1] == "\n"
end

unless command.stderr.empty?
command.stderr.lines.each do |line|
original_output << "%6s %s" % [level(Logger::DEBUG),
uuid(command) + c.red("\t" + line)]
original_output << "\n" unless line[-1] == "\n"
end
command.stderr = ''
command.clear_stderr_lines.each do |line|
original_output << "%6s %s" % [level(Logger::DEBUG),
uuid(command) + c.red("\t" + line)]
original_output << "\n" unless line[-1] == "\n"
end
end

Expand Down
18 changes: 6 additions & 12 deletions lib/sshkit/formatters/simple_text.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,14 @@ def write_command(command)
end

if SSHKit.config.output_verbosity == Logger::DEBUG
unless command.stdout.empty?
command.stdout.lines.each do |line|
original_output << "\t" + line
original_output << "\n" unless line[-1] == "\n"
end
command.stdout = ''
command.clear_stdout_lines.each do |line|
original_output << "\t" + line
original_output << "\n" unless line[-1] == "\n"
end

unless command.stderr.empty?
command.stderr.lines.each do |line|
original_output << "\t" + line
original_output << "\n" unless line[-1] == "\n"
end
command.stderr = ''
command.clear_stderr_lines.each do |line|
original_output << "\t" + line
original_output << "\n" unless line[-1] == "\n"
end
end

Expand Down
6 changes: 6 additions & 0 deletions test/unit/test_command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -199,5 +199,11 @@ def test_command_raises_command_failed_error_when_non_zero_exit
assert_equal "whoami exit status: 1\nwhoami stdout: Nothing written\nwhoami stderr: Nothing written\n", error.message
end

def test_clear_lines_methods_return_empty_array_when_blank
command = Command.new(:some_command)
assert_equal [], command.clear_stdout_lines
assert_equal [], command.clear_stderr_lines
end

end
end

0 comments on commit cd3cdad

Please sign in to comment.