diff --git a/cmd/detect.go b/cmd/detect.go index cc3b20c0c..a497fdbc5 100644 --- a/cmd/detect.go +++ b/cmd/detect.go @@ -6,6 +6,7 @@ import ( "strings" "time" + "github.com/rs/zerolog" "github.com/rs/zerolog/log" "github.com/spf13/cobra" "github.com/spf13/viper" @@ -54,6 +55,17 @@ func runDetect(cmd *cobra.Command, args []string) { // Setup detector detector := detect.NewDetector(cfg) + // set color flag at first + if detector.NoColor, err = cmd.Flags().GetBool("no-color"); err != nil { + log.Fatal().Err(err).Msg("") + } + // also init logger again without color + if detector.NoColor { + log.Logger = log.Output(zerolog.ConsoleWriter{ + Out: os.Stderr, + NoColor: detector.NoColor, + }) + } detector.Config.Path, err = cmd.Flags().GetString("config") if err != nil { log.Fatal().Err(err).Msg("") @@ -78,10 +90,6 @@ func runDetect(cmd *cobra.Command, args []string) { if detector.MaxTargetMegaBytes, err = cmd.Flags().GetInt("max-target-megabytes"); err != nil { log.Fatal().Err(err).Msg("") } - // set color flag - if detector.NoColor, err = cmd.Flags().GetBool("no-color"); err != nil { - log.Fatal().Err(err).Msg("") - } gitleaksIgnorePath, err := cmd.Flags().GetString("gitleaks-ignore-path") if err != nil { diff --git a/cmd/protect.go b/cmd/protect.go index 00412e25d..0526ccb46 100644 --- a/cmd/protect.go +++ b/cmd/protect.go @@ -5,6 +5,7 @@ import ( "path/filepath" "time" + "github.com/rs/zerolog" "github.com/rs/zerolog/log" "github.com/spf13/cobra" "github.com/spf13/viper" @@ -46,6 +47,17 @@ func runProtect(cmd *cobra.Command, args []string) { // Setup detector detector := detect.NewDetector(cfg) + // set color flag at first + if detector.NoColor, err = cmd.Flags().GetBool("no-color"); err != nil { + log.Fatal().Err(err).Msg("") + } + // also init logger again without color + if detector.NoColor { + log.Logger = log.Output(zerolog.ConsoleWriter{ + Out: os.Stderr, + NoColor: detector.NoColor, + }) + } detector.Config.Path, err = cmd.Flags().GetString("config") if err != nil { log.Fatal().Err(err).Msg("") @@ -70,10 +82,6 @@ func runProtect(cmd *cobra.Command, args []string) { if detector.MaxTargetMegaBytes, err = cmd.Flags().GetInt("max-target-megabytes"); err != nil { log.Fatal().Err(err).Msg("") } - // set color flag - if detector.NoColor, err = cmd.Flags().GetBool("no-color"); err != nil { - log.Fatal().Err(err).Msg("") - } gitleaksIgnorePath, err := cmd.Flags().GetString("gitleaks-ignore-path") if err != nil {