4
0

options.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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 optionsNextString(args []string, i *int) string {
  36. if len(args) > *i+1 {
  37. *i++
  38. } else {
  39. help()
  40. }
  41. return args[*i]
  42. }
  43. func parseOptions(args []string, repoUrl string) *Options {
  44. opts := &Options{}
  45. for i := 0; i < len(args); i++ {
  46. arg := args[i]
  47. switch arg {
  48. case "-c":
  49. opts.Concurrency = optionsNextInt(args, &i)
  50. case "-h", "--help":
  51. help()
  52. return nil
  53. default:
  54. fmt.Printf("Uknown option %s\n\n", arg)
  55. help()
  56. return nil
  57. }
  58. }
  59. return opts
  60. }