From 804fa517c6b0531f4a8764736e4975abf7189bad Mon Sep 17 00:00:00 2001 From: Ewoud Kohl van Wijngaarden Date: Thu, 4 Jul 2024 18:38:48 +0200 Subject: [PATCH] Auto-detect if colors are supported Not all output supports colors. For example, cron doesn't. This copies the autodetection from Kafo to disable colors if needed. --- lib/foreman_maintain.rb | 5 +++++ lib/foreman_maintain/config.rb | 5 +++++ test/lib/cli/health_command_test.rb | 8 ++++---- test/lib/reporter_test.rb | 1 - test/test_helper.rb | 7 ++----- 5 files changed, 16 insertions(+), 10 deletions(-) diff --git a/lib/foreman_maintain.rb b/lib/foreman_maintain.rb index 9b31a2fe5..8787a32fe 100644 --- a/lib/foreman_maintain.rb +++ b/lib/foreman_maintain.rb @@ -68,6 +68,7 @@ def setup(options = {}) # using a queue, we can log the messages which are generated before initializing logger self.config = Config.new(options) + configure_highline load_definitions init_logger update_path @@ -102,6 +103,10 @@ def load_definitions end end + def configure_highline + HighLine.use_color = config.use_color? + end + def cache ObjectCache.instance end diff --git a/lib/foreman_maintain/config.rb b/lib/foreman_maintain/config.rb index 0aa2b0088..7ed7f74a9 100644 --- a/lib/foreman_maintain/config.rb +++ b/lib/foreman_maintain/config.rb @@ -25,6 +25,11 @@ def initialize(options) @foreman_port = @options.fetch(:foreman_port, 443) end + def use_color? + ENV['TERM'] && ENV.fetch('NO_COLOR', '') == '' && \ + system('command -v tput', out: File.open('/dev/null')) && `tput colors`.to_i > 0 + end + private def load_log_configs diff --git a/test/lib/cli/health_command_test.rb b/test/lib/cli/health_command_test.rb index c73496a69..e2927bd04 100644 --- a/test/lib/cli/health_command_test.rb +++ b/test/lib/cli/health_command_test.rb @@ -40,10 +40,10 @@ module ForemanMaintain [dummy-check-fail-skipwhitelist] Check that ends up with fail [dummy-check-success] Check that ends up with success [dummy-check-warn] Check that ends up with warning - [external-service-is-accessible] External_service_is_accessible [pre-upgrade-check] - [present-service-is-running] Present service run check [default] - [service-is-stopped] Service not running check [default] - [upgrade-post-upgrade-check] Procedures::Upgrade::PostUpgradeCheck [post-upgrade-checks] + [external-service-is-accessible] External_service_is_accessible [pre-upgrade-check] + [present-service-is-running] Present service run check [default] + [service-is-stopped] Service not running check [default] + [upgrade-post-upgrade-check] Procedures::Upgrade::PostUpgradeCheck [post-upgrade-checks] OUTPUT end end diff --git a/test/lib/reporter_test.rb b/test/lib/reporter_test.rb index cafeccb50..160804583 100644 --- a/test/lib/reporter_test.rb +++ b/test/lib/reporter_test.rb @@ -388,7 +388,6 @@ def captured_out(simulate_terminal = true) # simulate carriage returns to get the output as user would see it out = capture.read out = simulate_carriage_returns(out) if simulate_terminal - out = remove_colors(out) capture.rewind out end diff --git a/test/test_helper.rb b/test/test_helper.rb index 7bed19b5a..285098d42 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -1,3 +1,4 @@ +ENV['NO_COLOR'] = '1' require 'foreman_maintain' require 'minitest/spec' require 'minitest/autorun' @@ -19,7 +20,7 @@ def assert_cmd(expected_output, args = [], ignore_whitespace: false) expected_output = expected_output.gsub(/\s+/, ' ') output = output.gsub(/\s+/, ' ') end - assert_equal expected_output, remove_colors(simulate_carriage_returns(output)) + assert_equal expected_output, simulate_carriage_returns(output) end def capture_io_with_stderr @@ -48,10 +49,6 @@ def run_cmd(args = []) def simulate_carriage_returns(output) output.gsub(/^.*\r/, '') end - - def remove_colors(output) - output.gsub(/\e.*?m/, '') - end end class FakePackageManager < ForemanMaintain::PackageManager::Base