audit.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package audit
  2. import (
  3. "fmt"
  4. "github.com/zricethezav/gitleaks/manager"
  5. "io/ioutil"
  6. "path"
  7. )
  8. func Run(m *manager.Manager) error {
  9. if m.Opts.OwnerPath != "" {
  10. files, err := ioutil.ReadDir(m.Opts.OwnerPath)
  11. if err != nil {
  12. return err
  13. }
  14. for _, f := range files {
  15. if !f.IsDir() {
  16. continue
  17. }
  18. m.Opts.RepoPath = fmt.Sprintf("%s/%s",m.Opts.OwnerPath, f.Name())
  19. if err := runHelper(NewRepo(m)); err != nil {
  20. // TODO or send to errchan?
  21. return err
  22. }
  23. }
  24. return nil
  25. }
  26. return runHelper(NewRepo(m))
  27. }
  28. func runHelper(r *Repo) error {
  29. // Check if gitleaks will perform a local audit.
  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.AuditLocal(); err != nil {
  39. return err
  40. }
  41. return nil
  42. }
  43. } else {
  44. if err := r.Clone(); err != nil {
  45. return err
  46. }
  47. }
  48. return r.Audit()
  49. }