audit.go 1.1 KB

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