Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DXCDT-321: Fix --no color flag #594

Merged
merged 13 commits into from
Jan 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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