main.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package main
  2. import (
  3. "os"
  4. "os/signal"
  5. "time"
  6. "github.com/zricethezav/gitleaks/v7/report"
  7. "github.com/zricethezav/gitleaks/v7/config"
  8. "github.com/zricethezav/gitleaks/v7/options"
  9. "github.com/zricethezav/gitleaks/v7/scan"
  10. "github.com/hako/durafmt"
  11. log "github.com/sirupsen/logrus"
  12. )
  13. func main() {
  14. // this block sets up a go routine to listen for an interrupt signal
  15. // which will immediately exit gitleaks
  16. stopChan := make(chan os.Signal, 1)
  17. signal.Notify(stopChan, os.Interrupt)
  18. go listenForInterrupt(stopChan)
  19. // setup options
  20. opts, err := options.ParseOptions()
  21. if err != nil {
  22. log.Error(err)
  23. os.Exit(1)
  24. }
  25. err = opts.Guard()
  26. if err != nil {
  27. log.Error(err)
  28. os.Exit(1)
  29. }
  30. // setup configs
  31. cfg, err := config.NewConfig(opts)
  32. if err != nil {
  33. log.Error(err)
  34. os.Exit(1)
  35. }
  36. // setup scanner
  37. scanner, err := scan.NewScanner(opts, cfg)
  38. if err != nil {
  39. log.Error(err)
  40. os.Exit(1)
  41. }
  42. // run and time the scan
  43. start := time.Now()
  44. scannerReport, err := scanner.Scan()
  45. log.Info("scan time: ", durafmt.Parse(time.Now().Sub(start)))
  46. if err != nil {
  47. log.Error(err)
  48. os.Exit(1)
  49. }
  50. // report scan
  51. if err := report.WriteReport(scannerReport, opts, cfg); err != nil {
  52. log.Error(err)
  53. os.Exit(1)
  54. }
  55. }
  56. func listenForInterrupt(stopScan chan os.Signal) {
  57. <-stopScan
  58. log.Warn("halting gitleaks scan")
  59. os.Exit(1)
  60. }