/
configs.go
71 lines (52 loc) · 1.26 KB
/
configs.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
package configs
import (
"runtime"
"strconv"
"strings"
"github.com/spf13/viper"
)
type Config struct {
Host string
Domain string
AccessToken string
}
func GetConfig() *Config {
// From the environment
viper.SetEnvPrefix("GRAVITY_CLI")
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
viper.AutomaticEnv()
// From config file
viper.SetConfigName("config")
viper.AddConfigPath("./")
viper.AddConfigPath("./configs")
viper.ReadInConfig()
runtime.GOMAXPROCS(8)
config := &Config{}
// Specify events from environment variable for watching
config.SetHost(viper.GetString("host"))
config.SetDomain(viper.GetString("domain"))
config.SetAccessToken(viper.GetString("accessToken"))
return config
}
func (config *Config) SetHost(host string) {
if len(host) == 0 {
return
}
config.Host = host
parts := strings.Split(host, ":")
viper.Set("gravity.host", parts[0])
if len(parts) == 2 {
port, err := strconv.Atoi(parts[1])
if err == nil {
viper.Set("gravity.port", port)
}
}
}
func (config *Config) SetDomain(domain string) {
config.Domain = domain
viper.Set("gravity.domain", domain)
}
func (config *Config) SetAccessToken(accessToken string) {
config.AccessToken = accessToken
viper.Set("gravity.accessToken", accessToken)
}