Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Dump entire BoltDB to stdout, including Nested Buckets
package main
import (
"fmt"
"log"
"strings"
"time"
"github.com/boltdb/bolt"
)
func check(err error) {
if err != nil {
log.Fatal(err)
}
}
func dumpCursor(tx *bolt.Tx, c *bolt.Cursor, indent int) {
for k, v := c.First(); k != nil; k, v = c.Next() {
if v == nil {
fmt.Printf(strings.Repeat("\t", indent)+"[%s]\n", k)
newBucket := c.Bucket().Bucket(k)
if newBucket == nil {
newBucket = tx.Bucket(k)
}
newCursor := newBucket.Cursor()
dumpCursor(tx, newCursor, indent+1)
} else {
fmt.Printf(strings.Repeat("\t", indent)+"%s\n", k)
fmt.Printf(strings.Repeat("\t", indent+1)+"%s\n", v)
}
}
}
func main() {
db, err := bolt.Open("webmint.db", 0666, &bolt.Options{Timeout: 1 * time.Second})
check(err)
defer db.Close()
err = db.View(func(tx *bolt.Tx) error {
c := tx.Cursor()
dumpCursor(tx, c, 0)
return nil
})
check(err)
}
[User]
[bob]
email
bob@jones.com
name
Bob Jones
website
https://bob.jones.me/
[chilts]
email
andychilton@gmail.com
name
name
website
https://chilts.org/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment