Created
September 15, 2016 19:53
Dump entire BoltDB to stdout, including Nested Buckets
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[User] | |
[bob] | |
bob@jones.com | |
name | |
Bob Jones | |
website | |
https://bob.jones.me/ | |
[chilts] | |
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