Skip to content

Commit

Permalink
fix: no-color option should also affect zerolog output (gitleaks#1242)
Browse files Browse the repository at this point in the history
  • Loading branch information
savely-krasovsky authored Aug 29, 2023
1 parent 8976539 commit 0b84afa
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
16 changes: 12 additions & 4 deletions cmd/detect.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strings"
"time"

"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
"github.com/spf13/viper"
Expand Down Expand Up @@ -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("")
Expand All @@ -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 {
Expand Down
16 changes: 12 additions & 4 deletions cmd/protect.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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("")
Expand All @@ -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 {
Expand Down

0 comments on commit 0b84afa

Please sign in to comment.