Skip to content

Commit

Permalink
Override new log_command_* methods in Dot and Pretty formatters
Browse files Browse the repository at this point in the history
No longer rely on state in Command for determining log message type or std stream data
  • Loading branch information
robd committed May 21, 2015
1 parent a888375 commit bd4f49c
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 40 deletions.
2 changes: 0 additions & 2 deletions lib/sshkit/backends/local.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,6 @@ def execute_command(cmd)
stderr_thread.join

cmd.exit_status = wait_thr.value.to_i
cmd.clear_stdout_lines
cmd.clear_stderr_lines

output.log_command_exit(cmd)
end
Expand Down
8 changes: 4 additions & 4 deletions lib/sshkit/formatters/dot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ module Formatter

class Dot < Abstract

def log_command_exit(command)
original_output << colorize('.', command.failure? ? :red : :green)
end

def write(obj)
return unless obj.is_a? SSHKit::Command
if obj.finished?
original_output << colorize('.', obj.failure? ? :red : :green)
end
end

end
Expand Down
58 changes: 26 additions & 32 deletions lib/sshkit/formatters/pretty.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,36 @@ class Pretty < Abstract
LEVEL_COLORS = [:black, :blue, :yellow, :red, :red].freeze

def write(obj)
case obj
when SSHKit::Command then write_command(obj)
when SSHKit::LogMessage then write_message(obj.verbosity, obj.to_s)
if obj.kind_of?(SSHKit::LogMessage)
write_message(obj.verbosity, obj.to_s)
else
raise "Output formatter only supports formatting SSHKit::Command and SSHKit::LogMessage, " \
"called with #{obj.class}: #{obj.inspect}"
raise "write only supports formatting SSHKit::LogMessage, called with #{obj.class}: #{obj.inspect}"
end
end

def log_command_start(command)
host_prefix = command.host.user ? "as #{colorize(command.host.user, :blue)}@" : 'on '
message = "Running #{colorize(command, :yellow, :bold)} #{host_prefix}#{colorize(command.host, :blue)}"
write_message(command.verbosity, message, command.uuid)
write_message(Logger::DEBUG, "Command: #{colorize(command.to_command, :blue)}", command.uuid)
end

def log_command_data(command, stream_type, stream_data)
color = case stream_type
when :stdout then :green
when :stderr then :red
else raise "Unrecognised stream_type #{stream_type}, expected :stdout or :stderr"
end
write_message(Logger::DEBUG, colorize("\t#{stream_data}".chomp, color), command.uuid)
end

def log_command_exit(command)
runtime = sprintf('%5.3f seconds', command.runtime)
successful_or_failed = command.failure? ? colorize('failed', :red, :bold) : colorize('successful', :green, :bold)
message = "Finished in #{runtime} with exit status #{command.exit_status} (#{successful_or_failed})."
write_message(command.verbosity, message, command.uuid)
end

protected

def format_message(verbosity, message, uuid=nil)
Expand All @@ -27,33 +48,6 @@ def format_message(verbosity, message, uuid=nil)

private

def write_command(command)
uuid = command.uuid

unless command.started?
host_prefix = command.host.user ? "as #{colorize(command.host.user, :blue)}@" : 'on '
message = "Running #{colorize(command, :yellow, :bold)} #{host_prefix}#{colorize(command.host, :blue)}"
write_message(command.verbosity, message, uuid)
write_message(Logger::DEBUG, "Command: #{colorize(command.to_command, :blue)}", uuid)
end

write_std_stream_debug(command.clear_stdout_lines, :green, uuid)
write_std_stream_debug(command.clear_stderr_lines, :red, uuid)

if command.finished?
runtime = sprintf('%5.3f seconds', command.runtime)
successful_or_failed = command.failure? ? colorize('failed', :red, :bold) : colorize('successful', :green, :bold)
message = "Finished in #{runtime} with exit status #{command.exit_status} (#{successful_or_failed})."
write_message(command.verbosity, message, uuid)
end
end

def write_std_stream_debug(lines, color, uuid)
lines.each do |line|
write_message(Logger::DEBUG, colorize("\t#{line}".chomp, color), uuid)
end
end

def write_message(verbosity, message, uuid=nil)
original_output << "#{format_message(verbosity, message, uuid)}\n" if verbosity >= SSHKit.config.output_verbosity
end
Expand Down
2 changes: 1 addition & 1 deletion test/unit/formatters/test_pretty.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def test_unsupported_class
raised_error = assert_raises RuntimeError do
pretty << Pathname.new('/tmp')
end
assert_equal('Output formatter only supports formatting SSHKit::Command and SSHKit::LogMessage, called with Pathname: #<Pathname:/tmp>', raised_error.message)
assert_equal('write only supports formatting SSHKit::LogMessage, called with Pathname: #<Pathname:/tmp>', raised_error.message)
end

def test_does_not_log_message_when_verbosity_is_too_low
Expand Down
2 changes: 1 addition & 1 deletion test/unit/formatters/test_simple_text.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def test_unsupported_class
raised_error = assert_raises RuntimeError do
simple << Pathname.new('/tmp')
end
assert_equal('Output formatter only supports formatting SSHKit::Command and SSHKit::LogMessage, called with Pathname: #<Pathname:/tmp>', raised_error.message)
assert_equal('write only supports formatting SSHKit::LogMessage, called with Pathname: #<Pathname:/tmp>', raised_error.message)
end

def test_does_not_log_when_verbosity_is_too_low
Expand Down

0 comments on commit bd4f49c

Please sign in to comment.