-
Notifications
You must be signed in to change notification settings - Fork 21
/
store.go
170 lines (142 loc) · 3.12 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
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
)
type Dashboard struct {
Id string `json:"id"`
Title string `json:"title"`
Tags []string `json:"tags"`
Path string `json:"-"`
}
type DashboardRepository struct {
Dir string
dashboards []*Dashboard
}
func (d *Dashboard) String() string {
return fmt.Sprintf("id=%s title=%s tags=%s path=%s", d.Id, d.Title, d.Tags, d.Path)
}
func (d *DashboardRepository) unmarshalDashboard(data io.Reader) (*Dashboard, error) {
body, err := ioutil.ReadAll(data)
if err != nil {
return nil, err
}
var dash Dashboard
err = json.Unmarshal(body, &dash)
if err != nil {
return nil, err
}
return &dash, nil
}
func (d *DashboardRepository) Load() error {
d.dashboards = []*Dashboard{}
err := filepath.Walk(d.Dir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() || !strings.HasSuffix(path, ".json") {
return nil
}
f, err := os.OpenFile(path, os.O_RDONLY, 0666)
if err != nil {
return err
}
defer f.Close()
dash, err := d.unmarshalDashboard(f)
if err != nil {
return err
}
dash.Path = path
d.dashboards = append(d.dashboards, dash)
return nil
})
if err != nil {
return err
}
log.Printf("Loaded %d dashboards\n", len(d.dashboards))
return nil
}
func (d *DashboardRepository) Search(query string) ([]*Dashboard, error) {
return d.dashboards, nil
}
func (d *DashboardRepository) Exists(id string) bool {
path := filepath.Join(d.Dir, id+".json")
if _, err := os.Stat(path); os.IsNotExist(err) {
return false
}
return true
}
func (d *DashboardRepository) Delete(id string) error {
dashboards := []*Dashboard{}
for _, d := range d.dashboards {
if d.Id == id {
err := os.Remove(d.Path)
if err != nil {
return err
}
log.Printf("Deleted dashboard %s", id)
} else {
dashboards = append(dashboards, d)
}
}
d.dashboards = dashboards
return nil
}
func (d *DashboardRepository) Get(id string) ([]byte, error) {
path := filepath.Join(d.Dir, id+".json")
f, err := os.OpenFile(path, os.O_RDONLY, 0666)
if err != nil {
return nil, err
}
defer f.Close()
return ioutil.ReadAll(f)
}
func (d *DashboardRepository) Save(id string, data []byte) error {
path := filepath.Join(d.Dir, id+".json.tmp")
f, err := os.OpenFile(path, os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0666)
if err != nil {
return err
}
defer f.Close()
n, err := f.Write(data)
if err != nil {
return err
}
if n != len(data) {
return fmt.Errorf("wrote %d, expected %d", n, len(data))
}
err = f.Sync()
if err != nil {
return err
}
newPath := filepath.Join(d.Dir, id+".json")
err = os.Rename(path, newPath)
if err != nil {
return err
}
dash, err := d.unmarshalDashboard(bytes.NewBuffer(data))
if err != nil {
return err
}
dash.Path = newPath
d.update(dash)
log.Printf("Saved dashboard %s", id)
return nil
}
func (d *DashboardRepository) update(dash *Dashboard) {
for _, d := range d.dashboards {
if d.Id == dash.Id {
d.Title = dash.Title
d.Tags = dash.Tags
return
}
}
d.dashboards = append(d.dashboards, dash)
}