From fd5535f71f488dda0915f610b6ca8c77c9ca2c59 Mon Sep 17 00:00:00 2001 From: Holger Bartnick Date: Sat, 26 Feb 2022 05:50:46 +0100 Subject: [PATCH] support sub buckets in get+keys (#295) add possibility to get keys and values of sub buckets --- cmd/bbolt/main.go | 42 ++++++++++++++++++++++++++++-------------- 1 file changed, 28 insertions(+), 14 deletions(-) diff --git a/cmd/bbolt/main.go b/cmd/bbolt/main.go index 42fd72c..5009aec 100644 --- a/cmd/bbolt/main.go +++ b/cmd/bbolt/main.go @@ -1153,12 +1153,13 @@ func (cmd *KeysCommand) Run(args ...string) error { } // Require database path and bucket. - path, bucket := fs.Arg(0), fs.Arg(1) + relevantArgs := fs.Args() + path, buckets := relevantArgs[0], relevantArgs[1:] if path == "" { return ErrPathRequired } else if _, err := os.Stat(path); os.IsNotExist(err) { return ErrFileNotFound - } else if bucket == "" { + } else if len(buckets) == 0 { return ErrBucketRequired } @@ -1172,13 +1173,19 @@ func (cmd *KeysCommand) Run(args ...string) error { // Print keys. return db.View(func(tx *bolt.Tx) error { // Find bucket. - b := tx.Bucket([]byte(bucket)) - if b == nil { + var lastbucket *bolt.Bucket = tx.Bucket([]byte(buckets[0])) + if lastbucket == nil { return ErrBucketNotFound } + for _, bucket := range buckets[1:] { + lastbucket = lastbucket.Bucket([]byte(bucket)) + if lastbucket == nil { + return ErrBucketNotFound + } + } // Iterate over each key. - return b.ForEach(func(key, _ []byte) error { + return lastbucket.ForEach(func(key, _ []byte) error { fmt.Fprintln(cmd.Stdout, string(key)) return nil }) @@ -1188,9 +1195,9 @@ func (cmd *KeysCommand) Run(args ...string) error { // Usage returns the help message. func (cmd *KeysCommand) Usage() string { return strings.TrimLeft(` -usage: bolt keys PATH BUCKET +usage: bolt keys PATH [BUCKET...] -Print a list of keys in the given bucket. +Print a list of keys in the given (sub)bucket. `, "\n") } @@ -1223,12 +1230,13 @@ func (cmd *GetCommand) Run(args ...string) error { } // Require database path, bucket and key. - path, bucket, key := fs.Arg(0), fs.Arg(1), fs.Arg(2) + relevantArgs := fs.Args() + path, buckets, key := relevantArgs[0], relevantArgs[1:len(relevantArgs)-1], relevantArgs[len(relevantArgs)-1] if path == "" { return ErrPathRequired } else if _, err := os.Stat(path); os.IsNotExist(err) { return ErrFileNotFound - } else if bucket == "" { + } else if len(buckets) == 0 { return ErrBucketRequired } else if key == "" { return ErrKeyRequired @@ -1244,13 +1252,19 @@ func (cmd *GetCommand) Run(args ...string) error { // Print value. return db.View(func(tx *bolt.Tx) error { // Find bucket. - b := tx.Bucket([]byte(bucket)) - if b == nil { + var lastbucket *bolt.Bucket = tx.Bucket([]byte(buckets[0])) + if lastbucket == nil { return ErrBucketNotFound } + for _, bucket := range buckets[1:] { + lastbucket = lastbucket.Bucket([]byte(bucket)) + if lastbucket == nil { + return ErrBucketNotFound + } + } // Find value for given key. - val := b.Get([]byte(key)) + val := lastbucket.Get([]byte(key)) if val == nil { return ErrKeyNotFound } @@ -1263,9 +1277,9 @@ func (cmd *GetCommand) Run(args ...string) error { // Usage returns the help message. func (cmd *GetCommand) Usage() string { return strings.TrimLeft(` -usage: bolt get PATH BUCKET KEY +usage: bolt get PATH [BUCKET..] KEY -Print the value of the given key in the given bucket. +Print the value of the given key in the given (sub)bucket. `, "\n") }