/
config.go
48 lines (43 loc) · 989 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
package cfg
import (
"errors"
"io/ioutil"
"os"
"path"
"gopkg.in/yaml.v2"
)
type Config struct {
HistoryFile string
}
// FromFile returns a configuration object from a given file path and errors
// if the file does not exists of it the file contexts can't be unmarshaled.
// expected format is yaml.
func FromFile(path string) (*Config, error) {
cfgfile, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
cfg := &Config{}
if err := yaml.Unmarshal(cfgfile, cfg); err != nil {
return nil, err
}
return cfg, nil
}
// ToFile marshals a configuration object to the given filepath using the yaml
// format
func ToFile(filePath string, cfg *Config) error {
b, err := yaml.Marshal(cfg)
if err != nil {
return err
}
d := path.Dir(filePath)
if _, err := os.Stat(d); errors.Is(err, os.ErrNotExist) {
if err := os.MkdirAll(d, 0755); err != nil {
return err
}
}
if err := ioutil.WriteFile(filePath, b, 0644); err != nil {
return err
}
return nil
}