Skip to content

Commit

Permalink
Added support for -force-color to the CLI.
Browse files Browse the repository at this point in the history
  • Loading branch information
apollo13 committed Jul 30, 2021
1 parent dffdc93 commit f701661
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 2 deletions.
4 changes: 4 additions & 0 deletions .changelog/10975.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

```release-note:improvement
cli: Added support for `-force-color` to the CLI to force colored output in CI etc.
```
2 changes: 2 additions & 0 deletions command/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
const (
// EnvNomadCLINoColor is an env var that toggles colored UI output.
EnvNomadCLINoColor = `NOMAD_CLI_NO_COLOR`
// EnvNomadCLIForceColor is an env var that forces colored UI output.
EnvNomadCLIForceColor = `NOMAD_CLI_FORCE_COLOR`
)

// DeprecatedCommand is a command that wraps an existing command and prints a
Expand Down
12 changes: 11 additions & 1 deletion command/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ type Meta struct {
// Whether to not-colorize output
noColor bool

// Whether to force colorized output
forceColor bool

// The region to send API requests
region string

Expand Down Expand Up @@ -70,6 +73,7 @@ func (m *Meta) FlagSet(n string, fs FlagSetFlags) *flag.FlagSet {
f.StringVar(&m.region, "region", "", "")
f.StringVar(&m.namespace, "namespace", "", "")
f.BoolVar(&m.noColor, "no-color", false, "")
f.BoolVar(&m.forceColor, "force-color", false, "")
f.StringVar(&m.caCert, "ca-cert", "", "")
f.StringVar(&m.caPath, "ca-path", "", "")
f.StringVar(&m.clientCert, "client-cert", "", "")
Expand Down Expand Up @@ -97,6 +101,7 @@ func (m *Meta) AutocompleteFlags(fs FlagSetFlags) complete.Flags {
"-region": complete.PredictAnything,
"-namespace": NamespacePredictor(m.Client, nil),
"-no-color": complete.PredictNothing,
"-force-color": complete.PredictNothing,
"-ca-cert": complete.PredictFiles("*"),
"-ca-path": complete.PredictDirs("*"),
"-client-cert": complete.PredictFiles("*"),
Expand Down Expand Up @@ -156,7 +161,7 @@ func (m *Meta) allNamespaces() bool {
func (m *Meta) Colorize() *colorstring.Colorize {
return &colorstring.Colorize{
Colors: colorstring.DefaultColors,
Disable: m.noColor || !terminal.IsTerminal(int(os.Stdout.Fd())),
Disable: !m.forceColor && (m.noColor || !terminal.IsTerminal(int(os.Stdout.Fd()))),
Reset: true,
}
}
Expand Down Expand Up @@ -200,6 +205,11 @@ func generalOptionsUsage(usageOpts usageOptsFlags) string {
Disables colored command output. Alternatively, NOMAD_CLI_NO_COLOR may be
set.
-force-color
Forces colored command output. This can be used in cases where the usual
terminal detection fails (CI systems mainly).
Alternatively, NOMAD_CLI_FORCE_COLOR may be set.
-ca-cert=<path>
Path to a PEM encoded CA cert file to use to verify the
Nomad server SSL certificate. Overrides the NOMAD_CACERT
Expand Down
1 change: 1 addition & 0 deletions command/meta_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func TestMeta_FlagSet(t *testing.T) {
[]string{
"address",
"no-color",
"force-color",
"region",
"namespace",
"ca-cert",
Expand Down
13 changes: 12 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,11 @@ func RunCustom(args []string) int {
if os.Getenv(command.EnvNomadCLINoColor) != "" {
color = false
}
// but override if forced (CI systems)
forceColor := false
if os.Getenv(command.EnvNomadCLIForceColor) != "" {
forceColor = true
}

isTerminal := terminal.IsTerminal(int(os.Stdout.Fd()))
metaPtr.Ui = &cli.BasicUi{
Expand All @@ -115,7 +120,7 @@ func RunCustom(args []string) int {
}

// Only use colored UI if stdout is a tty, and not disabled
if isTerminal && color {
if forceColor || (isTerminal && color) {
metaPtr.Ui = &cli.ColoredUi{
ErrorColor: cli.UiColorRed,
WarnColor: cli.UiColorYellow,
Expand Down Expand Up @@ -208,17 +213,23 @@ func printCommand(w io.Writer, name string, cmdFn cli.CommandFactory) {
// values based on format options
func setupEnv(args []string) []string {
noColor := false
forceColor := false
for _, arg := range args {
// Check if color is set
if arg == "-no-color" || arg == "--no-color" {
noColor = true
} else if arg == "-force-color" || arg == "--force-color" {
forceColor = true
}
}

// Put back into the env for later
if noColor {
os.Setenv(command.EnvNomadCLINoColor, "true")
}
if forceColor {
os.Setenv(command.EnvNomadCLIForceColor, "true")
}

return args
}
4 changes: 4 additions & 0 deletions website/content/partials/general_options.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
- `-no-color`: Disables colored command output. Alternatively,
`NOMAD_CLI_NO_COLOR` may be set.

-`-force-color`: Forces colored command output. This can be used in cases where
the usual terminal detection fails (CI systems mainly). Alternatively,
NOMAD_CLI_FORCE_COLOR may be set.

- `-ca-cert=<path>`: Path to a PEM encoded CA cert file to use to verify the
Nomad server SSL certificate. Overrides the `NOMAD_CACERT` environment
variable if set.
Expand Down
4 changes: 4 additions & 0 deletions website/content/partials/general_options_no_namespace.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
- `-no-color`: Disables colored command output. Alternatively,
`NOMAD_CLI_NO_COLOR` may be set.

-`-force-color`: Forces colored command output. This can be used in cases where
the usual terminal detection fails (CI systems mainly). Alternatively,
NOMAD_CLI_FORCE_COLOR may be set.

- `-ca-cert=<path>`: Path to a PEM encoded CA cert file to use to verify the
Nomad server SSL certificate. Overrides the `NOMAD_CACERT` environment
variable if set.
Expand Down

0 comments on commit f701661

Please sign in to comment.