-
Notifications
You must be signed in to change notification settings - Fork 3
/
config.go
65 lines (52 loc) · 1.39 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
package main
import (
"io/ioutil"
"log"
"github.com/spf13/viper"
yaml "gopkg.in/yaml.v2"
"daqnext/meson-cloud-client/daemon"
)
type AppCfg struct {
Version string `yaml:"version"`
Token string `yaml:"token"`
QueryUrl string `yaml:"queryUrl"`
LogLevel string `yaml:"logLevel"`
Ipfs daemon.IpfsCfg `yaml:"ipfs"`
}
type AppConfig struct {
cfg *AppCfg
usedFile string
v *viper.Viper
}
func loadConfig(extPath string) *AppConfig {
// Read Config
v := viper.New()
v.SetConfigName("config") // name of config file (without extension)
v.SetConfigType("yaml") // REQUIRED if the config file does not have the extension in the name
v.AddConfigPath(".") // path to look for the config file in
v.AddConfigPath(extPath) // path to look for the config file in
//viper.AddConfigPath("$HOME/.appname") // call multiple times to add many search paths
if err := v.ReadInConfig(); err != nil {
log.Panicln("Failed to read the config", err.Error())
}
usedFile := v.ConfigFileUsed()
var C AppCfg
if err := v.Unmarshal(&C); err != nil {
log.Panicln("Unable to decode into struct", err.Error())
}
return &AppConfig{
v: v,
usedFile: usedFile,
cfg: &C,
}
}
func (c *AppConfig) updateConfig() {
d, err := yaml.Marshal(c.cfg)
if err != nil {
log.Fatalf("error: %v", err)
}
err = ioutil.WriteFile(c.usedFile, d, 0644)
if err != nil {
log.Fatal(err)
}
}