-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
80 lines (71 loc) · 1.84 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
package main
import (
"fmt"
"os"
"path"
"github.com/BurntSushi/toml"
"github.com/mitchellh/go-homedir"
)
// Config File stuff
const confFile string = "config.toml"
const confCommonEnv string = "common"
type tomlConfig struct {
Environments map[string]environmentCfg
}
type environmentCfg struct {
APIURL string `toml:"api_url"`
APIUser string `toml:"api_user"`
APIPasswd string `toml:"api_passwd"`
}
var env environmentCfg
func initializeConfig() {
//
// read config from config file
//
realConfFile := confFile
confPath, err := homedir.Dir()
if err != nil {
log.Fatal(err)
}
if _, err := os.Stat(path.Join(confPath, fmt.Sprintf(".%s", confFile))); os.IsNotExist(err) {
log.Info("%s/.%s does not exist, trying ./%s", confPath, confFile, confFile)
if _, err := os.Stat(confFile); os.IsNotExist(err) {
log.Critical("%s does not exist./%s", confFile)
os.Exit(1)
}
} else {
realConfFile = path.Join(confPath, fmt.Sprintf(".%s", confFile))
}
var config tomlConfig
if _, err := toml.DecodeFile(realConfFile, &config); err != nil {
log.Critical("%s", err)
os.Exit(1)
}
var found bool
envs := make([]string, len(config.Environments))
i := 0
for e := range config.Environments {
envs[i] = e
if e == *environment {
found = true
}
i++
}
if !found {
log.Critical("Unknown environment. Did you mean one of %s", envs)
os.Exit(1)
}
env = config.Environments[*environment]
c := config.Environments[confCommonEnv]
log.Info("Requested Environment: %s, URL: %s, User: %s, Password: %s\n", *environment, env.APIURL, env.APIUser, env.APIPasswd)
log.Info("Common Environment: %s, URL: %s, User: %s, Password: %s\n", confCommonEnv, c.APIURL, c.APIUser, c.APIPasswd)
if env.APIURL == "" {
env.APIURL = c.APIURL
}
if env.APIUser == "" {
env.APIUser = c.APIUser
}
if env.APIPasswd == "" {
env.APIPasswd = c.APIPasswd
}
}