forked from ponzu-cms/ponzu
/
addon.go
165 lines (133 loc) · 3.04 KB
/
addon.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
package db
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"log"
"net/url"
"github.com/boltdb/bolt"
"github.com/gorilla/schema"
)
var (
// ErrNoAddonExists indicates that there was not addon found in the db
ErrNoAddonExists = errors.New("No addon exists.")
)
// Addon looks for an addon by its addon_reverse_dns as the key and returns
// the []byte as json representation of an addon
func Addon(key string) ([]byte, error) {
buf := &bytes.Buffer{}
err := store.View(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte("__addons"))
if b == nil {
return bolt.ErrBucketNotFound
}
val := b.Get([]byte(key))
if val == nil {
return ErrNoAddonExists
}
_, err := buf.Write(val)
if err != nil {
return err
}
return nil
})
if err != nil {
return nil, err
}
return buf.Bytes(), nil
}
// SetAddon stores the values of an addon into the __addons bucket with a the
// `addon_reverse_dns` field used as the key. `kind` is the interface{} type for
// the provided addon (as in the result of calling addon.Types[id])
func SetAddon(data url.Values, kind interface{}) error {
dec := schema.NewDecoder()
dec.IgnoreUnknownKeys(true)
dec.SetAliasTag("json")
err := dec.Decode(kind, data)
v, err := json.Marshal(kind)
k := data.Get("addon_reverse_dns")
if k == "" {
name := data.Get("addon_name")
return fmt.Errorf(`Addon "%s" has no identifier to use as key.`, name)
}
err = store.Update(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte("__addons"))
if b == nil {
return bolt.ErrBucketNotFound
}
err := b.Put([]byte(k), v)
if err != nil {
return err
}
return nil
})
if err != nil {
return err
}
return nil
}
// AddonAll returns all registered addons as a [][]byte
func AddonAll() [][]byte {
var all [][]byte
err := store.View(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte("__addons"))
if b == nil {
return bolt.ErrBucketNotFound
}
err := b.ForEach(func(k, v []byte) error {
all = append(all, v)
return nil
})
if err != nil {
return err
}
return nil
})
if err != nil {
log.Println("Error finding addons in db with db.AddonAll:", err)
return nil
}
return all
}
// DeleteAddon removes an addon from the db by its key, the addon_reverse_dns
func DeleteAddon(key string) error {
err := store.Update(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte("__addons"))
if b == nil {
return bolt.ErrBucketNotFound
}
if err := b.Delete([]byte(key)); err != nil {
return err
}
return nil
})
if err != nil {
return err
}
return nil
}
// AddonExists checks if there is an existing addon stored. The key is an the
// value at addon_reverse_dns
func AddonExists(key string) bool {
var exists bool
if store == nil {
Init()
}
err := store.Update(func(tx *bolt.Tx) error {
b, err := tx.CreateBucketIfNotExists([]byte("__addons"))
if err != nil {
return err
}
if b.Get([]byte(key)) == nil {
return nil
}
exists = true
return nil
})
if err != nil {
log.Println("Error checking existence of addon with key:", key, "-", err)
return false
}
return exists
}