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

@ -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()

@ -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'"))
}

@ -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"`