-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathconfig.go
136 lines (112 loc) · 2.73 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
/*
Copyright 2020 Adevinta
*/
package main
import (
"io"
"os"
log "github.com/sirupsen/logrus"
"github.com/BurntSushi/toml"
)
type config struct {
MaxEventAge int `toml:"max_event_age"`
Log logConfig
DB dbConfig
SQS sqsConfig
SNS snsConfig
Report reportConfig
Maintenance maintenanceConfig
}
type logConfig struct {
Level string
}
type dbConfig struct {
Dialect string
Host string `toml:"host"`
Port string `toml:"port"`
SSLMode string `toml:"sslmode"`
User string `toml:"user"`
Pass string `toml:"password"`
Name string `toml:"name"`
}
type sqsConfig struct {
NProcessors uint8 `toml:"number_of_processors"`
WaitTime int `toml:"wait_time"`
Timeout int
QueueARN string `toml:"queue_arn"`
Endpoint string `toml:"endpoint"`
}
type snsConfig struct {
TopicARN string `toml:"topic_arn"`
Enabled bool
Endpoint string `toml:"endpoint"`
}
type reportConfig struct {
URLReplace string `toml:"url_replace"`
}
type maintenanceConfig struct {
Tasks []maintenanceTaskConfig
}
type maintenanceTaskConfig struct {
Name string
Type string
Rate int
Options interface{}
}
func parseConfig(cfgFilePath string) (*config, error) {
cfgFile, err := os.Open(cfgFilePath)
if err != nil {
return nil, err
}
defer cfgFile.Close()
cfgData, err := io.ReadAll(cfgFile)
var conf config
if _, err := toml.Decode(string(cfgData[:]), &conf); err != nil {
return nil, err
}
if envVar := os.Getenv("VULNERABILITYDBCONSUMER_DB_HOST"); envVar != "" {
conf.DB.Host = envVar
}
if envVar := os.Getenv("VULNERABILITYDBCONSUMER_DB_PORT"); envVar != "" {
conf.DB.Port = envVar
}
if envVar := os.Getenv("VULNERABILITYDBCONSUMER_DB_USER"); envVar != "" {
conf.DB.User = envVar
}
if envVar := os.Getenv("VULNERABILITYDBCONSUMER_DB_NAME"); envVar != "" {
conf.DB.Name = envVar
}
if envVar := os.Getenv("VULNERABILITYDBCONSUMER_SQS_QUEUE_ARN"); envVar != "" {
conf.SQS.QueueARN = envVar
}
if envVar := os.Getenv("VULNERABILITYDBCONSUMER_SNS_TOPIC_ARN"); envVar != "" {
conf.SNS.TopicARN = envVar
}
if envVar := os.Getenv("VULNERABILITYDBCONSUMER_REPORT_URL_RPLC"); envVar != "" {
conf.Report.URLReplace = envVar
}
return &conf, nil
}
// parseLogLevel parses a configured string log level
// and returns the correspondent logrus log level.
// If log level is invalid, default level is Info.
func parseLogLevel(logLevel string) log.Level {
switch logLevel {
case "panic":
return log.PanicLevel
case "fatal":
return log.FatalLevel
case "error":
return log.ErrorLevel
case "warn":
return log.WarnLevel
case "info":
return log.InfoLevel
case "debug":
return log.DebugLevel
case "trace":
return log.TraceLevel
default:
return log.InfoLevel
}
}