-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstorage.go
More file actions
90 lines (78 loc) · 1.62 KB
/
Copy pathstorage.go
File metadata and controls
90 lines (78 loc) · 1.62 KB
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
/* Package storage defines a backend for storing and retrieving configuration data.
*/
package storage
import (
"encoding/json"
"errors"
"io/ioutil"
"os"
)
var KeyNotFound = errors.New("Key not found")
type Storer interface {
Get(ns, key string) (string, error)
Set(ns, key, value string) error
Remove(ns, key string) error
}
type storage map[string]map[string]string
type JSONStorage struct {
loc string
s storage
}
func New(loc string) (*JSONStorage, error) {
var s storage
if _, err := os.Stat(loc); err != nil {
if os.IsNotExist(err) {
s := map[string]map[string]string{}
return &JSONStorage{
s: s,
loc: loc,
}, nil
} else {
return nil, err
}
} else {
data, err := ioutil.ReadFile(loc)
if err != nil {
return nil, err
}
if err := json.Unmarshal(data, &s); err != nil {
return nil, err
}
}
return &JSONStorage{s: s, loc: loc}, nil
}
func (j *JSONStorage) Set(ns, key, value string) error {
if _, ok := j.s[ns]; !ok {
j.s[ns] = map[string]string{key: value}
return j.Save()
}
j.s[ns][key] = value
return j.Save()
}
func (j *JSONStorage) Remove(ns, key string) error {
if inner, ok := j.s[ns]; ok {
if _, ok := j.s[ns][key]; ok {
delete(inner, key)
if len(j.s[ns]) == 0 {
delete(j.s, ns)
}
return j.Save()
}
}
return KeyNotFound
}
func (j *JSONStorage) Get(ns, key string) (string, error) {
if _, ok := j.s[ns]; ok {
if val, ok := j.s[ns][key]; ok {
return val, nil
}
}
return "", KeyNotFound
}
func (j *JSONStorage) Save() error {
data, err := json.Marshal(j.s)
if err != nil {
return err
}
return ioutil.WriteFile(j.loc, data, 0770)
}