4
0

options.go 2.3 KB

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