forked from QubitProducts/bamboo
/
configuration.go
64 lines (52 loc) · 1.45 KB
/
configuration.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
package configuration
import (
"encoding/json"
"io/ioutil"
"log"
"os"
)
var logger = log.New(os.Stdout, "", log.Ldate|log.Ltime|log.Lshortfile)
/*
Service configuration struct
*/
type Configuration struct {
// Marathon configuration
Marathon Marathon
// Bamboo specific configuration
Bamboo Bamboo
// HAProxy output configuration
HAProxy HAProxy
// StatsD configuration
StatsD StatsD
}
/*
Returns Configuration struct from a given file path
Parameters:
filePath: full file path to the JSON configuration
*/
func (config *Configuration) FromFile(filePath string) error {
content, err := ioutil.ReadFile(filePath)
if err != nil {
panic(err)
}
return json.Unmarshal(content, &config)
}
func FromFile(filePath string) (Configuration, error) {
conf := &Configuration{}
err := conf.FromFile(filePath)
setValueFromEnv(&conf.Marathon.Endpoint, "MARATHON_ENDPOINT")
setValueFromEnv(&conf.Bamboo.Endpoint, "BAMBOO_ENDPOINT")
setValueFromEnv(&conf.Bamboo.Zookeeper.Host, "BAMBOO_ZK_HOST")
setValueFromEnv(&conf.Bamboo.Zookeeper.Path, "BAMBOO_ZK_PATH")
setValueFromEnv(&conf.HAProxy.TemplatePath, "HAPROXY_TEMPLATE_PATH")
setValueFromEnv(&conf.HAProxy.OutputPath, "HAPROXY_OUTPUT_PATH")
setValueFromEnv(&conf.HAProxy.ReloadCommand, "HAPROXY_RELOAD_CMD")
return *conf, err
}
func setValueFromEnv(field *string, envVar string) {
env := os.Getenv(envVar)
if len(env) > 0 {
log.Printf("Using environment override %s=%s", envVar, env)
*field = env
}
}