From a33efd59f13a9792ec46d7938b7f3aeed517acb4 Mon Sep 17 00:00:00 2001 From: Patrick Blesi Date: Tue, 8 Oct 2019 03:19:50 +0000 Subject: [PATCH] Let ConsoleFormatter context be configurable This allows for custom definitions of how context is defined for executing commands. A passed in context class should implement the following interface: * `current_task_name`: The currently executing task * `register_new_command`: A new command within the current task * `position`: The numerical position of the current command --- README.md | 1 + lib/airbrussh/configuration.rb | 3 ++- lib/airbrussh/console_formatter.rb | 2 +- test/airbrussh/configuration_test.rb | 5 ++++- 4 files changed, 8 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 2a45450..6d44cad 100644 --- a/README.md +++ b/README.md @@ -65,6 +65,7 @@ Here are the options you can use, and their effects (note that the defaults may |`banner`|`nil`|Provide a string (e.g. "Capistrano started!") that will be printed when Capistrano starts up.| |`color`|`:auto`|Use `true` or `false` to enable or disable ansi color. If set to `:auto`, Airbrussh automatically uses color based on whether the output is a TTY, or if the SSHKIT_COLOR environment variable is set.| |`command_output`|`true`|Set to `:stdout`, `:stderr`, or `true` to display the SSH output received via stdout, stderr, or both, respectively. Set to `false` to not show any SSH output, for a minimal look.| +|`context`|`Airbrussh::Rake::Context`|Defines the execution context. Targeted towards uses of Airbrussh outside of Rake/Capistrano. Alternate implementations should provide the definition for `current_task_name`, `register_new_command`, and `position`.| |`log_file`|`log/capistrano.log`|Capistrano's verbose output is saved to this file to facilitate debugging. Set to `nil` to disable completely.| |`truncate`|`:auto`|Set to a number (e.g. 80) to truncate the width of the output to that many characters, or `false` to disable truncation. If `:auto`, output is automatically truncated to the width of the terminal window, if it can be determined.| |`task_prefix`|`nil`|A string to prefix to task output. Handy for output collapsing like [buildkite](https://buildkite.com/docs/builds/managing-log-output)'s `---` prefix| diff --git a/lib/airbrussh/configuration.rb b/lib/airbrussh/configuration.rb index 3b68f36..4595326 100644 --- a/lib/airbrussh/configuration.rb +++ b/lib/airbrussh/configuration.rb @@ -5,7 +5,7 @@ module Airbrussh class Configuration attr_accessor :log_file, :monkey_patch_rake, :color, :truncate, :banner, - :command_output, :task_prefix + :command_output, :task_prefix, :context def initialize self.log_file = nil @@ -15,6 +15,7 @@ def initialize self.banner = :auto self.command_output = false self.task_prefix = nil + self.context = Airbrussh::Rake::Context end def apply_options(options) diff --git a/lib/airbrussh/console_formatter.rb b/lib/airbrussh/console_formatter.rb index 53a9d2c..dc46ab7 100644 --- a/lib/airbrussh/console_formatter.rb +++ b/lib/airbrussh/console_formatter.rb @@ -16,7 +16,7 @@ def initialize(io, config=Airbrussh.configuration) super(io) @config = config - @context = Airbrussh::Rake::Context.new(config) + @context = config.context.new(config) @console = Airbrussh::Console.new(original_output, config) write_banner diff --git a/test/airbrussh/configuration_test.rb b/test/airbrussh/configuration_test.rb index 2f42820..44aa550 100644 --- a/test/airbrussh/configuration_test.rb +++ b/test/airbrussh/configuration_test.rb @@ -16,6 +16,7 @@ def test_defaults assert_nil(@config.task_prefix) refute(@config.monkey_patch_rake) refute(@config.command_output) + assert_equal(Airbrussh::Rake::Context, @config.context) end def test_apply_options @@ -25,7 +26,8 @@ def test_apply_options :truncate => false, :banner => "hi", :monkey_patch_rake => true, - :command_output => true + :command_output => true, + :context => Class ) assert_equal("test", @config.log_file) @@ -34,6 +36,7 @@ def test_apply_options assert_equal("hi", @config.banner) assert(@config.monkey_patch_rake) assert(@config.command_output) + assert_equal(Class, @config.context) end def test_apply_options_warns_on_stderr_of_bad_key