Sha256: a159fc5f8331aa65635002f6f135ae1ddb85c452a7b8c480c7cb6d730ee6eea6

Contents?: true

Size: 1.53 KB

Versions: 8

Compression:

Stored size: 1.53 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
    ).freeze
    DUP_AND_FORWARD_METHODS = %w(<< write).freeze

    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

8 entries across 8 versions & 1 rubygems

Version Path
airbrussh-1.3.0 lib/airbrussh/delegating_formatter.rb
airbrussh-1.2.0 lib/airbrussh/delegating_formatter.rb
airbrussh-1.1.2 lib/airbrussh/delegating_formatter.rb
airbrussh-1.1.1 lib/airbrussh/delegating_formatter.rb
airbrussh-1.1.0 lib/airbrussh/delegating_formatter.rb
airbrussh-1.0.2 lib/airbrussh/delegating_formatter.rb
airbrussh-1.0.1 lib/airbrussh/delegating_formatter.rb
airbrussh-1.0.0 lib/airbrussh/delegating_formatter.rb