-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
config.go
111 lines (99 loc) · 2.86 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
package config
import (
"fmt"
"io/ioutil"
"time"
"github.com/AlexAkulov/hungryfox/helpers"
"gopkg.in/yaml.v2"
)
type WebHook struct {
Enable bool `yaml:"enable"`
Method string `yaml:"method"`
URL string `yaml:"url"`
Headers map[string]string `yaml:"headers"`
}
type SMTP struct {
Enable bool `yaml:"enable"`
From string `yaml:"mail_from"`
Host string `yaml:"host"`
Port int `yaml:"port"`
TLS bool `yaml:"tls"`
Username string `yaml:"username"`
Password string `yaml:"password"`
Recipient string `yaml:"recipient"`
SentToAuthor bool `yaml:"sent_to_autor"`
Delay string `yaml:"delay"`
}
type Config struct {
Common *Common `yaml:"common"`
Inspect []Inspect `yaml:"inspect"`
Patterns []Pattern `yaml:"patterns"`
Filters []Pattern `yaml:"filters"`
SMTP *SMTP `yaml:"smtp"`
WebHook *WebHook `yaml:"webhook"`
}
type Inspect struct {
Type string `yaml:"type"`
Paths []string `yaml:"paths"`
URL string `yaml:"url"`
Token string `yaml:"token"`
TrimPrefix string `yaml:"trim_prefix"`
TrimSuffix string `yaml:"trim_suffix"`
WorkDir string `yaml:"work_dir"`
Users []string `yaml:"users"`
Repos []string `yaml:"repos"`
Orgs []string `yaml:"orgs"`
}
type Common struct {
StateFile string `yaml:"state_file"`
HistoryPastLimitString string `yaml:"history_limit"`
LogLevel string `yaml:"log_level"`
LeaksFile string `yaml:"leaks_file"`
ScanIntervalString string `yaml:"scan_interval"`
PatternsPath string `yaml:"patterns_path"`
FiltresPath string `yaml:"filters_path"`
Workers int `yaml:"workers"`
HistoryPastLimit time.Time
ScanInterval time.Duration
}
type Pattern struct {
Name string `yaml:"name"`
File string `yaml:"file"`
Content string `yaml:"content"`
}
func defaultConfig() *Config {
return &Config{
SMTP: &SMTP{
Delay: "5m",
},
}
}
func LoadConfig(configLocation string) (*Config, error) {
config := defaultConfig()
configYaml, err := ioutil.ReadFile(configLocation)
if err != nil {
return nil, fmt.Errorf("can't read with: %v", err)
}
err = yaml.Unmarshal(configYaml, &config)
if err != nil {
return nil, fmt.Errorf("can't parse with: %v", err)
}
pastLimit, err := helpers.ParseDuration(config.Common.HistoryPastLimitString)
if err != nil {
return nil, err
}
config.Common.HistoryPastLimit = time.Now().Add(-pastLimit)
config.Common.ScanInterval, err = helpers.ParseDuration(config.Common.ScanIntervalString)
if err != nil {
return nil, err
}
if config.Common.ScanInterval < time.Second {
return nil, fmt.Errorf("scan_interval so small")
}
return config, nil
}
func PrintDefaultConfig() {
c := defaultConfig()
d, _ := yaml.Marshal(&c)
fmt.Print(string(d))
}