From a2ac11aae6a5aafb29fb41d7fa280e734d0b5fb6 Mon Sep 17 00:00:00 2001 From: Matt Brictson Date: Sun, 1 Nov 2015 12:19:23 -0800 Subject: [PATCH 1/2] Raise NameError if a bad formatter name is given This makes the error more explicit and helpful. Previously, setting `output.format = :bad_value` would not raise immediately, but would fail later with a confusing error once a logging operation is attempted. --- lib/sshkit/configuration.rb | 11 +++++++++-- test/unit/test_configuration.rb | 5 +++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/lib/sshkit/configuration.rb b/lib/sshkit/configuration.rb index 701e4cc7..22dc085c 100644 --- a/lib/sshkit/configuration.rb +++ b/lib/sshkit/configuration.rb @@ -53,9 +53,16 @@ def logger(verbosity) end def formatter(format) - SSHKit::Formatter.constants.each do |const| - return SSHKit::Formatter.const_get(const).new($stdout) if const.downcase.eql?(format.downcase) + formatter_class(format).new($stdout) + end + + def formatter_class(symbol) + name = symbol.to_s.downcase + found = SSHKit::Formatter.constants.find do |const| + const.to_s.downcase == name end + fail NameError, 'Unrecognized SSHKit::Formatter "#{symbol}"' if found.nil? + SSHKit::Formatter.const_get(found) end end diff --git a/test/unit/test_configuration.rb b/test/unit/test_configuration.rb index e6dda09f..845a21fa 100644 --- a/test/unit/test_configuration.rb +++ b/test/unit/test_configuration.rb @@ -78,6 +78,11 @@ def test_setting_formatter_types end end + def test_prohibits_unknown_formatter_type_with_exception + assert_raises(NameError) do + SSHKit.config.format = :doesnotexist + end + end end end From 72d051fdac2c46a515b36c9058080113b1c5630e Mon Sep 17 00:00:00 2001 From: Matt Brictson Date: Sun, 1 Nov 2015 12:30:10 -0800 Subject: [PATCH 2/2] Allow formatter config to take options/args This commit introduces `use_format`, which is like the existing `format=` but optionally accepts arguments/options that will be passed to the formatter's constructor. SSHKit's current formatters do not yet accept extra arguments, but this API lays the groundwork for future formatters or formatter gems like airbrussh that are configurable and would benefit from a standard API for doing so. --- lib/sshkit/configuration.rb | 29 +++++++++++++++++++++++------ test/unit/test_configuration.rb | 17 +++++++++++++++++ 2 files changed, 40 insertions(+), 6 deletions(-) diff --git a/lib/sshkit/configuration.rb b/lib/sshkit/configuration.rb index 22dc085c..6f47757a 100644 --- a/lib/sshkit/configuration.rb +++ b/lib/sshkit/configuration.rb @@ -6,7 +6,7 @@ class Configuration attr_writer :output, :backend, :default_env def output - @output ||= formatter(:pretty) + @output ||= use_format(:pretty) end def deprecation_logger @@ -34,8 +34,29 @@ def output_verbosity=(verbosity) @output_verbosity = logger(verbosity) end + # TODO: deprecate in favor of `use_format` def format=(format) - self.output = formatter(format) + use_format(format) + end + + # Tell SSHKit to use the specified `formatter` for stdout. The formatter + # can be the name of a built-in SSHKit formatter, like `:pretty`, a + # formatter class, like `SSHKit::Formatter::Pretty`, or a custom formatter + # class you've written yourself. + # + # Additional arguments will be passed to the formatter's constructor. + # + # Example: + # + # config.use_format(:pretty) + # + # Is equivalent to: + # + # config.output = SSHKit::Formatter::Pretty.new($stdout) + # + def use_format(formatter, *args) + klass = formatter.is_a?(Class) ? formatter : formatter_class(formatter) + self.output = klass.new($stdout, *args) end def command_map @@ -52,10 +73,6 @@ def logger(verbosity) verbosity.is_a?(Integer) ? verbosity : Logger.const_get(verbosity.upcase) end - def formatter(format) - formatter_class(format).new($stdout) - end - def formatter_class(symbol) name = symbol.to_s.downcase found = SSHKit::Formatter.constants.find do |const| diff --git a/test/unit/test_configuration.rb b/test/unit/test_configuration.rb index 845a21fa..9ec545df 100644 --- a/test/unit/test_configuration.rb +++ b/test/unit/test_configuration.rb @@ -83,6 +83,23 @@ def test_prohibits_unknown_formatter_type_with_exception SSHKit.config.format = :doesnotexist end end + + def test_options_can_be_provided_to_formatter + SSHKit.config.use_format(TestFormatter, :color => false) + formatter = SSHKit.config.output + assert_instance_of(TestFormatter, formatter) + assert_equal($stdout, formatter.output) + assert_equal({ :color => false }, formatter.options) + end + + class TestFormatter + attr_accessor :output, :options + + def initialize(output, options={}) + @output = output + @options = options + end + end end end