瀏覽代碼

No color (#1136)

* safer out of bounds

* no color option

* readme
Zachary Rice 2 年之前
父節點
當前提交
63c3076ea2
共有 6 個文件被更改,包括 19 次插入5 次删除
  1. 1 0
      README.md
  2. 4 0
      cmd/detect.go
  3. 4 0
      cmd/protect.go
  4. 1 0
      cmd/root.go
  5. 5 2
      detect/detect.go
  6. 4 3
      detect/utils.go

+ 1 - 0
README.md

@@ -152,6 +152,7 @@ Flags:
   -h, --help                       help for gitleaks
   -l, --log-level string           log level (trace, debug, info, warn, error, fatal) (default "info")
       --max-target-megabytes int   files larger than this will be skipped
+      --no-color                   turn off color for verbose output
       --no-banner                  suppress banner
       --redact                     redact secrets from logs and stdout
   -f, --report-format string       output format (json, csv, sarif) (default "json")

+ 4 - 0
cmd/detect.go

@@ -76,6 +76,10 @@ 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("")
+	}
 
 	if fileExists(filepath.Join(source, ".gitleaksignore")) {
 		if err = detector.AddGitleaksIgnore(filepath.Join(source, ".gitleaksignore")); err != nil {

+ 4 - 0
cmd/protect.go

@@ -69,6 +69,10 @@ 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("")
+	}
 
 	if fileExists(filepath.Join(source, ".gitleaksignore")) {
 		if err = detector.AddGitleaksIgnore(filepath.Join(source, ".gitleaksignore")); err != nil {

+ 1 - 0
cmd/root.go

@@ -45,6 +45,7 @@ func init() {
 	rootCmd.PersistentFlags().StringP("baseline-path", "b", "", "path to baseline with issues that can be ignored")
 	rootCmd.PersistentFlags().StringP("log-level", "l", "info", "log level (trace, debug, info, warn, error, fatal)")
 	rootCmd.PersistentFlags().BoolP("verbose", "v", false, "show verbose output from scan")
+	rootCmd.PersistentFlags().BoolP("no-color", "", false, "turn off color for verbose output")
 	rootCmd.PersistentFlags().Int("max-target-megabytes", 0, "files larger than this will be skipped")
 	rootCmd.PersistentFlags().Bool("redact", false, "redact secrets from logs and stdout")
 	rootCmd.PersistentFlags().Bool("no-banner", false, "suppress banner")

+ 5 - 2
detect/detect.go

@@ -57,6 +57,9 @@ type Detector struct {
 	// followSymlinks is a flag to enable scanning symlink files
 	FollowSymlinks bool
 
+	// NoColor is a flag to disable color output
+	NoColor bool
+
 	// commitMap is used to keep track of commits that have been scanned.
 	// This is only used for logging purposes and git scans.
 	commitMap map[string]bool
@@ -536,7 +539,7 @@ func (d *Detector) DetectReader(r io.Reader, bufSize int) ([]report.Finding, err
 		for _, finding := range d.Detect(fragment) {
 			findings = append(findings, finding)
 			if d.Verbose {
-				printFinding(finding)
+				printFinding(finding, d.NoColor)
 			}
 		}
 	}
@@ -610,7 +613,7 @@ func (d *Detector) addFinding(finding report.Finding) {
 	d.findingMutex.Lock()
 	d.findings = append(d.findings, finding)
 	if d.Verbose {
-		printFinding(finding)
+		printFinding(finding, d.NoColor)
 	}
 	d.findingMutex.Unlock()
 }

+ 4 - 3
detect/utils.go

@@ -91,7 +91,7 @@ func filter(findings []report.Finding, redact bool) []report.Finding {
 	return retFindings
 }
 
-func printFinding(f report.Finding) {
+func printFinding(f report.Finding, noColor bool) {
 	// trim all whitespace and tabs from the line
 	f.Line = strings.TrimSpace(f.Line)
 	// trim all whitespace and tabs from the secret
@@ -104,7 +104,7 @@ func printFinding(f report.Finding) {
 
 	skipColor := false
 
-	if matchInLineIDX == -1 {
+	if matchInLineIDX == -1 || noColor {
 		skipColor = true
 		matchInLineIDX = 0
 	}
@@ -144,11 +144,12 @@ func printFinding(f report.Finding) {
 
 	if skipColor {
 		fmt.Printf("%-12s %s\n", "Finding:", f.Match)
+		fmt.Printf("%-12s %s\n", "Secret:", f.Secret)
 	} else {
 		fmt.Printf("%-12s %s", "Finding:", finding)
+		fmt.Printf("%-12s %s\n", "Secret:", secret)
 	}
 
-	fmt.Printf("%-12s %s\n", "Secret:", secret)
 	fmt.Printf("%-12s %s\n", "RuleID:", f.RuleID)
 	fmt.Printf("%-12s %f\n", "Entropy:", f.Entropy)
 	if f.File == "" {