audit.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package audit
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "path"
  6. "github.com/zricethezav/gitleaks/manager"
  7. log "github.com/sirupsen/logrus"
  8. )
  9. // Run accepts a manager and begins an audit based on the options/configs set in the manager.
  10. func Run(m *manager.Manager) error {
  11. if m.Opts.OwnerPath != "" {
  12. files, err := ioutil.ReadDir(m.Opts.OwnerPath)
  13. if err != nil {
  14. return err
  15. }
  16. for _, f := range files {
  17. if !f.IsDir() {
  18. continue
  19. }
  20. m.Opts.RepoPath = fmt.Sprintf("%s/%s", m.Opts.OwnerPath, f.Name())
  21. if err := runHelper(NewRepo(m)); err != nil {
  22. log.Warnf("%s is not a git repo, skipping", f.Name())
  23. }
  24. }
  25. return nil
  26. }
  27. return runHelper(NewRepo(m))
  28. }
  29. func runHelper(r *Repo) error {
  30. if r.Manager.Opts.OpenLocal() {
  31. r.Name = path.Base(r.Manager.Opts.RepoPath)
  32. if err := r.Open(); err != nil {
  33. return err
  34. }
  35. // Check if we are checking uncommitted files. This is the default behavior
  36. // for a "$gitleaks" command with no options set
  37. if r.Manager.Opts.CheckUncommitted() {
  38. if err := r.AuditUncommitted(); err != nil {
  39. return err
  40. }
  41. return nil
  42. }
  43. } else {
  44. if err := r.Clone(nil); err != nil {
  45. return err
  46. }
  47. }
  48. return r.Audit()
  49. }