Skip to content

Commit

Permalink
Support new logging methods with legacy deocrator
Browse files Browse the repository at this point in the history
Add support for the new log_command_* methods with minimal changes to the
existing Formatter code. This is done by decorating the Command object to
make it behave like the legacy version that has stderr/stdout methods, filled
with the data passed to log_command_data.

The other trick is that to avoid calling @log_file_formatter.write within the
legacy code, I use a disabling_log_file method to temporarily "mute" the
@log_file_formatter within a block.

This commit is intended as a stopgap to restore compatibility with with
SSHKit @ master until more serious refactoring can be done.
  • Loading branch information
mattbrictson committed May 30, 2015
1 parent 97f6619 commit 24343d6
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion lib/airbrussh/formatter.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
# encoding: UTF-8
require "airbrussh/command_output"
require "airbrussh/command_with_data"
require "airbrussh/console"
require "colorize"
require "ostruct"
require "sshkit"

# rubocop:disable Metrics/ClassLength

module Airbrussh
class Formatter < SSHKit::Formatter::Abstract
class << self
Expand Down Expand Up @@ -78,14 +81,32 @@ def write_log_file_delimiter
end
end

def log_command_start(command)
@log_file_formatter.log_command_start(command)
disabling_log_file { write(Airbrussh::CommandWithData.new(command)) }
end

def log_command_data(command, stream_type, line)
@log_file_formatter.log_command_data(command, stream_type, line)

command_with_data = Airbrussh::CommandWithData.new(command)
command_with_data.public_send("#{stream_type}=", line)
disabling_log_file { write(command_with_data) }
end

def log_command_exit(command)
@log_file_formatter.log_command_exit(command)
disabling_log_file { write(Airbrussh::CommandWithData.new(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

case obj
when SSHKit::Command then write_command(obj)
when SSHKit::LogMessage then write_log_message(obj)
else write_command(obj)
end
end
alias_method :<<, :write
Expand Down Expand Up @@ -224,5 +245,13 @@ def clock
def config
Airbrussh.configuration
end

def disabling_log_file
orig_log_file_formatter = @log_file_formatter
@log_file_formatter = []
yield
ensure
@log_file_formatter = orig_log_file_formatter
end
end
end

0 comments on commit 24343d6

Please sign in to comment.