forked from codeskyblue/gosuv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
49 lines (43 loc) · 1.02 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
package main
import (
"io/ioutil"
"os"
"path/filepath"
"github.com/go-yaml/yaml"
)
type Configuration struct {
Server struct {
HttpAuth struct {
Enabled bool `yaml:"enabled"`
User string `yaml:"username"`
Password string `yaml:"password"`
} `yaml:"httpauth"`
Addr string `yaml:"addr"`
Name string `yaml:"name"`
Master string `yaml:"master"`
} `yaml:"server,omitempty"`
Notifications Notifications `yaml:"notifications,omitempty" json:"-"`
Client struct {
ServerURL string `yaml:"server_url"`
}
}
func readConf(filename string) (c Configuration, err error) {
// initial default value
c.Server.Addr = ":11313" // in memory of 08-31 13:13
c.Client.ServerURL = "http://localhost:11313"
data, err := ioutil.ReadFile(filename)
if err != nil {
data = []byte("")
}
err = yaml.Unmarshal(data, &c)
if err != nil {
return
}
cfgDir := filepath.Dir(filename)
if !IsDir(cfgDir) {
os.MkdirAll(cfgDir, 0755)
}
data, _ = yaml.Marshal(c)
err = ioutil.WriteFile(filename, data, 0644)
return
}