options.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. // Options for gitleaks
  16. type Options struct {
  17. Concurrency int
  18. B64EntropyCutoff int
  19. HexEntropyCutoff int
  20. UserURL string
  21. OrgURL string
  22. RepoURL string
  23. Strict bool
  24. }
  25. // help prints the usage string and exits
  26. func help() {
  27. os.Stderr.WriteString(usage)
  28. os.Exit(1)
  29. }
  30. // optionsNextInt is a parseOptions helper that returns the value (int) of an option
  31. // if valid.
  32. func optionsNextInt(args []string, i *int) int {
  33. if len(args) > *i+1 {
  34. *i++
  35. } else {
  36. help()
  37. }
  38. argInt, err := strconv.Atoi(args[*i])
  39. if err != nil {
  40. fmt.Printf("Invalid %s option: %s\n", args[*i-1], args[*i])
  41. help()
  42. }
  43. return argInt
  44. }
  45. // optionsNextString is a parseOptions helper that returns the value (string) of an option
  46. // if valid.
  47. func optionsNextString(args []string, i *int) string {
  48. if len(args) > *i+1 {
  49. *i++
  50. } else {
  51. fmt.Printf("Invalid %s option: %s\n", args[*i-1], args[*i])
  52. help()
  53. }
  54. return args[*i]
  55. }
  56. // parseOptions
  57. func parseOptions(args []string) *Options {
  58. opts := &Options{
  59. Concurrency: 10,
  60. B64EntropyCutoff: 70,
  61. HexEntropyCutoff: 40,
  62. }
  63. // default is repo if no additional options
  64. if len(args) == 1 {
  65. opts.RepoURL = args[0]
  66. return opts
  67. }
  68. for i := 0; i < len(args); i++ {
  69. arg := args[i]
  70. switch arg {
  71. case "-s":
  72. opts.Strict = true
  73. case "-e":
  74. opts.B64EntropyCutoff = optionsNextInt(args, &i)
  75. case "-x":
  76. opts.HexEntropyCutoff = optionsNextInt(args, &i)
  77. case "-c":
  78. opts.Concurrency = optionsNextInt(args, &i)
  79. case "-o":
  80. opts.OrgURL = optionsNextString(args, &i)
  81. case "-u":
  82. opts.UserURL = optionsNextString(args, &i)
  83. case "-r":
  84. opts.RepoURL = optionsNextString(args, &i)
  85. case "-h", "--help":
  86. help()
  87. return nil
  88. default:
  89. if i == len(args)-1 && opts.OrgURL == "" && opts.RepoURL == "" &&
  90. opts.UserURL == "" {
  91. opts.RepoURL = arg
  92. } else {
  93. fmt.Printf("Uknown option %s\n\n", arg)
  94. help()
  95. return nil
  96. }
  97. }
  98. }
  99. return opts
  100. }