Skip to content

Commit

Permalink
DXCDT-321: Fix --no color flag (#594)
Browse files Browse the repository at this point in the history
Co-authored-by: Will Vedder <[email protected]>
  • Loading branch information
willvedd and willvedd authored Jan 11, 2023
1 parent 8417c96 commit 958543a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
12 changes: 12 additions & 0 deletions internal/ansi/ansi.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion internal/cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 958543a

Please sign in to comment.