-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
61 lines (53 loc) · 1.28 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
package core
import (
"log"
"os"
"github.com/mitchellh/mapstructure"
"github.com/spf13/viper"
)
type Config struct {
DoLogs bool
ReadSaved bool
}
var DefaultConfig Config = Config{
DoLogs: false,
ReadSaved: true,
}
// read tunnels from $HOME/.gopolar/tunnels.toml
// create it if not exist
func readTunnels() []Tunnel {
cfgDir := homeDir + "/.gopolar/"
cfgPath := cfgDir + "tunnels.toml"
viper.SetConfigName("tunnels")
viper.AddConfigPath(cfgDir)
if err := viper.ReadInConfig(); err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); ok { // config file not found, create it and try again
err = os.MkdirAll(cfgDir, 0700)
if err != nil {
log.Fatalln("fail to create config directory: " + cfgDir)
}
f, err := os.Create(cfgPath)
f.Close()
if err != nil {
log.Fatalln("fail to create config file:", err)
}
return nil
} else {
log.Fatalln("fail to read config file:", err)
}
}
// read tunnels from config file
ts, ok := viper.Get("tunnels").([]interface{})
ret := []Tunnel{}
if ok {
for _, t := range ts {
ti := t.(map[string]interface{})
res := Tunnel{}
if err := mapstructure.Decode(ti, &res); err != nil {
log.Fatal("fail to parse config file:", err)
}
ret = append(ret, res)
}
} // else ts is nil
return ret
}