options.go 2.4 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. -u Git user url
  14. -r Git repo url
  15. -o Git organization url
  16. -s Strict mode uses stopwords in checks.go
  17. -e Base64 entropy cutoff, default is 70
  18. -x Hex entropy cutoff, default is 40
  19. -h --help Display this message
  20. `
  21. // Options for gitleaks
  22. type Options struct {
  23. Concurrency int
  24. B64EntropyCutoff int
  25. HexEntropyCutoff int
  26. UserURL string
  27. OrgURL string
  28. RepoURL string
  29. Strict bool
  30. }
  31. // help prints the usage string and exits
  32. func help() {
  33. os.Stderr.WriteString(usage)
  34. os.Exit(1)
  35. }
  36. // optionsNextInt is a parseOptions helper that returns the value (int) of an option
  37. // 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
  52. // if valid.
  53. func optionsNextString(args []string, i *int) string {
  54. if len(args) > *i+1 {
  55. *i++
  56. } else {
  57. fmt.Printf("Invalid %s option: %s\n", args[*i-1], args[*i])
  58. help()
  59. }
  60. return args[*i]
  61. }
  62. // parseOptions
  63. func parseOptions(args []string) *Options {
  64. opts := &Options{
  65. Concurrency: 10,
  66. B64EntropyCutoff: 70,
  67. HexEntropyCutoff: 40,
  68. }
  69. for i := 0; i < len(args); i++ {
  70. arg := args[i]
  71. switch arg {
  72. case "-s":
  73. opts.Strict = true
  74. case "-e":
  75. opts.B64EntropyCutoff = optionsNextInt(args, &i)
  76. case "-x":
  77. opts.HexEntropyCutoff = optionsNextInt(args, &i)
  78. case "-c":
  79. opts.Concurrency = optionsNextInt(args, &i)
  80. case "-o":
  81. opts.OrgURL = optionsNextString(args, &i)
  82. case "-u":
  83. opts.UserURL = optionsNextString(args, &i)
  84. case "-r":
  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. }