Skip to content

Commit

Permalink
Add support for new explicit command lifecycle methods from SSHKit
Browse files Browse the repository at this point in the history
The log_command_start, log_command_data and log_command_exit methods will be supported when PR257 is merged - capistrano/sshkit#257
  • Loading branch information
robd committed May 26, 2015
1 parent 3872f41 commit 21c2d70
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 22 deletions.
5 changes: 3 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ rvm:
- 2.1
- 2.2
env:
- sshkit="= 1.6.1"
- sshkit="= 1.7.1"
- sshkit=robd#deprecate-command-stdstream-state
- sshkit="master"
- sshkit="= 1.7.1"
- sshkit="= 1.6.1"
before_install: gem install bundler --conservative --version '~> 1.8'
8 changes: 7 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@ if (sshkit_version = ENV["sshkit"])
requirement = begin
Gem::Dependency.new("sshkit", sshkit_version).requirement
rescue ArgumentError
{ :github => "capistrano/sshkit", :branch => sshkit_version }
user, branch =
if sshkit_version.include?("#")
sshkit_version.split("#")
else
["capistrano", sshkit_version]
end
{ :github => "#{user}/sshkit", :branch => branch }
end
gem "sshkit", requirement
end
68 changes: 51 additions & 17 deletions lib/airbrussh/formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,20 +58,48 @@ def write_log_file_delimiter
end
end

def log_command_start(command)
@log_file_formatter.log_command_start(command)
return if debug?(command)

write_new_rake_task
write_command_start(command)
end

def log_command_data(command, stream_type, line)
@log_file_formatter.log_command_data(command, stream_type, line)
return if debug?(command)

write_command_output_line(command, line)
end

def log_command_exit(command)
@log_file_formatter.log_command_exit(command)
return if debug?(command)

write_command_exit(command)
end

def write(obj)
# SSHKit's :pretty formatter mutates the stdout and stderr data in the
# command obj. So we need to dup it to ensure our copy is unscathed.
@log_file_formatter << obj.dup

unless (new_rake_task = record_new_rake_task).nil?
print_line "#{clock} #{blue(new_rake_task)}"
end
write_new_rake_task

return if debug?(obj)

case obj
when SSHKit::Command then write_command(obj)
when SSHKit::LogMessage then write_log_message(obj)
when SSHKit::Command
write_command_start(obj)

%w(stderr stdout).each do |stream|
write_command_output(obj, stream) if config.log_std_stream?(stream)
end

write_command_exit(obj)
when SSHKit::LogMessage
write_log_message(obj)
end
end

Expand All @@ -94,6 +122,12 @@ def debug?(obj)
obj.respond_to?(:verbosity) && obj.verbosity <= SSHKit::Logger::DEBUG
end

def write_new_rake_task
unless (new_rake_task = record_new_rake_task).nil?
print_line "#{clock} #{blue(new_rake_task)}"
end
end

def record_new_rake_task
if @tasks[@current_rake_task].nil?
@tasks[@current_rake_task] = []
Expand All @@ -110,31 +144,31 @@ def write_log_message(log_message)
print_line(light_black(" " + log_message.to_s))
end

def write_command(command)
def write_command_exit(command)
if command.finished?
print_line " #{format_exit_status(command)} #{runtime(command)}"
end
end

def write_command_start(command)
shell_string = shell_string(command)
if record_first_execution?(shell_string)
print_line " #{number(command)} #{yellow(shell_string)}"
end

# Prints the data from the stdout and stderr streams of the given command,
# but only if enabled (see Airbrussh::Configuration#command_output).
%w(stderr stdout).each do |stream|
write_command_output(command, stream) if config.log_std_stream?(stream)
end

if command.finished?
print_line " #{format_exit_status(command)} #{runtime(command)}"
end
end

def write_command_output(command, stream)
lines = std_stream_lines(command, stream)
return if lines.join.empty?
lines.each do |line|
print_line " #{number(command)} #{line.chomp}"
write_command_output_line(command, line)
end
end

def write_command_output_line(command, line)
print_line " #{number(command)} #{line.chomp}"
end

def std_stream_lines(command, stream)
# Use a bit of meta-programming here, since stderr and stdout logic
# are identical except for different method names.
Expand Down
4 changes: 2 additions & 2 deletions test/test_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def test_formats_failing_execute_with_color

if sshkit_after?("1.6.1")
expected_log_output << command_std_stream(:stderr, error_message)
expected_log_output << "\e[0m" unless sshkit_branch?("master")
expected_log_output << "\e[0m" unless sshkit_branch?("master") || sshkit_branch?("deprecate-command-stdstream-state")
end

assert_log_file_lines(*expected_log_output)
Expand Down Expand Up @@ -318,7 +318,7 @@ def command_failed(exit_status)
def optional_color(text, color)
# Note: SSHKit versions up to 1.7.1 added colors to the log file even though it did not have a tty.
# Versions after this didn't, so we must match output both with, and without colors.
if sshkit_after?("1.7.1") || sshkit_branch?("master")
if sshkit_after?("1.7.1") || sshkit_branch?("master") || sshkit_branch?("deprecate-command-stdstream-state")
text
else
"\\e\\[#{color}m#{text}\\e\\[0m"
Expand Down

0 comments on commit 21c2d70

Please sign in to comment.