-
Notifications
You must be signed in to change notification settings - Fork 0
/
bootstrap.go
59 lines (54 loc) · 1.4 KB
/
bootstrap.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
package boltdb
import (
"github.com/go-ap/errors"
bolt "go.etcd.io/bbolt"
)
func Bootstrap(conf Config) error {
r, err := New(conf)
if err != nil {
return err
}
db, err := bolt.Open(r.path, 0600, nil)
if err != nil {
return err
}
defer db.Close()
return db.Update(func(tx *bolt.Tx) error {
root, err := tx.CreateBucketIfNotExists(r.root)
if err != nil {
return errors.Annotatef(err, "could not create root bucket")
}
_, err = root.CreateBucketIfNotExists([]byte(accessBucket))
if err != nil {
return errors.Annotatef(err, "could not create %s bucket", accessBucket)
}
_, err = root.CreateBucketIfNotExists([]byte(refreshBucket))
if err != nil {
return errors.Annotatef(err, "could not create %s bucket", refreshBucket)
}
_, err = root.CreateBucketIfNotExists([]byte(authorizeBucket))
if err != nil {
return errors.Annotatef(err, "could not create %s bucket", authorizeBucket)
}
_, err = root.CreateBucketIfNotExists([]byte(clientsBucket))
if err != nil {
return errors.Annotatef(err, "could not create %s bucket", clientsBucket)
}
return nil
})
}
func Clean(conf Config) error {
path, err := Path(conf)
if err != nil {
return err
}
db, err := bolt.Open(path, 0600, nil)
if err != nil {
return errors.Annotatef(err, "could not open db")
}
defer db.Close()
return db.Update(func(tx *bolt.Tx) error {
tx.DeleteBucket([]byte(rootBucket))
return nil
})
}