forked from hypnoglow/dotbro
/
rc.go
58 lines (46 loc) · 1.01 KB
/
rc.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
package main
import (
"encoding/json"
"io/ioutil"
"os"
)
const rcFilepath = "${HOME}/.dotbro/profile.json"
// RC represents data for dotbro's runcom file.
type RC struct {
Config rcConfig `json:"config"`
}
// rcConfig represents "config" section of RC data
type rcConfig struct {
Path string `json:"path"`
}
// readRC reads RC from rcFilepath and returns it.
func readRC() (rc RC, err error) {
rcFile := os.ExpandEnv(rcFilepath)
bytes, err := ioutil.ReadFile(rcFile)
if os.IsNotExist(err) {
return rc, nil
} else if err != nil {
return rc, err
}
err = json.Unmarshal(bytes, &rc)
return rc, err
}
// saveRC creates RC, saves it to rcFilepath and returns it.
func saveRC(configPath string) (rc RC, err error) {
rcFile := os.ExpandEnv(rcFilepath)
err = createPath(rcFile)
if err != nil {
return rc, err
}
rc = RC{
Config: rcConfig{
Path: configPath,
},
}
bytes, err := json.MarshalIndent(rc, "", " ")
if err != nil {
return rc, err
}
ioutil.WriteFile(rcFile, bytes, 0666)
return rc, err
}