-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
74 lines (61 loc) · 1.56 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
package main
import (
"flag"
"io/ioutil"
"os"
log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v3"
)
type Project struct {
ID int `yaml:"id"`
Path string `yaml:"path"`
}
type Config struct {
GitLab struct {
Url string `yaml:"url"`
AccessToken string `yaml:"access_token"`
Projects []*Project `yaml:"projects"`
} `yaml:"gitlab"`
PollIntervalS int `yaml:"poll_interval_s"`
LoggingLevel string
}
var config Config
func init() {
var configFilePath string
flag.StringVar(&configFilePath, "config", "config.yaml", "config file absolute path")
flag.Parse()
data, err := ioutil.ReadFile(configFilePath)
if err != nil {
log.Panicln("failed to read config.yaml file")
}
err = yaml.Unmarshal(data, &config)
if err != nil {
log.Panicln("failed to parse config")
}
if accessToken := os.Getenv("GITLAB_ACCESS_TOKEN"); len(accessToken) > 0 {
config.GitLab.AccessToken = accessToken
}
if loggingLevel := os.Getenv("LOGGING_LEVEL"); len(loggingLevel) > 0 {
setupLoggingLevel(loggingLevel)
}
}
func setupLoggingLevel(l string) {
switch l {
case "panic", "Panic", "PANIC":
log.SetLevel(log.PanicLevel)
case "fatal", "Fatal", "FATAL":
log.SetLevel(log.FatalLevel)
case "error", "Error", "ERROR":
log.SetLevel(log.ErrorLevel)
case "warn", "Warn", "WARN":
log.SetLevel(log.WarnLevel)
case "info", "Info", "INFO":
log.SetLevel(log.InfoLevel)
case "debug", "Debug", "DEBUG":
log.SetLevel(log.DebugLevel)
case "trace", "Trace", "TRACE":
log.SetLevel(log.TraceLevel)
default:
log.Panicln("unknown logging level:", l)
}
}