support sub buckets in get+keys (#295)

add possibility to get keys and values of sub buckets
pull/323/head
Holger Bartnick 2022-02-26 05:50:46 +01:00 committed by GitHub
parent d5db64bdbf
commit fd5535f71f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 28 additions and 14 deletions

View File

@ -1153,12 +1153,13 @@ func (cmd *KeysCommand) Run(args ...string) error {
} }
// Require database path and bucket. // Require database path and bucket.
path, bucket := fs.Arg(0), fs.Arg(1) relevantArgs := fs.Args()
path, buckets := relevantArgs[0], relevantArgs[1:]
if path == "" { if path == "" {
return ErrPathRequired return ErrPathRequired
} else if _, err := os.Stat(path); os.IsNotExist(err) { } else if _, err := os.Stat(path); os.IsNotExist(err) {
return ErrFileNotFound return ErrFileNotFound
} else if bucket == "" { } else if len(buckets) == 0 {
return ErrBucketRequired return ErrBucketRequired
} }
@ -1172,13 +1173,19 @@ func (cmd *KeysCommand) Run(args ...string) error {
// Print keys. // Print keys.
return db.View(func(tx *bolt.Tx) error { return db.View(func(tx *bolt.Tx) error {
// Find bucket. // Find bucket.
b := tx.Bucket([]byte(bucket)) var lastbucket *bolt.Bucket = tx.Bucket([]byte(buckets[0]))
if b == nil { if lastbucket == nil {
return ErrBucketNotFound return ErrBucketNotFound
} }
for _, bucket := range buckets[1:] {
lastbucket = lastbucket.Bucket([]byte(bucket))
if lastbucket == nil {
return ErrBucketNotFound
}
}
// Iterate over each key. // Iterate over each key.
return b.ForEach(func(key, _ []byte) error { return lastbucket.ForEach(func(key, _ []byte) error {
fmt.Fprintln(cmd.Stdout, string(key)) fmt.Fprintln(cmd.Stdout, string(key))
return nil return nil
}) })
@ -1188,9 +1195,9 @@ func (cmd *KeysCommand) Run(args ...string) error {
// Usage returns the help message. // Usage returns the help message.
func (cmd *KeysCommand) Usage() string { func (cmd *KeysCommand) Usage() string {
return strings.TrimLeft(` 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") `, "\n")
} }
@ -1223,12 +1230,13 @@ func (cmd *GetCommand) Run(args ...string) error {
} }
// Require database path, bucket and key. // 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 == "" { if path == "" {
return ErrPathRequired return ErrPathRequired
} else if _, err := os.Stat(path); os.IsNotExist(err) { } else if _, err := os.Stat(path); os.IsNotExist(err) {
return ErrFileNotFound return ErrFileNotFound
} else if bucket == "" { } else if len(buckets) == 0 {
return ErrBucketRequired return ErrBucketRequired
} else if key == "" { } else if key == "" {
return ErrKeyRequired return ErrKeyRequired
@ -1244,13 +1252,19 @@ func (cmd *GetCommand) Run(args ...string) error {
// Print value. // Print value.
return db.View(func(tx *bolt.Tx) error { return db.View(func(tx *bolt.Tx) error {
// Find bucket. // Find bucket.
b := tx.Bucket([]byte(bucket)) var lastbucket *bolt.Bucket = tx.Bucket([]byte(buckets[0]))
if b == nil { if lastbucket == nil {
return ErrBucketNotFound return ErrBucketNotFound
} }
for _, bucket := range buckets[1:] {
lastbucket = lastbucket.Bucket([]byte(bucket))
if lastbucket == nil {
return ErrBucketNotFound
}
}
// Find value for given key. // Find value for given key.
val := b.Get([]byte(key)) val := lastbucket.Get([]byte(key))
if val == nil { if val == nil {
return ErrKeyNotFound return ErrKeyNotFound
} }
@ -1263,9 +1277,9 @@ func (cmd *GetCommand) Run(args ...string) error {
// Usage returns the help message. // Usage returns the help message.
func (cmd *GetCommand) Usage() string { func (cmd *GetCommand) Usage() string {
return strings.TrimLeft(` 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") `, "\n")
} }