Skip to content

Commit

Permalink
Small refactor detect and sources (#1297)
Browse files Browse the repository at this point in the history
  • Loading branch information
zricethezav committed Nov 22, 2023
1 parent 01e60c8 commit ca7aa14
Show file tree
Hide file tree
Showing 11 changed files with 359 additions and 346 deletions.
14 changes: 11 additions & 3 deletions cmd/detect.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"

"github.com/zricethezav/gitleaks/v8/detect"
"github.com/zricethezav/gitleaks/v8/report"
"github.com/zricethezav/gitleaks/v8/sources"
)

func init() {
Expand Down Expand Up @@ -63,7 +63,11 @@ func runDetect(cmd *cobra.Command, args []string) {

// start the detector scan
if noGit {
findings, err = detector.DetectFiles(source)
paths, err := sources.FilesystemTargets(source, detector.Sema, detector.FollowSymlinks)
if err != nil {
log.Fatal().Err(err)
}
findings, err = detector.DetectFiles(paths)
if err != nil {
// don't exit on error, just log it
log.Error().Err(err).Msg("")
Expand All @@ -81,7 +85,11 @@ func runDetect(cmd *cobra.Command, args []string) {
if err != nil {
log.Fatal().Err(err).Msg("")
}
findings, err = detector.DetectGit(source, logOpts, detect.DetectType)
gitCmd, err := sources.NewGitLogCmd(source, logOpts)
if err != nil {
log.Fatal().Err(err).Msg("")
}
findings, err = detector.DetectGit(gitCmd)
if err != nil {
// don't exit on error, just log it
log.Error().Err(err).Msg("")
Expand Down
17 changes: 5 additions & 12 deletions cmd/protect.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"

"github.com/zricethezav/gitleaks/v8/detect"
"github.com/zricethezav/gitleaks/v8/report"
"github.com/zricethezav/gitleaks/v8/sources"
)

func init() {
Expand Down Expand Up @@ -35,22 +35,15 @@ func runProtect(cmd *cobra.Command, args []string) {
log.Fatal().Err(err).Msg("")
}
start := time.Now()

detector := Detector(cmd, cfg, source)

// get log options for git scan
logOpts, err := cmd.Flags().GetString("log-opts")
if err != nil {
log.Fatal().Err(err).Msg("")
}

// start git scan
var findings []report.Finding
if staged {
findings, err = detector.DetectGit(source, logOpts, detect.ProtectStagedType)
} else {
findings, err = detector.DetectGit(source, logOpts, detect.ProtectType)
gitCmd, err := sources.NewGitDiffCmd(source, staged)
if err != nil {
log.Fatal().Err(err).Msg("")
}
findings, err = detector.DetectGit(gitCmd)

findingSummaryAndExit(findings, cmd, cfg, exitCode, start, err)
}
32 changes: 32 additions & 0 deletions detect/baseline.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"os"
"path/filepath"

"github.com/zricethezav/gitleaks/v8/report"
)
Expand Down Expand Up @@ -49,3 +50,34 @@ func LoadBaseline(baselinePath string) ([]report.Finding, error) {

return previousFindings, nil
}

func (d *Detector) AddBaseline(baselinePath string, source string) error {
if baselinePath != "" {
absoluteSource, err := filepath.Abs(source)
if err != nil {
return err
}

absoluteBaseline, err := filepath.Abs(baselinePath)
if err != nil {
return err
}

relativeBaseline, err := filepath.Rel(absoluteSource, absoluteBaseline)
if err != nil {
return err
}

baseline, err := LoadBaseline(baselinePath)
if err != nil {
return err
}

d.baseline = baseline
baselinePath = relativeBaseline

}

d.baselinePath = baselinePath
return nil
}
Loading

0 comments on commit ca7aa14

Please sign in to comment.