-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.go
100 lines (85 loc) · 1.91 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
/*
Copyright 2021 Adevinta
*/
package main
import (
"io/ioutil"
"os"
"github.com/BurntSushi/toml"
log "github.com/sirupsen/logrus"
"github.com/adevinta/vulcan-reports-generator/pkg/notify"
"github.com/adevinta/vulcan-reports-generator/pkg/queue"
)
type config struct {
Log logConfig
API apiConfig
DB dbConfig
SQS sqsConfig
S3 s3Config `toml:"aws3"`
SES notify.SESConfig
Generators map[string]interface{}
}
type logConfig struct {
Level string
AddCaller bool `toml:"add_caller"`
}
type apiConfig struct {
Port int `toml:"port"`
}
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 {
queue.SQSConfig
NProcessors uint8 `toml:"number_of_processors"`
}
type s3Config struct {
Region string `toml:"region"`
Endpoint string `toml:"endpoint"`
PathStyle bool `toml:"path_style"`
}
func parseConfig(cfgFilePath string) (*config, error) {
cfgFile, err := os.Open(cfgFilePath)
if err != nil {
return nil, err
}
defer cfgFile.Close()
cfgData, err := ioutil.ReadAll(cfgFile)
if err != nil {
return nil, err
}
var conf config
if _, err := toml.Decode(string(cfgData[:]), &conf); err != nil {
return nil, err
}
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
}
}