aboutsummaryrefslogtreecommitdiff
path: root/cmd/bolt/main.go
diff options
context:
space:
mode:
authorBen Johnson <benbjohnson@yahoo.com>2014-03-21 22:05:28 -0600
committerBen Johnson <benbjohnson@yahoo.com>2014-03-21 22:05:28 -0600
commit8fa6531b1cff8288d453dafa656753fb08192aeb (patch)
tree70d0dad19d15af36b6b4997f2cc9801a247979f7 /cmd/bolt/main.go
parentAdd 'bolt get'. (diff)
downloaddedo-8fa6531b1cff8288d453dafa656753fb08192aeb.tar.gz
dedo-8fa6531b1cff8288d453dafa656753fb08192aeb.tar.xz
Add 'bolt keys'.
Diffstat (limited to 'cmd/bolt/main.go')
-rw-r--r--cmd/bolt/main.go46
1 files changed, 43 insertions, 3 deletions
diff --git a/cmd/bolt/main.go b/cmd/bolt/main.go
index 0a17a13..c29d5df 100644
--- a/cmd/bolt/main.go
+++ b/cmd/bolt/main.go
@@ -23,14 +23,19 @@ func NewApp() *cli.App {
{
Name: "get",
Usage: "retrieve a value for given key",
- Action: Get,
+ Action: GetCommand,
+ },
+ {
+ Name: "keys",
+ Usage: "retrieve a list of all keys in a bucket",
+ Action: KeysCommand,
},
}
return app
}
-// Get retrieves the value for a given bucket/key.
-func Get(c *cli.Context) {
+// GetCommand retrieves the value for a given bucket/key.
+func GetCommand(c *cli.Context) {
path, name, key := c.Args().Get(0), c.Args().Get(1), c.Args().Get(2)
if _, err := os.Stat(path); os.IsNotExist(err) {
fatal(err)
@@ -68,6 +73,41 @@ func Get(c *cli.Context) {
}
}
+// KeysCommand retrieves a list of keys for a given bucket.
+func KeysCommand(c *cli.Context) {
+ path, name := c.Args().Get(0), c.Args().Get(1)
+ if _, err := os.Stat(path); os.IsNotExist(err) {
+ fatal(err)
+ return
+ }
+
+ db, err := bolt.Open(path, 0600)
+ if err != nil {
+ fatal(err)
+ return
+ }
+ defer db.Close()
+
+ err = db.With(func(tx *bolt.Tx) error {
+ // Find bucket.
+ b := tx.Bucket(name)
+ if b == nil {
+ fatalf("bucket not found: %s", name)
+ return nil
+ }
+
+ // Iterate over each key.
+ return b.ForEach(func(key, _ []byte) error {
+ logger.Println(string(key))
+ return nil
+ })
+ })
+ if err != nil {
+ fatal(err)
+ return
+ }
+}
+
var logger = log.New(os.Stderr, "", 0)
var logBuffer *bytes.Buffer