-
Notifications
You must be signed in to change notification settings - Fork 0
/
store.go
308 lines (262 loc) · 6.32 KB
/
store.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
package SimpleStorage
import (
"encoding/json"
"errors"
"fmt"
"github.com/cstockton/go-conv"
"github.com/fsnotify/fsnotify"
"io/ioutil"
"log"
"os"
"os/user"
"strconv"
"sync"
)
type Config struct {
StorageDir string
}
type Storage struct {
StorageFile string
lock *sync.Mutex
data map[string]string
notify []chan struct{}
}
func NewSimpleStorage(name string, conf *Config) *Storage {
s := Storage{}
// Chose StorageFile
if envdir := os.Getenv("STORAGE_DIR"); envdir != "" {
if envdir[len(envdir)-1:] == "/" { // remove trailing /
envdir = envdir[:len(envdir)-1]
}
s.StorageFile = envdir + "/ss_" + name
} else if conf != nil {
if e := s.testDir(conf.StorageDir); e != nil {
fmt.Errorf("Cant create Storage Directory")
return nil
}
s.StorageFile = conf.StorageDir + "/storage"
} else {
usr, err := user.Current()
if err != nil {
log.Fatal(err)
}
dir := usr.HomeDir + "/." + name
if e := s.testDir(dir); e != nil {
fmt.Errorf("Cant create Storage Directory")
return nil
}
s.StorageFile = dir + "/storage"
}
// make sure File exists
if _, err := os.Stat(s.StorageFile); os.IsNotExist(err) {
if _, err := os.Create(s.StorageFile); err != nil {
fmt.Errorf("Cant create Storage Directory")
return nil
}
}
s.lock = &sync.Mutex{}
s.read()
go s.watchFileChange()
return &s
}
func (this *Storage) GetUpdateChan() chan struct{} {
res := make(chan struct{})
this.notify = append(this.notify, res)
return res
}
func (this *Storage) sendNotify() {
this.lock.Lock()
defer this.lock.Unlock()
for _, c := range this.notify {
c <- struct{}{}
}
}
func (this *Storage) watchFileChange() {
watcher, err := fsnotify.NewWatcher()
if err != nil {
fmt.Println("ERROR", err)
}
defer watcher.Close()
//
if err := watcher.Add(this.StorageFile); err != nil {
fmt.Println("ERROR ", err)
}
for {
select {
case <-watcher.Events:
this.read()
this.sendNotify()
case err := <-watcher.Errors:
fmt.Println("ERROR", err)
}
}
}
func (this *Storage) read() {
data, err := ioutil.ReadFile(this.StorageFile)
if err != nil {
this.data = map[string]string{}
return
}
if err = json.Unmarshal(data, &this.data); err != nil {
this.data = map[string]string{}
return
}
}
func (this *Storage) save() {
this.lock.Lock()
data, _ := json.Marshal(&this.data)
err := ioutil.WriteFile(this.StorageFile, data, 644)
if err != nil {
fmt.Println("Error saving Data to ", this.StorageFile)
}
this.lock.Unlock()
this.sendNotify()
}
func (this *Storage) testDir(dir string) error {
if _, err := os.Stat(dir); os.IsNotExist(err) {
return os.MkdirAll(dir, os.ModePerm)
}
return nil
}
// Setters
func (this *Storage) StoreInterface(key string, val interface{}) {
bytes, _ := json.Marshal(val)
this.data[key] = string(bytes)
this.save()
}
func (this *Storage) StoreString(key, value string) {
this.data[key] = value
this.save()
}
func (this *Storage) StoreInt(key string, value int) {
this.data[key] = strconv.Itoa(value)
this.save()
}
func (this *Storage) StoreUint(key string, value uint) {
v, _ := conv.String(value)
this.data[key] = v
this.save()
}
func (this *Storage) StoreInt8(key string, value int8) {
v, _ := conv.String(value)
this.data[key] = v
this.save()
}
func (this *Storage) StoreInt32(key string, value int32) {
v, _ := conv.String(value)
this.data[key] = v
this.save()
}
func (this *Storage) StoreInt64(key string, value int64) {
v, _ := conv.String(value)
this.data[key] = v
this.save()
}
func (this *Storage) StoreuInt8(key string, value uint8) {
v, _ := conv.String(value)
this.data[key] = v
this.save()
}
func (this *Storage) StoreUint32(key string, value uint32) {
v, _ := conv.String(value)
this.data[key] = v
this.save()
}
func (this *Storage) StoreUint64(key string, value uint64) {
v, _ := conv.String(value)
this.data[key] = v
this.save()
}
func (this *Storage) StoreFloat32(key string, value float32) {
v, _ := conv.String(value)
this.data[key] = v
this.save()
}
func (this *Storage) StoreFloat64(key string, value float64) {
v, _ := conv.String(value)
this.data[key] = v
this.save()
}
func (this *Storage) StoreComplex64(key string, value complex64) {
v, _ := conv.String(value)
this.data[key] = v
this.save()
}
func (this *Storage) StoreComplex128(key string, value complex128) {
v, _ := conv.String(value)
this.data[key] = v
this.save()
}
// Getters
func (this *Storage) GetInterface(key string, value interface{}) error {
if val, ok := this.data[key]; ok {
return json.Unmarshal([]byte(val), value)
}
return errors.New("Key not found")
}
func (this *Storage) GetString(key string) (string, error) {
if val, ok := this.data[key]; ok {
return val, nil
}
return "", errors.New("Key not found")
}
func (this *Storage) GetInt(key string) (int, error) {
if val, ok := this.data[key]; ok {
return strconv.Atoi(val)
}
return 0, errors.New("Key not found")
}
func (this *Storage) GetInt8(key string) (int8, error) {
if val, ok := this.data[key]; ok {
return conv.Int8(val)
}
return 0, errors.New("Key not found")
}
func (this *Storage) GetInt32(key string) (int32, error) {
if val, ok := this.data[key]; ok {
return conv.Int32(val)
}
return 0, errors.New("Key not found")
}
func (this *Storage) GetInt64(key string) (int64, error) {
if val, ok := this.data[key]; ok {
return conv.Int64(val)
}
return 0, errors.New("Key not found")
}
func (this *Storage) GetUint(key string) (uint, error) {
if val, ok := this.data[key]; ok {
return conv.Uint(val)
}
return 0, errors.New("Key not found")
}
func (this *Storage) GetUint8(key string) (uint8, error) {
if val, ok := this.data[key]; ok {
return conv.Uint8(val)
}
return 0, errors.New("Key not found")
}
func (this *Storage) GetUint32(key string) (uint32, error) {
if val, ok := this.data[key]; ok {
return conv.Uint32(val)
}
return 0, errors.New("Key not found")
}
func (this *Storage) GetUint64(key string) (uint64, error) {
if val, ok := this.data[key]; ok {
return conv.Uint64(val)
}
return 0, errors.New("Key not found")
}
func (this *Storage) GetFloat32(key string) (float32, error) {
if val, ok := this.data[key]; ok {
return conv.Float32(val)
}
return 0, errors.New("Key not found")
}
func (this *Storage) GetFloat64(key string) (float64, error) {
if val, ok := this.data[key]; ok {
return conv.Float64(val)
}
return 0, errors.New("Key not found")
}