Skip to content

Commit

Permalink
address comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
ahrav committed Jul 31, 2023
1 parent 95b9daf commit a4d1651
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 104 deletions.
42 changes: 27 additions & 15 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import (
"strconv"
"strings"
"syscall"
"time"

"github.com/felixge/fgprof"
"github.com/go-logr/logr"
"github.com/jpillora/overseer"
"google.golang.org/protobuf/types/known/anypb"
"gopkg.in/alecthomas/kingpin.v2"
Expand Down Expand Up @@ -162,7 +162,7 @@ func main() {
// make it the default logger for contexts
context.SetDefaultLogger(logger)
defer func() { _ = sync() }()
logFatal := common.LogFatalFunc(logger)
logFatal := logFatalFunc(logger)

updateCfg := overseer.Config{
Program: run,
Expand All @@ -188,7 +188,7 @@ func main() {
func run(state overseer.State) {
ctx := context.Background()
logger := ctx.Logger()
logFatal := common.LogFatalFunc(logger)
logFatal := logFatalFunc(logger)

logger.V(2).Info(fmt.Sprintf("trufflehog %s", version.BuildVersion))

Expand Down Expand Up @@ -328,7 +328,7 @@ func run(state overseer.State) {
printer = new(output.PlainPrinter)
}

e := engine.Start(ctx,
e, err := engine.Start(ctx,
engine.WithConcurrency(uint8(*concurrency)),
engine.WithDecoders(decoders.DefaultDecoders()...),
engine.WithDetectors(!*noVerification, engine.DefaultDetectors()...),
Expand All @@ -341,6 +341,9 @@ func run(state overseer.State) {
engine.WithPrintAvgDetectorTime(*printAvgDetectorTime),
engine.WithPrinter(printer),
)
if err != nil {
logFatal(err, "error initializing engine")
}

var repoPath string
var remote bool
Expand Down Expand Up @@ -498,12 +501,13 @@ func run(state overseer.State) {
// Wait for all workers to finish.
e.Finish(ctx, logFatal)

metrics := e.GetMetrics()
// Print results.
logger.Info("finished scanning",
"chunks", e.ChunksScanned(),
"bytes", e.BytesScanned(),
"verified-secrets", e.VerifiedResults(),
"unverified-secrets", e.UnverifiedResults(),
"chunks", metrics.ChunksScanned,
"bytes", metrics.BytesScanned,
"verified_secrets", metrics.VerifiedSecretsFound,
"unverified_secrets", metrics.UnverifiedSecretsFound,
)

if *printAvgDetectorTime {
Expand All @@ -516,6 +520,19 @@ func run(state overseer.State) {
}
}

// logFatalFunc returns a log.Fatal style function. Calling the returned
// function will terminate the program without cleanup.
func logFatalFunc(logger logr.Logger) func(error, string, ...any) {
return func(err error, message string, keyAndVals ...any) {
logger.Error(err, message, keyAndVals...)
if err != nil {
os.Exit(1)
return
}
os.Exit(0)
}
}

func commaSeparatedToSlice(s []string) []string {
var result []string
for _, items := range s {
Expand All @@ -532,13 +549,8 @@ func commaSeparatedToSlice(s []string) []string {

func printAverageDetectorTime(e *engine.Engine) {
fmt.Fprintln(os.Stderr, "Average detector time is the measurement of average time spent on each detector when results are returned.")
for detectorName, durations := range e.DetectorAvgTime() {
var total time.Duration
for _, d := range durations {
total += d
}
avgDuration := total / time.Duration(len(durations))
fmt.Fprintf(os.Stderr, "%s: %s\n", detectorName, avgDuration)
for detectorName, duration := range e.GetDetectorsMetrics() {
fmt.Fprintf(os.Stderr, "%s: %s\n", detectorName, duration)
}
}

Expand Down
16 changes: 0 additions & 16 deletions pkg/common/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ import (
"crypto/rand"
"io"
"math/big"
"os"
"strings"

"github.com/go-logr/logr"
)

func AddStringSliceItem(item string, slice *[]string) {
Expand Down Expand Up @@ -67,16 +64,3 @@ func RandomID(length int) string {

return string(b)
}

// LogFatalFunc returns a log.Fatal style function. Calling the returned
// function will terminate the program without cleanup.
func LogFatalFunc(logger logr.Logger) func(error, string, ...any) {
return func(err error, message string, keyAndVals ...any) {
logger.Error(err, message, keyAndVals...)
if err != nil {
os.Exit(1)
return
}
os.Exit(0)
}
}
Loading

0 comments on commit a4d1651

Please sign in to comment.