4
0

options.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package main
  2. import (
  3. "fmt"
  4. "os"
  5. "strconv"
  6. )
  7. const usage = `usage: gitleaks [options] <url>
  8. Options:
  9. -c Concurrency factor (default is 10)
  10. -u --user Git user url
  11. -r --repo Git repo url
  12. -o --org Git organization url
  13. -s --strict Strict mode uses stopwords in config.yml
  14. -b --b64Entropy Base64 entropy cutoff (default is 70)
  15. -x --hexEntropy Hex entropy cutoff (default is 40)
  16. -e --entropy Enable entropy
  17. -h --help Display this message
  18. `
  19. // Options for gitleaks
  20. type Options struct {
  21. Concurrency int
  22. B64EntropyCutoff int
  23. HexEntropyCutoff int
  24. UserURL string
  25. OrgURL string
  26. RepoURL string
  27. Strict bool
  28. Entropy bool
  29. }
  30. // help prints the usage string and exits
  31. func help() {
  32. os.Stderr.WriteString(usage)
  33. os.Exit(1)
  34. }
  35. // optionsNextInt is a parseOptions helper that returns the value (int) of an option if valid
  36. func optionsNextInt(args []string, i *int) int {
  37. if len(args) > *i+1 {
  38. *i++
  39. } else {
  40. help()
  41. }
  42. argInt, err := strconv.Atoi(args[*i])
  43. if err != nil {
  44. fmt.Printf("Invalid %s option: %s\n", args[*i-1], args[*i])
  45. help()
  46. }
  47. return argInt
  48. }
  49. // optionsNextString is a parseOptions helper that returns the value (string) of an option if valid
  50. func optionsNextString(args []string, i *int) string {
  51. if len(args) > *i+1 {
  52. *i++
  53. } else {
  54. fmt.Printf("Invalid %s option: %s\n", args[*i-1], args[*i])
  55. help()
  56. }
  57. return args[*i]
  58. }
  59. // parseOptions
  60. func parseOptions(args []string) *Options {
  61. opts := &Options{
  62. Concurrency: 10,
  63. B64EntropyCutoff: 70,
  64. HexEntropyCutoff: 40,
  65. Entropy: false,
  66. }
  67. for i := 0; i < len(args); i++ {
  68. arg := args[i]
  69. switch arg {
  70. case "-s", "--strict":
  71. opts.Strict = true
  72. case "-b", "--b64Entropy":
  73. opts.B64EntropyCutoff = optionsNextInt(args, &i)
  74. case "-x", "--hexEntropy":
  75. opts.HexEntropyCutoff = optionsNextInt(args, &i)
  76. case "-e", "--entropy":
  77. opts.Entropy = true
  78. case "-c":
  79. opts.Concurrency = optionsNextInt(args, &i)
  80. case "-o", "--org":
  81. opts.OrgURL = optionsNextString(args, &i)
  82. case "-u", "--user":
  83. opts.UserURL = optionsNextString(args, &i)
  84. case "-r", "--repo":
  85. opts.RepoURL = optionsNextString(args, &i)
  86. case "-h", "--help":
  87. help()
  88. return nil
  89. default:
  90. if i == len(args)-1 && opts.OrgURL == "" && opts.RepoURL == "" &&
  91. opts.UserURL == "" {
  92. opts.RepoURL = arg
  93. } else {
  94. fmt.Printf("Uknown option %s\n\n", arg)
  95. help()
  96. return nil
  97. }
  98. }
  99. }
  100. return opts
  101. }