This repository has been archived by the owner on Sep 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconfig.go
133 lines (116 loc) · 2.96 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package main
import (
"flag"
"fmt"
"io/ioutil"
"os"
"strconv"
"github.com/BurntSushi/toml"
log "github.com/Sirupsen/logrus"
)
type Config struct {
Interval int `toml:"interval"`
MetadataVersion string `toml:"metadata-version"`
LogLevel string `toml:"log-level"`
OneTime bool `toml:"onetime"`
IncludeInactive bool `toml:"include-inactive"`
Templates []Template `toml:"template"`
}
type Template struct {
Source string `toml:"source"`
Dest string `toml:"dest"`
CheckCmd string `toml:"check-cmd"`
NotifyCmd string `toml:"notify-cmd"`
NotifyLbl string `toml:"notify-lbl"`
NotifyOutput bool `toml:"notify-output"`
Staging string ""
}
func initConfig() (*Config, error) {
config := Config{
MetadataVersion: "latest",
Interval: 5,
LogLevel: "info",
}
if len(configFile) > 0 {
log.Debugf("Loading config from file %s", configFile)
err := setConfigFromFile(configFile, &config)
if err != nil {
return nil, fmt.Errorf("Could not load config file: %v", err)
}
} else {
setTemplateFromFlags(&config)
}
overwriteConfigFromEnv(&config)
overwriteConfigFromFlags(&config)
if config.Interval == 0 {
return nil, fmt.Errorf("Interval must be greater than 0")
}
lvl, err := log.ParseLevel(config.LogLevel)
if err != nil {
return nil, fmt.Errorf("Invalid log level: %s", config.LogLevel)
}
log.SetLevel(lvl)
return &config, nil
}
func setConfigFromFile(path string, conf *Config) error {
buf, err := ioutil.ReadFile(path)
if err != nil {
return err
}
_, err = toml.Decode(string(buf), conf)
if err != nil {
return err
}
return nil
}
func setTemplateFromFlags(conf *Config) {
tmpl := Template{
Source: flag.Arg(0),
Dest: flag.Arg(1),
CheckCmd: checkCmd,
NotifyCmd: notifyCmd,
NotifyLbl: notifyLbl,
NotifyOutput: notifyOutput,
Staging: "",
}
conf.Templates = []Template{tmpl}
}
func overwriteConfigFromFlags(conf *Config) {
flag.Visit(func(f *flag.Flag) {
switch f.Name {
case "interval":
conf.Interval = interval
case "metadata-version":
conf.MetadataVersion = metadataVersion
case "onetime":
conf.OneTime = onetime
case "include-inactive":
conf.IncludeInactive = includeInactive
case "log-level":
conf.LogLevel = logLevel
}
})
}
func overwriteConfigFromEnv(conf *Config) {
var env string
if env = os.Getenv("RANCHER_GEN_LOGLEVEL"); len(env) > 0 {
conf.LogLevel = env
}
if env = os.Getenv("RANCHER_GEN_INTERVAL"); len(env) > 0 {
interval, err := strconv.Atoi(env)
if err != nil {
conf.Interval = interval
} else {
log.Warnf("Invalid value for environment variable 'RANCHER_GEN_INTERVAL': %s", env)
}
}
if env = os.Getenv("RANCHER_GEN_METADATA_VER"); len(env) > 0 {
conf.MetadataVersion = env
}
if env = os.Getenv("RANCHER_GEN_ONETIME"); len(env) > 0 {
conf.OneTime = true
}
if env = os.Getenv("RANCHER_GEN_INACTIVE"); len(env) > 0 {
conf.IncludeInactive = true
}
}