Skip to content

Commit

Permalink
Merge pull request #531 from batchcorp/dselans/list_bucket_contents
Browse files Browse the repository at this point in the history
Add the ability to ls keys of a bucket
  • Loading branch information
ripienaar authored Aug 22, 2022
2 parents beb4263 + f5be97d commit 8780072
Showing 1 changed file with 109 additions and 22 deletions.
131 changes: 109 additions & 22 deletions cli/kv_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package cli

import (
"errors"
"fmt"
"io"
"math"
Expand All @@ -31,27 +32,29 @@ import (
)

type kvCommand struct {
bucket string
key string
val string
raw bool
history uint64
ttl time.Duration
replicas uint
force bool
maxValueSize int64
maxValueSizeString string
maxBucketSize int64
maxBucketSizeString string
revision uint64
description string
listNames bool
storage string
placementCluster string
placementTags []string
repubSource string
repubDest string
repubHeadersOnly bool
bucket string
key string
val string
raw bool
history uint64
ttl time.Duration
replicas uint
force bool
maxValueSize int64
maxValueSizeString string
maxBucketSize int64
maxBucketSizeString string
revision uint64
description string
listNames bool
lsVerbose bool
lsVerboseDisplayValue bool
storage string
placementCluster string
placementTags []string
repubSource string
repubDest string
repubHeadersOnly bool
}

func configureKVCommand(app commandHost) {
Expand Down Expand Up @@ -129,8 +132,11 @@ NOTE: This is an experimental feature.
watch.Arg("bucket", "The bucket to act on").Required().StringVar(&c.bucket)
watch.Arg("key", "The key to act on").Default(">").StringVar(&c.key)

ls := kv.Command("ls", "List available Buckets").Alias("list").Action(c.lsAction)
ls := kv.Command("ls", "List available buckets or the keys in a bucket").Alias("list").Action(c.lsAction)
ls.Arg("bucket", "The bucket to list the keys").StringVar(&c.bucket)
ls.Flag("names", "Show just the bucket names").Short('n').UnNegatableBoolVar(&c.listNames)
ls.Flag("verbose", "Show detailed info about the key").Short('v').UnNegatableBoolVar(&c.lsVerbose)
ls.Flag("display-value", "Display value in verbose output (has no effect without 'verbose')").UnNegatableBoolVar(&c.lsVerboseDisplayValue)

rmHistory := kv.Command("compact", "Removes all historic values from the store where the last value is a delete").Action(c.compactAction)
rmHistory.Arg("bucket", "The bucket to act on").Required().StringVar(&c.bucket)
Expand Down Expand Up @@ -176,12 +182,93 @@ func (c *kvCommand) strForOp(op nats.KeyValueOp) string {
}

func (c *kvCommand) lsAction(_ *fisk.ParseContext) error {
if c.bucket != "" {
return c.lsBucketKeys()
}

return c.lsBuckets()
}

func (c *kvCommand) lsBucketKeys() error {
_, js, err := prepareJSHelper()
if err != nil {
return fmt.Errorf("unable to prepare js helper: %s", err)
}

kv, err := js.KeyValue(c.bucket)
if err != nil {
return fmt.Errorf("unable to load bucket: %s", err)
}

keys, err := kv.Keys()
if err != nil {
if err == nats.ErrNoKeysFound {
fmt.Println("No keys found in bucket")
return nil
}

return fmt.Errorf("unable to fetch keys in bucket: %s", err)
}

if c.lsVerbose {
if err := c.displayKeyInfo(kv, keys); err != nil {
return fmt.Errorf("unable to display key info: %s", err)
}
} else {
for _, v := range keys {
fmt.Println(v)
}
}

return nil
}

func (c *kvCommand) displayKeyInfo(kv nats.KeyValue, keys []string) error {
if kv == nil {
return errors.New("key value cannot be nil")
}

table := newTableWriter(fmt.Sprintf("Contents for bucket '%s'", c.bucket))

if c.lsVerboseDisplayValue {
table.AddHeaders("Key", "Created", "Delta", "Revision", "Value")
} else {
table.AddHeaders("Key", "Created", "Delta", "Revision")
}

for _, keyName := range keys {
kve, err := kv.Get(keyName)
if err != nil {
return fmt.Errorf("unable to fetch key %s: %s", keyName, err)
}

row := []interface{}{
kve.Key(),
kve.Created().Format("2006-01-02 15:04:05"),
kve.Delta(),
kve.Revision(),
}

if c.lsVerboseDisplayValue {
row = append(row, string(kve.Value()))
}

table.AddRow(row...)
}

fmt.Println(table.Render())

return nil
}

func (c *kvCommand) lsBuckets() error {
_, mgr, err := prepareHelper("", natsOpts()...)
if err != nil {
return err
}

var found []*jsm.Stream

err = mgr.EachStream(func(s *jsm.Stream) {
if s.IsKVBucket() {
found = append(found, s)
Expand Down

0 comments on commit 8780072

Please sign in to comment.