-
Notifications
You must be signed in to change notification settings - Fork 12
/
config.go
54 lines (47 loc) · 895 Bytes
/
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
package config
import (
"io"
"os"
"path/filepath"
"gopkg.in/yaml.v2"
)
// LoadFile ...
func LoadFile(path string) *Config {
fp, err := os.Open(path)
if err != nil {
cfg := DefaultConfig()
return &cfg
}
defer fp.Close()
c := load(fp)
c.originalFilePath = path
return c
}
func SaveFile(config *Config, path string) {
fp, err := os.CreateTemp(filepath.Dir(path), `taoblog-config-*.yaml`)
if err != nil {
panic(err)
}
if err := yaml.NewEncoder(fp).Encode(config); err != nil {
fp.Close()
os.Remove(fp.Name())
panic(err)
}
fp.Close()
if err := os.Rename(fp.Name(), path); err != nil {
panic(err)
}
}
// Load ...
func load(r io.Reader) *Config {
c := DefaultConfig()
dec := yaml.NewDecoder(r)
dec.SetStrict(true)
err := dec.Decode(&c)
if err != nil {
panic(err)
}
c.Auth.AdminName = c.Comment.Author
c.Auth.AdminEmails = c.Comment.Emails
return &c
}