options.go 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. package main
  2. import (
  3. "fmt"
  4. "os"
  5. "strconv"
  6. )
  7. // TODO regex on type.. user/organization can be treated as the same:
  8. // hittps://github.com/<user or org>
  9. // hittps://github.com/<user or org>/repo
  10. const usage = `usage: gitleaks [git link] [options]
  11. Options:
  12. -c Concurrency factor (potential number of git files open)
  13. -h --help Display this message
  14. `
  15. type Options struct {
  16. Concurrency int
  17. }
  18. func help() {
  19. os.Stderr.WriteString(usage)
  20. os.Exit(1)
  21. }
  22. func optionsNextInt(args []string, i *int) int {
  23. if len(args) > *i+1 {
  24. *i++
  25. } else {
  26. help()
  27. }
  28. argInt, err := strconv.Atoi(args[*i])
  29. if err != nil {
  30. fmt.Printf("Invalid %s option: %s\n", args[*i-1], args[*i])
  31. help()
  32. }
  33. return argInt
  34. }
  35. func parseOptions(args []string) *Options {
  36. opts := &Options{}
  37. for i := 0; i < len(args); i++ {
  38. arg := args[i]
  39. switch arg {
  40. case "-c":
  41. opts.Concurrency = optionsNextInt(args, &i)
  42. case "-h", "--help":
  43. help()
  44. return nil
  45. default:
  46. fmt.Printf("Uknown option %s\n\n", arg)
  47. help()
  48. return nil
  49. }
  50. }
  51. return opts
  52. }