Skip to content

Instantly share code, notes, and snippets.

@chilts
Created September 15, 2016 19:53
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save chilts/6bd440c056f85e9c2c6114c017ecab1e to your computer and use it in GitHub Desktop.
Save chilts/6bd440c056f85e9c2c6114c017ecab1e to your computer and use it in GitHub Desktop.
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