Prepare boltutil for reading and writing another map
Refactor the code so that another function can also read and write maps
into the bolt db.
@@ -32,7 +32,11 @@ var (
// ReadLabels reads the labels key from the bucket
// Uses the key "labels"
func ReadLabels(bkt *bolt.Bucket) (map[string]string, error) {
- lbkt := bkt.Bucket(bucketKeyLabels)
+ return readMap(bkt, bucketKeyLabels)
+}
+
+func readMap(bkt *bolt.Bucket, bucketName []byte) (map[string]string, error) {
+ lbkt := bkt.Bucket(bucketName)
if lbkt == nil {
return nil, nil
}
@@ -53,9 +57,13 @@ func ReadLabels(bkt *bolt.Bucket) (map[string]string, error) {
// bucket. Typically, this removes zero-value entries.
// Uses the key "labels"
func WriteLabels(bkt *bolt.Bucket, labels map[string]string) error {
+ return writeMap(bkt, bucketKeyLabels, labels)
+}
+
+func writeMap(bkt *bolt.Bucket, bucketName []byte, labels map[string]string) error {
// Remove existing labels to keep from merging
- if lbkt := bkt.Bucket(bucketKeyLabels); lbkt != nil {
- if err := bkt.DeleteBucket(bucketKeyLabels); err != nil {
+ if lbkt := bkt.Bucket(bucketName); lbkt != nil {
+ if err := bkt.DeleteBucket(bucketName); err != nil {
return err
}
}
@@ -64,7 +72,7 @@ func WriteLabels(bkt *bolt.Bucket, labels map[string]string) error {
return nil
}
- lbkt, err := bkt.CreateBucket(bucketKeyLabels)
+ lbkt, err := bkt.CreateBucket(bucketName)
if err != nil {
return err
}
