-
Notifications
You must be signed in to change notification settings - Fork 22
/
config.go
148 lines (122 loc) · 2.78 KB
/
config.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
137
138
139
140
141
142
143
144
145
146
147
148
package hunter
import (
"errors"
"os"
"runtime"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/brittonhayes/pillager/internal/validate"
"github.com/brittonhayes/pillager/pkg/format"
"github.com/brittonhayes/pillager/pkg/rules"
"github.com/spf13/afero"
"github.com/zricethezav/gitleaks/v8/config"
)
// Config takes all of the configurable
// parameters for a Hunter.
type Config struct {
Filesystem afero.Fs
Reporter format.Reporter
Gitleaks config.Config
ScanPath string
Verbose bool
Redact bool
Debug bool
Workers int
Template string
}
type ConfigOption func(*Config)
func NewConfig(opts ...ConfigOption) *Config {
var (
defaultFS = afero.NewOsFs()
defaultVerbose = false
defaultScanPath = "."
defaultReporter = format.JSON{}
defaultWorkers = runtime.NumCPU()
defaultGitleaks = rules.NewLoader().Load()
defaultTemplate = ""
defaultLogLevel = zerolog.ErrorLevel
)
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
zerolog.SetGlobalLevel(defaultLogLevel)
config := &Config{
ScanPath: defaultScanPath,
Filesystem: defaultFS,
Reporter: defaultReporter,
Workers: defaultWorkers,
Gitleaks: defaultGitleaks,
Verbose: defaultVerbose,
Template: defaultTemplate,
}
for _, opt := range opts {
opt(config)
}
if err := config.validate(); err != nil {
log.Fatal().Err(err).Send()
}
return config
}
func WithFS(fs afero.Fs) ConfigOption {
return func(c *Config) {
c.Filesystem = fs
}
}
func WithScanPath(path string) ConfigOption {
return func(c *Config) {
c.ScanPath = validate.Path(c.Filesystem, c.ScanPath)
}
}
func WithLogLevel(level string) ConfigOption {
return func(c *Config) {
lvl, err := zerolog.ParseLevel(level)
if err != nil {
log.Fatal().Err(err).Send()
}
zerolog.SetGlobalLevel(lvl)
}
}
func WithVerbose(verbose bool) ConfigOption {
return func(c *Config) {
c.Verbose = verbose
}
}
func WithWorkers(count int) ConfigOption {
return func(c *Config) {
c.Workers = count
}
}
func WithRedact(redact bool) ConfigOption {
return func(c *Config) {
c.Redact = redact
}
}
func WithFormat(reporter format.Reporter) ConfigOption {
return func(c *Config) {
if c.Template != "" {
custom := &format.Custom{}
custom.WithTemplate(c.Template)
c.Reporter = custom
return
}
c.Reporter = reporter
}
}
func WithTemplate(template string) ConfigOption {
return func(c *Config) {
c.Reporter = format.Custom{}
c.Template = template
}
}
func WithGitleaksConfig(g config.Config) ConfigOption {
return func(c *Config) {
c.Gitleaks = g
}
}
func (c *Config) validate() error {
if c.Filesystem == nil {
return errors.New("missing filesystem in Config")
}
if c.Gitleaks.Rules == nil {
return errors.New("no gitleaks rules provided")
}
return nil
}