Add --color flag to force colored output on/off

This commit is contained in:
Ben Paxton 2019-03-13 17:03:24 +00:00 committed by Isaev Denis
parent cfff8fd30a
commit c41b1e2034
3 changed files with 15 additions and 0 deletions

View File

@ -1,6 +1,7 @@
package commands package commands
import ( import (
"github.com/fatih/color"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/pflag" "github.com/spf13/pflag"
@ -53,6 +54,17 @@ func NewExecutor(version, commit, date string) *Executor {
logutils.SetupVerboseLog(e.log, commandLineCfg.Run.IsVerbose) 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 of commands must be done before config file reading because
// init sets config with the default values of flags // init sets config with the default values of flags
e.initRoot() e.initRoot()

View File

@ -103,4 +103,6 @@ func initRootFlagSet(fs *pflag.FlagSet, cfg *config.Config, needVersionOption bo
if needVersionOption { if needVersionOption {
fs.BoolVar(&cfg.Run.PrintVersion, "version", false, wh("Print version")) 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'"))
} }

View File

@ -295,6 +295,7 @@ type Config struct { //nolint:maligned
Output struct { Output struct {
Format string Format string
Color string
PrintIssuedLine bool `mapstructure:"print-issued-lines"` PrintIssuedLine bool `mapstructure:"print-issued-lines"`
PrintLinterName bool `mapstructure:"print-linter-name"` PrintLinterName bool `mapstructure:"print-linter-name"`
PrintWelcomeMessage bool `mapstructure:"print-welcome"` PrintWelcomeMessage bool `mapstructure:"print-welcome"`