/
config.go
132 lines (106 loc) · 2.79 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
package main
import (
"flag"
"io/ioutil"
"os"
"github.com/BurntSushi/toml"
log "github.com/Sirupsen/logrus"
)
var (
config Config // holds the global config
c struct {
logLevel string
eventsocket struct {
port uint
}
harmony struct {
api string
verifyssl bool
}
}
configFile = ""
defaultConfigFile = "config.toml"
printVersion bool
)
func init() {
flag.StringVar(&configFile, "configFile", "", "the config file")
flag.BoolVar(&printVersion, "version", false, "print version and exit")
flag.StringVar(&c.logLevel, "logLevel", "", "the level of messages to log")
flag.StringVar(&c.harmony.api, "harmony.api", "http://harmony.dev:4774", "the url to the Harmony API")
flag.BoolVar(&c.harmony.verifyssl, "harmony.verifyssl", true, "verify ssl connections to the harmony api")
flag.UintVar(&c.eventsocket.port, "port", 4775, "port for eventsocket to listen on")
}
// Config is the main config type
type Config struct {
// LogLevel main application loggin level
LogLevel string `toml:"LogLevel"`
// Harmony is the main Harmony config
Harmony HarmonyConfig `toml:"Harmony"`
// Eventsocket is the main Harmony config
Eventsocket EventsocketConfig `toml:"Eventsocket"`
}
// HarmonyConfig is the main eventsocket config
type HarmonyConfig struct {
// API url to the Harmony API
API string `toml:"API"`
// VerifySSL is wether ot not we are to verify the secure Harmony API connections
VerifySSL bool `toml:"VerifySSL"`
}
// EventsocketConfig is the main eventsocket config
type EventsocketConfig struct {
// Port to listen on
Port uint `toml:"Port"`
}
func initConfig() error {
if configFile == "" {
if _, err := os.Stat(defaultConfigFile); !os.IsNotExist(err) {
configFile = defaultConfigFile
}
}
// Set defaults.
config = Config{
LogLevel: "info",
Harmony: HarmonyConfig{
API: "http://harmony.dev:4774",
VerifySSL: true,
},
Eventsocket: EventsocketConfig{
Port: 4775,
},
}
// Update config from the TOML configuration file.
if configFile == "" {
log.Info("Skipping config file parsing")
} else {
log.WithField("file", configFile).Info("Loading config")
configBytes, err := ioutil.ReadFile(configFile)
if err != nil {
return err
}
_, err = toml.Decode(string(configBytes), &config)
if err != nil {
return err
}
}
// Update config from commandline flags.
processFlags()
if config.LogLevel != "" {
LogSetLevel(config.LogLevel)
}
return nil
}
func processFlags() {
flag.Visit(setConfigFromFlag)
}
func setConfigFromFlag(f *flag.Flag) {
switch f.Name {
case "logLevel":
config.LogLevel = c.logLevel
case "harmony.api":
config.Harmony.API = c.harmony.api
case "harmony.verifyssl":
config.Harmony.VerifySSL = c.harmony.verifyssl
case "port":
config.Eventsocket.Port = c.eventsocket.port
}
}