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 Sep 19, 2021
1 parent f6c1e1d commit 24e99a5
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 5 deletions.
3 changes: 3 additions & 0 deletions .changelog/10975.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```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
14 changes: 10 additions & 4 deletions command/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@ package command

import (
"flag"
"os"
"strings"

"github.com/hashicorp/nomad/api"
"github.com/mitchellh/cli"
"github.com/mitchellh/colorstring"
"github.com/posener/complete"
"golang.org/x/crypto/ssh/terminal"
)

const (
Expand Down Expand Up @@ -39,6 +37,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 +71,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 +99,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 @@ -155,11 +158,10 @@ func (m *Meta) allNamespaces() bool {

func (m *Meta) Colorize() *colorstring.Colorize {
_, coloredUi := m.Ui.(*cli.ColoredUi)
noColor := m.noColor || !coloredUi || !terminal.IsTerminal(int(os.Stdout.Fd()))

return &colorstring.Colorize{
Colors: colorstring.DefaultColors,
Disable: noColor,
Disable: !coloredUi,
Reset: true,
}
}
Expand Down Expand Up @@ -203,6 +205,10 @@ 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. 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 @@ -27,6 +27,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
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
}
3 changes: 3 additions & 0 deletions website/content/partials/general_options.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
- `-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. 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
3 changes: 3 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,9 @@
- `-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. 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 24e99a5

Please sign in to comment.