-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconfig.go
85 lines (71 loc) · 1.93 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
package main
import (
"flag"
"fmt"
"log"
"os"
"github.com/ghodss/yaml"
"github.com/criteo/consul-timeline/consul"
"github.com/criteo/consul-timeline/server"
cass "github.com/criteo/consul-timeline/storage/cassandra"
"github.com/criteo/consul-timeline/storage/memory"
"github.com/criteo/consul-timeline/storage/mysql"
)
type Config struct {
LogLevel string `json:"log_level"`
Storage string `json:"storage"`
Consul consul.Config `json:"consul"`
Server server.Config `json:"server"`
Mysql mysql.Config `json:"mysql"`
Cassandra cass.Config `json:"cassandra"`
Memory memory.Config `json:"memory"`
}
var DefaultConfig = Config{
LogLevel: "info",
Storage: memory.Name,
}
var (
logLevelFlag = flag.String("log-level", DefaultConfig.LogLevel, "(debug, info, warning, error, fatal)")
configFileFlag = flag.String("config", "", "Config file path (yaml, json)")
storageFlag = flag.String("storage", DefaultConfig.Storage, "Storage backend (mysql, cassandra, memory)")
printConfigFlag = flag.Bool("print-config", false, "Print the configuration")
)
func FromFlags() Config {
cfg := Config{
LogLevel: *logLevelFlag,
Storage: *storageFlag,
Consul: consul.ConfigFromFlags(),
Server: server.ConfigFromFlags(),
Mysql: mysql.ConfigFromFlags(),
Cassandra: cass.ConfigFromFlags(),
Memory: memory.ConfigFromFlags(),
}
return cfg
}
func GetConfig() Config {
flag.Parse()
cfg := DefaultConfig
cfg.Consul = consul.DefaultConfig
cfg.Server = server.DefaultConfig
cfg.Mysql = mysql.DefaultConfig
cfg.Cassandra = cass.DefaultConfig
cfg.Memory = memory.DefaultConfig
if *configFileFlag != "" {
f, err := os.ReadFile(*configFileFlag)
if err != nil {
log.Fatal(err)
}
err = yaml.Unmarshal(f, &cfg)
if err != nil {
log.Fatal(err)
}
} else {
cfg = FromFlags()
}
if *printConfigFlag {
b, _ := yaml.Marshal(cfg)
fmt.Println(string(b))
os.Exit(0)
}
return cfg
}