-
Notifications
You must be signed in to change notification settings - Fork 0
/
boltdb.go
128 lines (97 loc) · 2.68 KB
/
boltdb.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
package datastores
import (
"encoding/json"
"fmt"
"github.com/boltdb/bolt"
)
// The BoltDB database information
type BoltDB struct {
Database string
User string
Password string
}
func (store BoltDB) InitStore(overwrite bool) error {
// Open the database:
db, err := bolt.Open(store.Database, 0600, nil)
checkErr(err)
defer db.Close()
return err
}
func (store BoltDB) Get(configItem *ConfigItem) (ConfigItem, error) {
// Open the database:
db, err := bolt.Open(store.Database, 0600, nil)
checkErr(err)
defer db.Close()
// Our return item:
retval := ConfigItem{}
// TODO: Get global first, then get application, then get application + machine
// Get the key based on the application
err = db.View(func(tx *bolt.Tx) error {
// Get the item from the bucket with the app name
b := tx.Bucket([]byte(configItem.Application))
if b != nil {
// Get the item based on the config name:
configBytes := b.Get([]byte(configItem.Name))
// Need to make sure we got something back here before we try to unmarshal?
if len(configBytes) > 0 {
// Unmarshal data into our config item
if err := json.Unmarshal(configBytes, &retval); err != nil {
return err
}
}
}
return nil
})
checkErr(err)
return retval, err
}
func (store BoltDB) GetAll(application string) ([]ConfigItem, error) {
// Open the database:
db, err := bolt.Open(store.Database, 0600, nil)
checkErr(err)
defer db.Close()
// Our return items:
retval := []ConfigItem{}
// TODO: Get global first, then get application, then get application + machine
// Get the key based on the application
err = db.View(func(tx *bolt.Tx) error {
// Get the items from the bucket with the app name
b := tx.Bucket([]byte(application))
c := b.Cursor()
for k, v := c.First(); k != nil; k, v = c.Next() {
// Unmarshal data into our config item
ci := ConfigItem{}
if err := json.Unmarshal(v, &ci); err != nil {
return err
}
// Add the item to our list of config items
retval = append(retval, ci)
}
return nil
})
checkErr(err)
return retval, err
}
func (store BoltDB) Set(configItem *ConfigItem) error {
// Open the database:
db, err := bolt.Open(store.Database, 0600, nil)
checkErr(err)
defer db.Close()
// Update the database:
err = db.Update(func(tx *bolt.Tx) error {
// Put the item in the bucket with the app name
b, err := tx.CreateBucketIfNotExists([]byte(configItem.Application))
checkErr(err)
// Serialize to JSON format
encoded, err := json.Marshal(configItem)
checkErr(err)
// Store it, with the 'name' as the key:
return b.Put([]byte(configItem.Name), encoded)
})
return err
}
func checkErr(err error) {
if err != nil {
fmt.Println(err)
}
}