options.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. package main
  2. import (
  3. "fmt"
  4. "os"
  5. "strconv"
  6. )
  7. const usage = `usage: gitleaks [options] <url>
  8. Options:
  9. -c --concurrency 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. Persist bool
  32. IncludeForks bool
  33. Tmp bool
  34. EnableJSON bool
  35. Token string
  36. Verbose bool
  37. }
  38. // help prints the usage string and exits
  39. func help() {
  40. os.Stderr.WriteString(usage)
  41. os.Exit(1)
  42. }
  43. // optionsNextInt is a parseOptions helper that returns the value (int) of an option if valid
  44. func optionsNextInt(args []string, i *int) int {
  45. if len(args) > *i+1 {
  46. *i++
  47. } else {
  48. help()
  49. }
  50. argInt, err := strconv.Atoi(args[*i])
  51. if err != nil {
  52. fmt.Printf("Invalid %s option: %s\n", args[*i-1], args[*i])
  53. help()
  54. }
  55. return argInt
  56. }
  57. // optionsNextString is a parseOptions helper that returns the value (string) of an option if valid
  58. func optionsNextString(args []string, i *int) string {
  59. if len(args) > *i+1 {
  60. *i++
  61. } else {
  62. fmt.Printf("Invalid %s option: %s\n", args[*i-1], args[*i])
  63. help()
  64. }
  65. return args[*i]
  66. }
  67. // parseOptions
  68. func parseOptions(args []string) *Options {
  69. opts := &Options{
  70. Concurrency: 10,
  71. B64EntropyCutoff: 70,
  72. HexEntropyCutoff: 40,
  73. }
  74. if len(args) == 0 {
  75. help()
  76. }
  77. for i := 0; i < len(args); i++ {
  78. arg := args[i]
  79. switch arg {
  80. case "-s", "--since":
  81. opts.SinceCommit = optionsNextString(args, &i)
  82. case "--strict":
  83. opts.Strict = true
  84. case "-b", "--b64Entropy":
  85. opts.B64EntropyCutoff = optionsNextInt(args, &i)
  86. case "-x", "--hexEntropy":
  87. opts.HexEntropyCutoff = optionsNextInt(args, &i)
  88. case "-e", "--entropy":
  89. opts.Entropy = true
  90. case "-c", "--concurrency":
  91. opts.Concurrency = optionsNextInt(args, &i)
  92. case "-o", "--org":
  93. opts.OrgURL = optionsNextString(args, &i)
  94. case "-u", "--user":
  95. opts.UserURL = optionsNextString(args, &i)
  96. case "-p", "--persist":
  97. opts.UserURL = optionsNextString(args, &i)
  98. case "-r", "--repo":
  99. opts.RepoURL = optionsNextString(args, &i)
  100. case "-f", "--forks":
  101. opts.IncludeForks = true
  102. case "-t", "--temporary":
  103. opts.Tmp = true
  104. case "-gt", "--token":
  105. opts.Token = optionsNextString(args, &i)
  106. case "-j", "--json":
  107. opts.EnableJSON = true
  108. case "-v", "--verbose":
  109. opts.Verbose = true
  110. case "-h", "--help":
  111. help()
  112. return nil
  113. default:
  114. if i == len(args)-1 && opts.OrgURL == "" && opts.RepoURL == "" &&
  115. opts.UserURL == "" {
  116. opts.RepoURL = arg
  117. } else {
  118. fmt.Printf("Uknown option %s\n\n", arg)
  119. help()
  120. }
  121. }
  122. }
  123. return opts
  124. }