-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkonf.go
50 lines (44 loc) · 1.25 KB
/
konf.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
package konf
import (
"bytes"
"io/ioutil"
"os"
"github.com/ruggi/env"
)
// EnvTag is the tag used for injecting environment variables into the configuration during Load.
var EnvTag = env.DefaultTag
// Load loads the configuration in the file specified at path into the target interface (which must be a pointer).
// It also injects environment variables into it, if any match is found. See github.com/ruggi/env for more.
func Load(path string, target interface{}) error {
formatter, err := parsePath(path)
if err != nil {
return err
}
data, err := ioutil.ReadFile(path)
if err != nil {
return err
}
return load(formatter, data, target)
}
func load(formatter formatter, data []byte, target interface{}) error {
r := bytes.NewReader(data)
if err := formatter.Decode(r, target); err != nil {
return err
}
env.ParseInto(target)
return nil
}
// Save saves the given interface v to the file at the given path, using the format obtained from the path's extension,
// if supported.
func Save(path string, v interface{}) error {
formatter, err := parsePath(path)
if err != nil {
return err
}
file, err := os.OpenFile(path, os.O_TRUNC|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return err
}
defer file.Close()
return formatter.Encode(file, v)
}