From c41b1e2034bdd2404d14d695defc4937e70c6cb2 Mon Sep 17 00:00:00 2001 From: Ben Paxton Date: Wed, 13 Mar 2019 17:03:24 +0000 Subject: [PATCH] Add --color flag to force colored output on/off --- pkg/commands/executor.go | 12 ++++++++++++ pkg/commands/root.go | 2 ++ pkg/config/config.go | 1 + 3 files changed, 15 insertions(+) diff --git a/pkg/commands/executor.go b/pkg/commands/executor.go index 481f645d..fe09f10d 100644 --- a/pkg/commands/executor.go +++ b/pkg/commands/executor.go @@ -1,6 +1,7 @@ package commands import ( + "github.com/fatih/color" "github.com/spf13/cobra" "github.com/spf13/pflag" @@ -53,6 +54,17 @@ func NewExecutor(version, commit, date string) *Executor { logutils.SetupVerboseLog(e.log, commandLineCfg.Run.IsVerbose) } + switch commandLineCfg.Output.Color { + case "always": + color.NoColor = false + case "never": + color.NoColor = true + case "auto": + // nothing + default: + e.log.Fatalf("invalid value %q for --color; must be 'always', 'auto', or 'never'", commandLineCfg.Output.Color) + } + // init of commands must be done before config file reading because // init sets config with the default values of flags e.initRoot() diff --git a/pkg/commands/root.go b/pkg/commands/root.go index f41e957d..539b8dff 100644 --- a/pkg/commands/root.go +++ b/pkg/commands/root.go @@ -103,4 +103,6 @@ func initRootFlagSet(fs *pflag.FlagSet, cfg *config.Config, needVersionOption bo if needVersionOption { fs.BoolVar(&cfg.Run.PrintVersion, "version", false, wh("Print version")) } + + fs.StringVar(&cfg.Output.Color, "color", "auto", wh("Use color when printing; can be 'always', 'auto', or 'never'")) } diff --git a/pkg/config/config.go b/pkg/config/config.go index 9a95744a..22d0cd29 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -295,6 +295,7 @@ type Config struct { //nolint:maligned Output struct { Format string + Color string PrintIssuedLine bool `mapstructure:"print-issued-lines"` PrintLinterName bool `mapstructure:"print-linter-name"` PrintWelcomeMessage bool `mapstructure:"print-welcome"`