Skip to content

Commit c41b1e2

Browse files
benpaxton-hfjirfag
authored andcommitted
Add --color flag to force colored output on/off
1 parent cfff8fd commit c41b1e2

File tree

3 files changed

+15
-0
lines changed

3 files changed

+15
-0
lines changed

pkg/commands/executor.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package commands
22

33
import (
4+
"github.com/fatih/color"
45
"github.com/spf13/cobra"
56
"github.com/spf13/pflag"
67

@@ -53,6 +54,17 @@ func NewExecutor(version, commit, date string) *Executor {
5354
logutils.SetupVerboseLog(e.log, commandLineCfg.Run.IsVerbose)
5455
}
5556

57+
switch commandLineCfg.Output.Color {
58+
case "always":
59+
color.NoColor = false
60+
case "never":
61+
color.NoColor = true
62+
case "auto":
63+
// nothing
64+
default:
65+
e.log.Fatalf("invalid value %q for --color; must be 'always', 'auto', or 'never'", commandLineCfg.Output.Color)
66+
}
67+
5668
// init of commands must be done before config file reading because
5769
// init sets config with the default values of flags
5870
e.initRoot()

pkg/commands/root.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,4 +103,6 @@ func initRootFlagSet(fs *pflag.FlagSet, cfg *config.Config, needVersionOption bo
103103
if needVersionOption {
104104
fs.BoolVar(&cfg.Run.PrintVersion, "version", false, wh("Print version"))
105105
}
106+
107+
fs.StringVar(&cfg.Output.Color, "color", "auto", wh("Use color when printing; can be 'always', 'auto', or 'never'"))
106108
}

pkg/config/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,7 @@ type Config struct { //nolint:maligned
295295

296296
Output struct {
297297
Format string
298+
Color string
298299
PrintIssuedLine bool `mapstructure:"print-issued-lines"`
299300
PrintLinterName bool `mapstructure:"print-linter-name"`
300301
PrintWelcomeMessage bool `mapstructure:"print-welcome"`

0 commit comments

Comments
 (0)