forked from plimble/goconf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
goconf.go
59 lines (48 loc) · 989 Bytes
/
goconf.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
package goconf
import (
"io/ioutil"
"github.com/kelseyhightower/envconfig"
"gopkg.in/yaml.v2"
)
func Parse(v interface{}, options ...Option) error {
var err error
o := &opts{}
for _, option := range options {
option(o)
}
if o.yaml {
if err = parseYaml(v, o); err != nil {
return err
}
}
if o.env {
if err = envconfig.Process(o.envPrefix, v); err != nil {
return err
}
}
return nil
}
func ReloadYaml(path string, v interface{}) error {
yamlBytes, err := ioutil.ReadFile(path)
if err != nil {
return err
}
return yaml.Unmarshal(yamlBytes, v)
}
func ReloadYamlFromBytes(bytes []byte, v interface{}) error {
return yaml.Unmarshal(bytes, v)
}
func parseYaml(v interface{}, o *opts) error {
var err error
if o.yamlBytes != nil {
err = yaml.Unmarshal(o.yamlBytes, v)
}
if err == nil && o.yamlPath != "" {
yamlBytes, err := ioutil.ReadFile(o.yamlPath)
if err != nil {
return err
}
return yaml.Unmarshal(yamlBytes, v)
}
return err
}