-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
158 lines (128 loc) Β· 2.89 KB
/
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package pomo
import (
"bytes"
"encoding/json"
"fmt"
_fs "io/fs"
"log"
"os"
"path/filepath"
"github.com/rogpeppe/go-internal/lockedfile"
)
type Conf struct {
Id string // usually application name
Dir string // usually os.UserConfigDir
File string // usually config.yaml
}
func (c Conf) DirPath() string { return filepath.Join(c.Dir, c.Id) }
func (c Conf) Path() string { return filepath.Join(c.Dir, c.Id, c.File) }
func (c Conf) Init() error {
d := c.DirPath()
if d == "" {
return fmt.Errorf("could not resolve config path for %q", c.Id)
}
if len(c.Id) == 0 && len(c.Dir) == 0 {
return fmt.Errorf("empty directory id")
}
if err := Mkdir(d); err != nil {
return err
}
if !Exists(c.Path()) {
return WriteAppend("{}", c.Path())
}
return nil
}
func (c Conf) Data() []byte {
buf, err := os.ReadFile(c.Path())
if err != nil {
log.Println(err)
return []byte("{}")
}
return buf
}
func (c Conf) Set(key string, val any) error {
var config map[string]interface{}
if err := json.Unmarshal(c.Data(), &config); err != nil {
return err
}
// append key to json
config[key] = val
if err := c.OverWrite(config); err != nil {
return err
}
return nil
}
func (c Conf) Del(key string) error {
var config map[string]json.RawMessage
if err := json.Unmarshal(c.Data(), &config); err != nil {
return err
}
if _, exists := config[key]; exists {
delete(config, key)
if err := c.OverWrite(config); err != nil {
return err
}
}
return nil
}
func (c Conf) Query(key string) any {
var config map[string]interface{}
if err := json.Unmarshal(c.Data(), &config); err != nil {
return nil
}
val, exists := config[key]
if !exists {
return nil
}
return val
}
func (c Conf) QueryString(key string) string {
result := c.Query(key)
if val, ok := result.(string); !ok {
log.Fatalf("QueryString: %s key value is not a string", key)
} else {
return val
}
return ""
}
func (c Conf) Print() error {
var prettyJSON bytes.Buffer
if err := json.Indent(&prettyJSON, c.Data(), "", " "); err != nil {
return err
}
fmt.Println(prettyJSON.String())
return nil
}
func (c Conf) Edit() error {
if err := c.mkdir(); err != nil {
return err
}
path := c.Path()
if path == "" {
return fmt.Errorf("unable to locate config for %q", c.Id)
}
return Editor(path)
}
func (c Conf) OverWrite(newconf any) error {
buf, err := json.Marshal(newconf)
if err != nil {
return err
}
if err := c.mkdir(); err != nil {
return err
}
return lockedfile.Write(c.Path(),
bytes.NewReader(buf), _fs.FileMode(DefaultPerms))
}
// QueryPrint prints the output of Query.
func (c Conf) QueryPrint(q string) { fmt.Print(c.Query(q)) }
//////////////////////////////////////////////////////
// Private methods
//////////////////////////////////////////////////////
func (c Conf) mkdir() error {
d := c.DirPath()
if d == "" {
return fmt.Errorf("failed to find config for %q", c.Id)
}
return Mkdir(d)
}