options.go 2.6 KB

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