|
6 | 6 | "strings"
|
7 | 7 |
|
8 | 8 | "github.com/hashicorp/nomad/api"
|
| 9 | + colorable "github.com/mattn/go-colorable" |
9 | 10 | "github.com/mitchellh/cli"
|
10 | 11 | "github.com/mitchellh/colorstring"
|
11 | 12 | "github.com/posener/complete"
|
@@ -39,6 +40,9 @@ type Meta struct {
|
39 | 40 | // Whether to not-colorize output
|
40 | 41 | noColor bool
|
41 | 42 |
|
| 43 | + // Whether to force colorized output |
| 44 | + forceColor bool |
| 45 | + |
42 | 46 | // The region to send API requests
|
43 | 47 | region string
|
44 | 48 |
|
@@ -70,6 +74,7 @@ func (m *Meta) FlagSet(n string, fs FlagSetFlags) *flag.FlagSet {
|
70 | 74 | f.StringVar(&m.region, "region", "", "")
|
71 | 75 | f.StringVar(&m.namespace, "namespace", "", "")
|
72 | 76 | f.BoolVar(&m.noColor, "no-color", false, "")
|
| 77 | + f.BoolVar(&m.forceColor, "force-color", false, "") |
73 | 78 | f.StringVar(&m.caCert, "ca-cert", "", "")
|
74 | 79 | f.StringVar(&m.caPath, "ca-path", "", "")
|
75 | 80 | f.StringVar(&m.clientCert, "client-cert", "", "")
|
@@ -97,6 +102,7 @@ func (m *Meta) AutocompleteFlags(fs FlagSetFlags) complete.Flags {
|
97 | 102 | "-region": complete.PredictAnything,
|
98 | 103 | "-namespace": NamespacePredictor(m.Client, nil),
|
99 | 104 | "-no-color": complete.PredictNothing,
|
| 105 | + "-force-color": complete.PredictNothing, |
100 | 106 | "-ca-cert": complete.PredictFiles("*"),
|
101 | 107 | "-ca-path": complete.PredictDirs("*"),
|
102 | 108 | "-client-cert": complete.PredictFiles("*"),
|
@@ -155,15 +161,47 @@ func (m *Meta) allNamespaces() bool {
|
155 | 161 |
|
156 | 162 | func (m *Meta) Colorize() *colorstring.Colorize {
|
157 | 163 | _, coloredUi := m.Ui.(*cli.ColoredUi)
|
158 |
| - noColor := m.noColor || !coloredUi || !terminal.IsTerminal(int(os.Stdout.Fd())) |
159 | 164 |
|
160 | 165 | return &colorstring.Colorize{
|
161 | 166 | Colors: colorstring.DefaultColors,
|
162 |
| - Disable: noColor, |
| 167 | + Disable: !coloredUi, |
163 | 168 | Reset: true,
|
164 | 169 | }
|
165 | 170 | }
|
166 | 171 |
|
| 172 | +func (m *Meta) SetupUi(args []string) { |
| 173 | + noColor := os.Getenv(EnvNomadCLINoColor) != "" |
| 174 | + forceColor := os.Getenv(EnvNomadCLIForceColor) != "" |
| 175 | + |
| 176 | + for _, arg := range args { |
| 177 | + // Check if color is set |
| 178 | + if arg == "-no-color" || arg == "--no-color" { |
| 179 | + noColor = true |
| 180 | + } else if arg == "-force-color" || arg == "--force-color" { |
| 181 | + forceColor = true |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | + m.Ui = &cli.BasicUi{ |
| 186 | + Reader: os.Stdin, |
| 187 | + Writer: colorable.NewColorableStdout(), |
| 188 | + ErrorWriter: colorable.NewColorableStderr(), |
| 189 | + } |
| 190 | + |
| 191 | + // Only use colored UI if not disabled and stdout is a tty or colors are |
| 192 | + // forced. |
| 193 | + isTerminal := terminal.IsTerminal(int(os.Stdout.Fd())) |
| 194 | + useColor := !noColor && (isTerminal || forceColor) |
| 195 | + if useColor { |
| 196 | + m.Ui = &cli.ColoredUi{ |
| 197 | + ErrorColor: cli.UiColorRed, |
| 198 | + WarnColor: cli.UiColorYellow, |
| 199 | + InfoColor: cli.UiColorGreen, |
| 200 | + Ui: m.Ui, |
| 201 | + } |
| 202 | + } |
| 203 | +} |
| 204 | + |
167 | 205 | type usageOptsFlags uint8
|
168 | 206 |
|
169 | 207 | const (
|
@@ -196,12 +234,17 @@ func generalOptionsUsage(usageOpts usageOptsFlags) string {
|
196 | 234 | `
|
197 | 235 |
|
198 | 236 | // note: that although very few commands use color explicitly, all of them
|
199 |
| - // return red-colored text on error so we don't want to make this |
200 |
| - // configurable |
| 237 | + // return red-colored text on error so we want the color flags to always be |
| 238 | + // present in the help messages. |
201 | 239 | remainingText := `
|
202 | 240 | -no-color
|
203 | 241 | Disables colored command output. Alternatively, NOMAD_CLI_NO_COLOR may be
|
204 |
| - set. |
| 242 | + set. This option takes precedence over -force-color. |
| 243 | +
|
| 244 | + -force-color |
| 245 | + Forces colored command output. This can be used in cases where the usual |
| 246 | + terminal detection fails. Alternatively, NOMAD_CLI_FORCE_COLOR may be set. |
| 247 | + This option has no effect if -no-color is also used. |
205 | 248 |
|
206 | 249 | -ca-cert=<path>
|
207 | 250 | Path to a PEM encoded CA cert file to use to verify the
|
|
0 commit comments