-
Notifications
You must be signed in to change notification settings - Fork 0
/
viper.go
113 lines (104 loc) · 2.7 KB
/
viper.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
package config
import (
"fmt"
"github.com/fsnotify/fsnotify"
"github.com/parvez0/wabacli/log"
"github.com/parvez0/wabacli/pkg/errutil/handler"
"github.com/spf13/viper"
"os"
"sync"
)
const (
DefaultCurrentContext = "default"
DefaultServer = "https://localhost"
)
var (
once sync.Once
config *Configuration
vp *viper.Viper
)
// TODO add auth expiry time to config
// New provides a singleton for creating the configuration
// Once handles the cases where multiple routines are trying
// to initialize the config file
func GetConfig() (c *Configuration, err error) {
if config != nil {
return config, nil
}
once.Do(func() {
c, err = initializeConfig()
})
return
}
// createConfig will helps in creating the config
// file if it doesn't exits. it initializes a empty
// config file which can used to write the data of
// the accounts which will be added
func createConfig() {
cp := "/etc/wabacli"
home, err := os.UserHomeDir()
if err != nil {
log.Debug(fmt.Sprintf("home directory not found %s. using '%s/config' directory as default config path", err.Error(), cp))
} else {
cp = home + "/.waba"
}
err = os.Mkdir(cp, 0700)
if ex := os.IsNotExist(err); ex {
handler.FatalError(fmt.Errorf("failed to create config file at '%s'; %s", cp, err.Error()))
}
_, err = os.Stat(cp + "/config")
if err != nil {
log.Debug("creating config file at :", cp)
_, err = os.Create(cp + "/config")
if err != nil {
handler.FatalError(fmt.Errorf("failed to create config file at '%s'; %s", cp, err.Error()))
}
}
}
// initializeConfig will read the config file from home directory
// and decodes it into Configuration structure
func initializeConfig() (*Configuration, error) {
vp = viper.New()
vp.SetConfigName("config")
vp.SetConfigType("yaml")
vp.AddConfigPath("$HOME/.waba/")
vp.AddConfigPath("/etc/wabacli")
vp.AddConfigPath(".")
vp.AutomaticEnv()
vp.SetDefault("clusters", []Cluster{
{
Auth: "",
Server: DefaultServer,
Name: "Default Server",
Number: 987654321,
Context: DefaultCurrentContext,
Insecure: true,
},
})
vp.SetDefault("current_context", DefaultCurrentContext)
if err := vp.ReadInConfig(); err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
createConfig()
} else {
return nil, err
}
}
err := vp.Unmarshal(&config)
if err != nil {
return config, err
}
if config.CurrentContext != config.CurrentCluster.Context {
for _, v := range config.Clusters {
if v.Context == config.CurrentContext {
config.CurrentCluster = v
break
}
}
}
vp.WatchConfig()
vp.OnConfigChange(func(in fsnotify.Event) {
log.Debug("config change detected: ", in.Name, in.Op.String(), ", updating to latest")
_ = vp.WriteConfig()
})
return config, nil
}