Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the ability to ls keys of a bucket #531

Merged
merged 3 commits into from
Aug 22, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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