-
Notifications
You must be signed in to change notification settings - Fork 1
/
oniontree.go
362 lines (333 loc) · 7.88 KB
/
oniontree.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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
package oniontree
import (
"fmt"
"github.com/go-yaml/yaml"
"io"
"os"
"path"
"path/filepath"
"sort"
"strings"
)
const (
cairnName string = ".oniontree"
)
type OnionTree struct {
dir string
format string
}
// Init initializes empty repository.
func (o OnionTree) Init() error {
for _, dir := range []string{o.TaggedDir(), o.UnsortedDir()} {
if err := os.MkdirAll(dir, 0755); err != nil {
return err
}
}
pth := path.Join(o.dir, cairnName)
cairnFile, err := os.Create(pth)
if err != nil {
return err
}
return cairnFile.Close()
}
// Add adds a new service to the repository with data from `s`.
func (o OnionTree) AddService(s *Service) error {
if err := s.Validate(); err != nil {
return err
}
pth := path.Join(o.UnsortedDir(), o.idToFilename(s.ID()))
file, err := os.OpenFile(pth, os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0666)
if err != nil {
if os.IsExist(err) {
return &ErrIdExists{s.ID()}
}
return err
}
defer file.Close()
data, err := o.marshalData(s)
if err != nil {
return err
}
if _, err := file.Write(data); err != nil {
return err
}
return nil
}
// Remove removes a service `id` from the repository with all its tags.
func (o OnionTree) RemoveService(id string) error {
tags, err := o.ListServiceTags(id)
if err != nil {
return err
}
if err := o.UntagService(id, tags); err != nil {
return err
}
pth := path.Join(o.UnsortedDir(), o.idToFilename(id))
if err := os.Remove(pth); err != nil {
if os.IsNotExist(err) {
return &ErrIdNotExists{id}
}
return err
}
return nil
}
// Update replaces existing service with new data from `s`.
func (o OnionTree) UpdateService(s *Service) error {
if err := s.Validate(); err != nil {
return err
}
pth := path.Join(o.UnsortedDir(), o.idToFilename(s.ID()))
file, err := os.OpenFile(pth, os.O_WRONLY|os.O_TRUNC, 0666)
if err != nil {
if os.IsNotExist(err) {
return &ErrIdNotExists{s.ID()}
}
return err
}
defer file.Close()
data, err := o.marshalData(s)
if err != nil {
return err
}
if _, err := file.Write(data); err != nil {
return err
}
return nil
}
// GetService returns content of service `id`.
func (o OnionTree) GetService(id string) (*Service, error) {
data, err := o.GetServiceBytes(id)
if err != nil {
return nil, err
}
s := NewService(id)
if err := o.unmarshalData(data, &s); err != nil {
return nil, err
}
return s, nil
}
// GetServiceBytes returns raw bytes of service `id`.
func (o OnionTree) GetServiceBytes(id string) ([]byte, error) {
pth := path.Join(o.UnsortedDir(), o.idToFilename(id))
file, err := os.Open(pth)
if err != nil {
if os.IsNotExist(err) {
return nil, &ErrIdNotExists{id}
}
return nil, err
}
defer file.Close()
data := []byte{}
buff := make([]byte, 1024)
for {
num, err := file.Read(buff)
if err != nil {
if err != io.EOF {
return nil, err
}
break
}
data = append(data, buff[:num]...)
}
return data, nil
}
// ListServices returns a list of service IDs found in the repository.
func (o OnionTree) ListServices() ([]string, error) {
file, err := os.Open(o.UnsortedDir())
if err != nil {
return nil, err
}
defer file.Close()
files, err := file.Readdirnames(0)
if err != nil {
return nil, err
}
for idx, _ := range files {
files[idx] = o.filenameToId(files[idx])
}
sort.Strings(files)
return files, nil
}
// ListServicesWithTag returns a list of services tagged with `tag`.
func (o OnionTree) ListServicesWithTag(tag Tag) ([]string, error) {
pth := path.Join(o.TaggedDir(), tag.String())
file, err := os.Open(pth)
if err != nil {
if os.IsNotExist(err) {
return nil, &ErrTagNotExists{tag}
}
return nil, err
}
defer file.Close()
services, err := file.Readdirnames(0)
if err != nil {
return nil, err
}
for idx, _ := range services {
services[idx] = o.filenameToId(services[idx])
}
sort.Strings(services)
return services, nil
}
// ListTags returns a list of tags found in the repository.
func (o OnionTree) ListTags() ([]Tag, error) {
file, err := os.Open(o.TaggedDir())
if err != nil {
return nil, err
}
defer file.Close()
files, err := file.Readdirnames(0)
if err != nil {
return nil, err
}
sort.Strings(files)
tags := make([]Tag, len(files))
for i := range files {
tags[i] = Tag(files[i])
}
return tags, nil
}
// ListServiceTags returns tags of service `id`.
// NOTICE: This function is very inefficient as it has to scale down the
// tagged directory recursively to find all symbolic links matching a pattern.
func (o OnionTree) ListServiceTags(id string) ([]Tag, error) {
matching := []Tag{}
tags, err := o.ListTags()
if err != nil {
return nil, err
}
for i := range tags {
serviceIDs, err := o.ListServicesWithTag(tags[i])
if err != nil {
return nil, err
}
for _, serviceID := range serviceIDs {
if serviceID == id {
matching = append(matching, tags[i])
break
}
}
}
return matching, nil
}
// TagService adds tags `tags` to service `id`.
func (o OnionTree) TagService(id string, tags []Tag) error {
for _, tag := range tags {
if err := tag.Validate(); err != nil {
return err
}
filename := o.idToFilename(id)
pth := path.Join(o.UnsortedDir(), filename)
if !isFile(pth) {
return &ErrIdNotExists{id}
}
pthTag := path.Join(o.TaggedDir(), tag.String())
// Create tag directory, ignore error if it already exists.
if err := os.Mkdir(pthTag, 0755); err != nil {
if !os.IsExist(err) {
return err
}
}
pthRel, err := filepath.Rel(pthTag, pth)
if err != nil {
return err
}
// Create tag, ignore error if it already exists.
if err := os.Symlink(pthRel, path.Join(pthTag, filename)); err != nil {
if !os.IsExist(err) {
return err
}
}
}
return nil
}
// UntagService removes tags `tags` from service `id`.
func (o OnionTree) UntagService(id string, tags []Tag) error {
for _, tag := range tags {
filename := o.idToFilename(id)
pth := path.Join(o.UnsortedDir(), filename)
if !isFile(pth) {
return &ErrIdNotExists{id}
}
pthTag := path.Join(o.TaggedDir(), tag.String())
pthLink := path.Join(pthTag, filename)
if isSymlink(pthLink) {
if err := os.Remove(pthLink); err != nil {
return err
}
}
if isEmptyDir(pthTag) {
if err := os.Remove(pthTag); err != nil {
return err
}
}
}
return nil
}
func (o OnionTree) Dir() string {
return o.dir
}
func (o OnionTree) UnsortedDir() string {
return path.Join(o.dir, "unsorted")
}
func (o OnionTree) TaggedDir() string {
return path.Join(o.dir, "tagged")
}
func (o OnionTree) marshalData(data interface{}) (b []byte, err error) {
switch o.format {
case "yaml":
b, err = yaml.Marshal(data)
default:
panic("unsupported format")
}
return
}
func (o OnionTree) unmarshalData(b []byte, data interface{}) (err error) {
switch o.format {
case "yaml":
err = yaml.Unmarshal(b, data)
default:
panic("unsupported format")
}
return
}
func (o OnionTree) idToFilename(id string) string {
return fmt.Sprintf("%s.%s", id, o.format)
}
func (o OnionTree) filenameToId(filename string) string {
return strings.TrimSuffix(filename, filepath.Ext(filename))
}
const maxDepth = 8
func (o OnionTree) findRootDir(dir string) (string, error) {
for i := 0; i < maxDepth; i++ {
// TODO: It would be better to return an error from isDir (if there'd be one).
if !isDir(dir) {
return "", &ErrNotOnionTree{dir}
}
pth := path.Join(dir, cairnName)
if isFile(pth) {
return path.Dir(pth), nil
}
dir = path.Join(dir, "..")
}
return "", &ErrNotOnionTree{dir}
}
// New returns initialized OnionTree structure. The function
// does not check if `dir` is a valid OnionTree repository.
func New(dir string) *OnionTree {
return &OnionTree{
dir: dir,
format: "yaml",
}
}
// Open attempts to "open" `dir` as a valid OnionTree repository.
// The function fails if the `dir` is not a valid OnionTree repository.
func Open(dir string) (*OnionTree, error) {
o := &OnionTree{format: "yaml"}
root, err := o.findRootDir(dir)
if err != nil {
return nil, err
}
o.dir = root
return o, nil
}