-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.go
194 lines (174 loc) · 3.35 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package main
import (
"fmt"
"log"
"os"
"strconv"
"strings"
"sync"
"gopkg.in/yaml.v3"
)
const (
DEFAULT_PORT = 7837
DEFAULT_API_URL = "https://srink.co"
)
type config struct {
confPath string
mu sync.RWMutex
data map[string]any
log *log.Logger
}
func newConfig(confPath string, l *log.Logger) *config {
return &config{
confPath: confPath,
data: make(map[string]any),
log: l,
}
}
// tryAdd adds the key if it doesn't exist already
func (c *config) tryAdd(key string, value any) {
if _, ok := c.get(key); ok {
return
}
c.add(key, value)
}
func (c *config) add(key string, value any) {
c.mu.Lock()
defer c.mu.Unlock()
c.data[key] = value
}
func (c *config) get(key string) (value any, ok bool) {
c.mu.RLock()
value, ok = c.data[key]
c.mu.RUnlock()
return
}
func (c *config) getString(key string) string {
v, ok := c.get(key)
if !ok {
return ""
}
switch val := v.(type) {
case string:
return val
case int64:
return strconv.FormatInt(val, 10)
case int:
return strconv.FormatInt(int64(val), 10)
default:
return fmt.Sprint(val)
}
}
func (c *config) getInt(key string) int {
v, ok := c.get(key)
if !ok {
return 0
}
switch val := v.(type) {
case int:
return val
case string:
value, _ := strconv.Atoi(val)
return value
case int64:
// might be corrupted
return int(val)
default:
return 0
}
}
func (c *config) getInt64(key string) int64 {
v, ok := c.get(key)
if !ok {
return 0
}
switch val := v.(type) {
case int64:
return val
case int:
return int64(val)
case string:
value, _ := strconv.ParseInt(val, 10, 64)
return value
default:
return 0
}
}
func (c *config) getBool(key string) bool {
v, ok := c.get(key)
if !ok {
return false
}
switch val := v.(type) {
case bool:
return val
case string:
value, _ := strconv.ParseBool(val)
return value
default:
return false
}
}
func (c *config) init(in []byte) error {
return yaml.Unmarshal(in, c.data)
}
func (c *config) build() []byte {
buf, err := yaml.Marshal(c.data)
if err != nil {
c.log.Fatalln("Failed to build config:", err)
}
return buf
}
func (c *config) write() {
err := os.WriteFile(
c.confPath, c.build(), os.ModePerm,
)
if err != nil {
c.log.Fatalln("Failed to write config:", err)
}
}
func getUserConfigDir() string {
dir, err := os.UserConfigDir()
if err != nil {
log.Fatalln("Failed to read user_config_dir:", err)
}
return dir
}
func getPath(fs ...string) string {
return strings.Join(fs, "/")
}
func isDirExist(name string) bool {
_, err := os.ReadDir(name)
return !os.IsNotExist(err)
}
func checkConfigDir(userConfDir string) {
confDir := getPath(userConfDir, "srink")
if !isDirExist(confDir) {
err := os.Mkdir(confDir, os.ModePerm)
if err != nil {
log.Fatalln("Failed to create config dir:", err)
}
log.Println("Created new config dir at", confDir)
}
}
func readUserConfig(name string, l *log.Logger) *config {
userConfDir := getUserConfigDir()
checkConfigDir(userConfDir)
fPath := getPath(userConfDir, "srink", name)
conf := newConfig(fPath, l)
buf, err := os.ReadFile(fPath)
if err != nil {
log.Println("Failed to read conf file:", err)
if os.IsNotExist(err) {
log.Println("Creating a new", name, "in config dir")
conf.write()
} else {
os.Exit(1)
}
}
err = conf.init(buf)
if err != nil {
log.Fatalln("Failed to initialise config:", err)
}
return conf
}