Skip to content
Open
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
69 changes: 48 additions & 21 deletions cmd/bbolt/command/command_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"

"github.com/spf13/cobra"
"github.com/spf13/pflag"

bolt "go.etcd.io/bbolt"
"go.etcd.io/bbolt/errors"
Expand All @@ -22,35 +23,61 @@ func newGetCommand() *cobra.Command {
Short: "get the value of a key from a (sub)bucket in a bbolt database",
Args: cobra.MinimumNArgs(3),
RunE: func(cmd *cobra.Command, args []string) error {
path := args[0]
if path == "" {
return ErrPathRequired
if err := opts.Validate(args); err != nil {
return err
}
buckets := args[1 : len(args)-1]
keyStr := args[len(args)-1]
return opts.Run(cmd, args)
},
}
opts.AddFlags(cmd.Flags())

// validate input parameters
if len(buckets) == 0 {
return fmt.Errorf("bucket is required: %w", ErrBucketRequired)
}
return cmd
}

key, err := parseBytes(keyStr, opts.parseFormat)
if err != nil {
return err
}
func (o *getOptions) AddFlags(fs *pflag.FlagSet) {
fs.StringVar(&o.parseFormat, "parse-format", "ascii-encoded", "Input format one of: ascii-encoded|hex")
fs.StringVar(&o.format, "format", "auto", "Output format one of: "+FORMAT_MODES+" (default: auto)")
}

if len(key) == 0 {
return fmt.Errorf("key is required: %w", errors.ErrKeyRequired)
}
func (o *getOptions) Validate(args []string) error {
if len(args) < 3 {
return fmt.Errorf("requires at least 3 arguments")
}

return getFunc(cmd, path, buckets, key, opts)
},
path := args[0]
if path == "" {
return ErrPathRequired
}

cmd.Flags().StringVar(&opts.parseFormat, "parse-format", "ascii-encoded", "Input format one of: ascii-encoded|hex")
cmd.Flags().StringVar(&opts.format, "format", "auto", "Output format one of: "+FORMAT_MODES+" (default: auto)")
buckets := args[1 : len(args)-1]
if len(buckets) == 0 {
return fmt.Errorf("bucket is required: %w", ErrBucketRequired)
}

return cmd
keyStr := args[len(args)-1]
key, err := parseBytes(keyStr, o.parseFormat)
if err != nil {
return err
}

if len(key) == 0 {
return fmt.Errorf("key is required: %w", errors.ErrKeyRequired)
}

return nil
}

func (o *getOptions) Run(cmd *cobra.Command, args []string) error {
path := args[0]
buckets := args[1 : len(args)-1]
keyStr := args[len(args)-1]

key, err := parseBytes(keyStr, o.parseFormat)
if err != nil {
return err
}
Comment on lines +71 to +78
Copy link
Member

@ahrtr ahrtr Jan 17, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems duplicated to the validate.

It is OK to add a separate AddFlags, but I do not see much value to add a separate Validate. Two proposals:

  • [preferred] remove the Validate method, do the validation in Run directly.
  • add the parsed arguments as fields in getOptions, so that Run can use them directly.


return getFunc(cmd, path, buckets, key, *o)
}

// getFunc opens the given bbolt db file and retrieves the key value from the bucket path.
Expand Down
Loading