From 958543adc8a6534f98c9385fde9cb89e4c4f8d05 Mon Sep 17 00:00:00 2001 From: Will Vedder Date: Wed, 11 Jan 2023 05:02:28 -0500 Subject: [PATCH] DXCDT-321: Fix `--no color` flag (#594) Co-authored-by: Will Vedder --- internal/ansi/ansi.go | 12 ++++++++++++ internal/cli/root.go | 2 +- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/internal/ansi/ansi.go b/internal/ansi/ansi.go index 650f81a5d..b5ea92070 100644 --- a/internal/ansi/ansi.go +++ b/internal/ansi/ansi.go @@ -29,6 +29,10 @@ var DisableColors = false // `CLICOLOR_FORCE`. Cf. https://bixense.com/clicolors/ var EnvironmentOverrideColors = true +// Initialize the aurora.Aurora instance. This value needs to be +// set to prevent any runtime errors. Re-initialization of this +// value is done later in the application lifecycle, once the +// color configuration (ex:`--no-color`) is known. var color = Color() // Bold returns bolded text if the writer supports colors. @@ -42,6 +46,14 @@ func Color() aurora.Aurora { return aurora.NewAurora(shouldUseColors()) } +// Initialize re-instantiates the Aurora instance +// This initialization step is necessary because the parsing of the +// --no-color flag is done fairly late in the application cycle +func Initialize(shouldDisableColors bool) { + DisableColors = shouldDisableColors + color = Color() +} + // ColorizeJSON returns a colorized version of the input JSON, if the writer // supports colors. func ColorizeJSON(json string, darkStyle bool) string { diff --git a/internal/cli/root.go b/internal/cli/root.go index fe0484417..dd69b9a05 100644 --- a/internal/cli/root.go +++ b/internal/cli/root.go @@ -84,7 +84,7 @@ func buildRootCmd(cli *cli) *cobra.Command { Long: rootShort + "\n" + getLogin(cli), Version: buildinfo.GetVersionWithCommit(), PersistentPreRunE: func(cmd *cobra.Command, args []string) error { - ansi.DisableColors = cli.noColor + ansi.Initialize(cli.noColor) prepareInteractivity(cmd) // If the user is trying to login, no need to go