-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstore.go
More file actions
61 lines (50 loc) · 1.06 KB
/
Copy pathstore.go
File metadata and controls
61 lines (50 loc) · 1.06 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
package main
import (
"errors"
"fmt"
c "github.com/ostafen/clover/v2"
)
type store struct {
db *c.DB
}
type collectionName string
const (
collectionGistContent collectionName = "gist_content_list"
collectionDraftedGists collectionName = "drafted_gists"
)
var (
collections = []collectionName{
collectionGistContent,
collectionDraftedGists,
}
)
func (s *store) init(path string) error {
db, err := c.Open(path)
if err != nil {
return errors.New(fmt.Sprintf("Failed to check collection: %v", err))
}
s.db = db
for _, collection := range collections {
s.initCollection(collection)
}
return nil
}
func (s *store) drop() error {
for _, collection := range collections {
if err := s.db.DropCollection(string(collection)); err != nil {
return fmt.Errorf("error while dropping collection %s\n", collection)
}
}
return nil
}
func (s *store) initCollection(name collectionName) error {
exists, err := s.db.HasCollection(string(name))
if err != nil {
return err
}
if !exists {
s.db.CreateCollection(string(name))
return nil
}
return nil
}