forked from gitleaks/gitleaks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.go
136 lines (124 loc) Β· 3.03 KB
/
options.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package main
import (
"fmt"
"os"
"strconv"
)
const usage = `usage: gitleaks [options] <url>
Options:
-c --concurrency Upper bound on concurrent diffs
-u --user Git user url
-r --repo Git repo url
-o --org Git organization url
-s --since Commit to stop at
-b --b64Entropy Base64 entropy cutoff (default is 70)
-x --hexEntropy Hex entropy cutoff (default is 40)
-e --entropy Enable entropy
-j --json Output gitleaks report
-h --help Display this message
--token Github API token
--strict Enables stopwords
`
// Options for gitleaks
type Options struct {
Concurrency int
B64EntropyCutoff int
HexEntropyCutoff int
UserURL string
OrgURL string
RepoURL string
Strict bool
Entropy bool
SinceCommit string
Persist bool
IncludeForks bool
Tmp bool
EnableJSON bool
Token string
Verbose bool
}
// help prints the usage string and exits
func help() {
os.Stderr.WriteString(usage)
os.Exit(1)
}
// optionsNextInt is a parseOptions helper that returns the value (int) of an option if valid
func optionsNextInt(args []string, i *int) int {
if len(args) > *i+1 {
*i++
} else {
help()
}
argInt, err := strconv.Atoi(args[*i])
if err != nil {
fmt.Printf("Invalid %s option: %s\n", args[*i-1], args[*i])
help()
}
return argInt
}
// optionsNextString is a parseOptions helper that returns the value (string) of an option if valid
func optionsNextString(args []string, i *int) string {
if len(args) > *i+1 {
*i++
} else {
fmt.Printf("Invalid %s option: %s\n", args[*i-1], args[*i])
help()
}
return args[*i]
}
// parseOptions
func parseOptions(args []string) *Options {
opts := &Options{
Concurrency: 10,
B64EntropyCutoff: 70,
HexEntropyCutoff: 40,
}
if len(args) == 0 {
help()
}
for i := 0; i < len(args); i++ {
arg := args[i]
switch arg {
case "-s", "--since":
opts.SinceCommit = optionsNextString(args, &i)
case "--strict":
opts.Strict = true
case "-b", "--b64Entropy":
opts.B64EntropyCutoff = optionsNextInt(args, &i)
case "-x", "--hexEntropy":
opts.HexEntropyCutoff = optionsNextInt(args, &i)
case "-e", "--entropy":
opts.Entropy = true
case "-c", "--concurrency":
opts.Concurrency = optionsNextInt(args, &i)
case "-o", "--org":
opts.OrgURL = optionsNextString(args, &i)
case "-u", "--user":
opts.UserURL = optionsNextString(args, &i)
case "-r", "--repo":
opts.RepoURL = optionsNextString(args, &i)
case "-t", "--temporary":
opts.Tmp = true
case "--token":
opts.Token = optionsNextString(args, &i)
case "-j", "--json":
opts.EnableJSON = true
case "-h", "--help":
help()
return nil
default:
if i == len(args)-1 && opts.OrgURL == "" && opts.RepoURL == "" &&
opts.UserURL == "" {
opts.RepoURL = arg
} else {
fmt.Printf("Unknown option %s\n\n", arg)
help()
}
}
}
// "guards"
if opts.Tmp && opts.EnableJSON {
fmt.Println("Report generation with temporary clones not supported")
}
return opts
}