Browse Source

Quiet flag (#491)

* Adding a quiet option

* better description

Co-authored-by: Zach Rice <zrice@gitlab.com>
Zachary Rice 5 years ago
parent
commit
58d130c5f3
6 changed files with 39 additions and 27 deletions
  1. 4 0
      options/options.go
  2. 5 6
      scan/commit.go
  3. 5 6
      scan/filesatcommit.go
  4. 16 5
      scan/leak.go
  5. 5 6
      scan/nogit.go
  6. 4 4
      scan/unstaged.go

+ 4 - 0
options/options.go

@@ -21,6 +21,7 @@ import (
 // Options stores values of command line options
 type Options struct {
 	Verbose        bool   `short:"v" long:"verbose" description:"Show verbose output from scan"`
+	Quiet          bool   `short:"q" long:"quiet" description:"Sets log level to error and only output leaks, one json object per line"`
 	RepoURL        string `short:"r" long:"repo-url" description:"Repository URL"`
 	Path           string `short:"p" long:"path" description:"Path to directory (repo if contains .git) or file"`
 	ConfigPath     string `short:"c" long:"config-path" description:"Path to config"`
@@ -83,6 +84,9 @@ func ParseOptions() (Options, error) {
 	if opts.Debug {
 		log.SetLevel(log.DebugLevel)
 	}
+	if opts.Quiet {
+		log.SetLevel(log.ErrorLevel)
+	}
 
 	return opts, nil
 }

+ 5 - 6
scan/commit.go

@@ -95,9 +95,8 @@ func (cs *CommitScanner) Scan() (Report, error) {
 						leak.Rule = rule.Description
 						leak.Tags = strings.Join(rule.Tags, ", ")
 
-						if cs.opts.Verbose {
-							leak.Log(cs.opts.Redact)
-						}
+						leak.Log(cs.opts)
+
 						scannerReport.Leaks = append(scannerReport.Leaks, leak)
 						continue
 					}
@@ -137,9 +136,9 @@ func (cs *CommitScanner) Scan() (Report, error) {
 						leak.LeakURL = leak.URL()
 						leak.Rule = rule.Description
 						leak.Tags = strings.Join(rule.Tags, ", ")
-						if cs.opts.Verbose {
-							leak.Log(cs.opts.Redact)
-						}
+
+						leak.Log(cs.opts)
+
 						scannerReport.Leaks = append(scannerReport.Leaks, leak)
 					}
 				}

+ 5 - 6
scan/filesatcommit.go

@@ -81,9 +81,8 @@ func (fs *FilesAtCommitScanner) Scan() (Report, error) {
 				leak.Rule = rule.Description
 				leak.Tags = strings.Join(rule.Tags, ", ")
 
-				if fs.opts.Verbose {
-					leak.Log(fs.opts.Redact)
-				}
+				leak.Log(fs.opts)
+
 				scannerReport.Leaks = append(scannerReport.Leaks, leak)
 				continue
 			}
@@ -122,9 +121,9 @@ func (fs *FilesAtCommitScanner) Scan() (Report, error) {
 				leak.LeakURL = leak.URL()
 				leak.Rule = rule.Description
 				leak.Tags = strings.Join(rule.Tags, ", ")
-				if fs.opts.Verbose {
-					leak.Log(fs.opts.Redact)
-				}
+
+				leak.Log(fs.opts)
+
 				scannerReport.Leaks = append(scannerReport.Leaks, leak)
 			}
 		}

+ 16 - 5
scan/leak.go

@@ -6,6 +6,8 @@ import (
 	"strings"
 	"time"
 
+	"github.com/zricethezav/gitleaks/v7/options"
+
 	"github.com/go-git/go-git/v5/plumbing/object"
 )
 
@@ -56,13 +58,22 @@ func (leak Leak) WithCommit(commit *object.Commit) Leak {
 }
 
 // Log logs a leak and redacts if necessary
-func (leak Leak) Log(redact bool) {
-	if redact {
+func (leak Leak) Log(opts options.Options) {
+	if !opts.Quiet && !opts.Verbose {
+		return
+	}
+	if opts.Redact {
 		leak = RedactLeak(leak)
 	}
-	var b []byte
-	b, _ = json.MarshalIndent(leak, "", "	")
-	fmt.Println(string(b))
+	if opts.Quiet {
+		var b []byte
+		b, _ = json.Marshal(leak)
+		fmt.Println(string(b))
+	} else {
+		var b []byte
+		b, _ = json.MarshalIndent(leak, "", "	")
+		fmt.Println(string(b))
+	}
 }
 
 // URL generates a url to the leak if leak.RepoURL is set

+ 5 - 6
scan/nogit.go

@@ -77,9 +77,8 @@ func (ngs *NoGitScanner) Scan() (Report, error) {
 					leak.Rule = rule.Description
 					leak.Tags = strings.Join(rule.Tags, ", ")
 
-					if ngs.opts.Verbose {
-						leak.Log(ngs.opts.Redact)
-					}
+					leak.Log(ngs.opts)
+
 					leaks <- leak
 				}
 			}
@@ -120,9 +119,9 @@ func (ngs *NoGitScanner) Scan() (Report, error) {
 					leak.LineNumber = lineNumber
 					leak.Rule = rule.Description
 					leak.Tags = strings.Join(rule.Tags, ", ")
-					if ngs.opts.Verbose {
-						leak.Log(ngs.opts.Redact)
-					}
+
+					leak.Log(ngs.opts)
+
 					leaks <- leak
 				}
 			}

+ 4 - 4
scan/unstaged.go

@@ -86,7 +86,7 @@ func (us *UnstagedScanner) Scan() (Report, error) {
 					leak.Rule = rule.Description
 					leak.Tags = strings.Join(rule.Tags, ", ")
 					if us.opts.Verbose {
-						leak.Log(us.opts.Redact)
+						leak.Log(us.opts)
 					}
 					scannerReport.Leaks = append(scannerReport.Leaks, leak)
 				}
@@ -198,9 +198,9 @@ func (us *UnstagedScanner) Scan() (Report, error) {
 					leak.Repo = us.repoName
 					leak.Rule = rule.Description
 					leak.Tags = strings.Join(rule.Tags, ", ")
-					if us.opts.Verbose {
-						leak.Log(us.opts.Redact)
-					}
+
+					leak.Log(us.opts)
+
 					scannerReport.Leaks = append(scannerReport.Leaks, leak)
 				}
 			}