/
config.go
65 lines (57 loc) · 1.47 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
package util
import (
"flag"
"github.com/BurntSushi/toml"
log "github.com/sirupsen/logrus"
)
var (
// Configuration is main conig
Configuration TomlConfig
)
// TomlConfig is the main config struct
type TomlConfig struct {
Global struct {
Port string `toml:"port"`
Timeout int `toml:"timeout"`
} `toml:"global"`
Log struct {
Format string `toml:"format"`
Level string `toml:"level"`
} `toml:"log"`
Kafka struct {
DefaultRefreshRate int `toml:"default_refresh_rate"`
MinKafkaVersion string `toml:"min_kafka_version"`
AdminTimeout int `toml:"admin_timeout"`
} `toml:"kafka"`
Clusters map[string]clusters
}
type clusters struct {
Brokers []string
TopicFilter string
}
func init() {
Configuration = initConfig()
}
// initConfig load the config from path, parse it and return TomlConfig
func initConfig() TomlConfig {
log.Debug("Config init")
// Parse the user input for a configuration file
configPath := flag.String("config", "/opt/kcm/kcm.toml", "Absolute path to the configuration file")
flag.Parse()
log.Infof("Starting from config file:%v", *configPath)
var config TomlConfig
if _, err := toml.DecodeFile(*configPath, &config); err != nil {
log.Panicf("Unble to load config file.\n%v", err)
}
// Set custom defaults
if config.Kafka.AdminTimeout == 0 {
config.Kafka.AdminTimeout = 5
}
if config.Global.Port == "" {
config.Global.Port = ":9899"
}
if config.Global.Timeout == 0 {
config.Global.Timeout = 3
}
return config
}