forked from projecteru2/agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
102 lines (90 loc) · 2.4 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
package types
import (
"io/ioutil"
"os"
log "github.com/Sirupsen/logrus"
"gopkg.in/urfave/cli.v2"
yaml "gopkg.in/yaml.v2"
)
type DockerConfig struct {
Endpoint string `yaml:"endpoint"`
}
type MetricsConfig struct {
Step int64 `yaml:"step"`
Transfers []string `yaml:"transfers"`
}
type APIConfig struct {
Addr string `yaml:"addr"`
}
type LogConfig struct {
Forwards []string `yaml:"forwards"`
Stdout bool `yaml:"stdout"`
}
type Config struct {
PidFile string `yaml:"pid"`
HealthCheckInterval int `yaml:"health_check_interval"`
HealthCheckTimeout int `yaml:"health_check_timeout"`
Core string `yaml:"core"`
HostName string `yaml:"-"`
Docker DockerConfig
Metrics MetricsConfig
API APIConfig
Log LogConfig
}
//LoadConfigFromFile 从config path指定的文件加载config
//失败就算了, 反正也要从cli覆写的
func (config *Config) LoadConfigFromFile(configPath string) error {
bytes, err := ioutil.ReadFile(configPath)
if err != nil {
return err
}
return yaml.Unmarshal(bytes, config)
}
//PrepareConfig 从cli覆写并做准备
func (config *Config) PrepareConfig(c *cli.Context) {
hostname, err := os.Hostname()
if err != nil {
log.Fatal(err)
}
config.HostName = hostname
if c.String("core-endpoint") != "" {
config.Core = c.String("core-endpoint")
}
if c.String("pidfile") != "" {
config.PidFile = c.String("pidfile")
}
if c.Int("health-check-interval") > 0 {
config.HealthCheckInterval = c.Int("health-check-interval")
}
if c.Int("health-check-timeout") > 0 {
config.HealthCheckTimeout = c.Int("health-check-timeout")
}
if c.String("docker-endpoint") != "" {
config.Docker.Endpoint = c.String("docker-endpoint")
}
if c.Int64("metrics-step") > 0 {
config.Metrics.Step = c.Int64("metrics-step")
}
if len(c.StringSlice("metrics-transfers")) > 0 {
config.Metrics.Transfers = c.StringSlice("metrics-transfers")
}
if c.String("api-addr") != "" {
config.API.Addr = c.String("api-addr")
}
if len(c.StringSlice("log-forwards")) > 0 {
config.Log.Forwards = c.StringSlice("log-forwards")
}
if c.String("log-stdout") != "" {
config.Log.Stdout = c.String("log-stdout") == "yes"
}
//validate
if config.PidFile == "" {
log.Fatal("need to set pidfile")
}
if config.HealthCheckTimeout == 0 {
config.HealthCheckTimeout = 3
}
if config.HealthCheckInterval == 0 {
config.HealthCheckInterval = 10
}
}