Sha256: aeb1073709dee0342296fa31fedecbef59f17e0b1531c420d6e2b961b27bbf9a

Contents?: true

Size: 1.51 KB

Versions: 4

Compression:

Stored size: 1.51 KB

Contents

require "sshkit"

module Airbrussh
  # This class quacks like an SSHKit::Formatter, but when any formatting
  # methods are called, it simply forwards them to one more more concrete
  # formatters. This allows us to split out the responsibilities of
  # ConsoleFormatter and LogFileFormatter into two separate classes, with
  # DelegatingFormatter forwarding the logging messages to both at once.
  #
  class DelegatingFormatter
    FORWARD_METHODS = %w(
      fatal error warn info debug log
      log_command_start log_command_data log_command_exit
    )
    DUP_AND_FORWARD_METHODS = %w(<< write)

    attr_reader :formatters

    def initialize(formatters)
      @formatters = formatters
    end

    FORWARD_METHODS.each do |method|
      define_method(method) do |*args|
        formatters.map { |f| f.public_send(method, *args) }.last
      end
    end

    # For versions of SSHKit up to and including 1.7.1, the LogfileFormatter
    # and ConsoleFormatter (and all of SSHKit's built in formatters) clear
    # the stdout and stderr data in the command obj. Therefore, ensure only
    # one of the formatters (the last one) gets the original command. This is
    # also the formatter whose return value is passed to the caller.
    #
    DUP_AND_FORWARD_METHODS.each do |method|
      define_method(method) do |command_or_log_message|
        formatters[0...-1].each do |f|
          f.public_send(method, command_or_log_message.dup)
        end
        formatters.last.public_send(method, command_or_log_message)
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
airbrussh-1.0.0.beta1 lib/airbrussh/delegating_formatter.rb
airbrussh-0.8.0 lib/airbrussh/delegating_formatter.rb
airbrussh-0.7.0 lib/airbrussh/delegating_formatter.rb
airbrussh-0.6.0 lib/airbrussh/delegating_formatter.rb