feat(list): added --only-keys, -k and --only-values, -v flags

This commit is contained in:
Lewis Wynne 2025-11-07 00:22:07 +00:00
parent 3f30a23c36
commit d68bc8979b

View file

@ -69,6 +69,21 @@ func list(cmd *cobra.Command, args []string) error {
return err return err
} }
keysOnly, err := cmd.Flags().GetBool("only-keys")
if err != nil {
return err
}
valuesOnly, err := cmd.Flags().GetBool("only-values")
if err != nil {
return err
}
if keysOnly && valuesOnly {
return fmt.Errorf("--only-keys and --only-values are mutually exclusive")
}
prefetchVals := !keysOnly
placeholder := []byte("[secret: pass --include-secret to view]")
trans := TransactionArgs{ trans := TransactionArgs{
key: targetDB, key: targetDB,
readonly: true, readonly: true,
@ -81,22 +96,40 @@ func list(cmd *cobra.Command, args []string) error {
format := fmt.Sprintf("%%s%s%%s\n", delimiter) format := fmt.Sprintf("%%s%s%%s\n", delimiter)
opts := badger.DefaultIteratorOptions opts := badger.DefaultIteratorOptions
opts.PrefetchSize = 10 opts.PrefetchSize = 10
opts.PrefetchValues = prefetchVals
it := tx.NewIterator(opts) it := tx.NewIterator(opts)
defer it.Close() defer it.Close()
for it.Rewind(); it.Valid(); it.Next() { for it.Rewind(); it.Valid(); it.Next() {
item := it.Item() item := it.Item()
key := item.Key() key := item.Key()
meta := item.UserMeta() meta := item.UserMeta()
if err := item.Value(func(v []byte) error { if meta&metaSecret != 0 && !includeSecret {
if meta&metaSecret != 0 && !includeSecret { switch {
placeholder := []byte("[secret: pass --secret to view]") case keysOnly:
store.Print("%s\n", false, key)
case valuesOnly:
store.Print("%s\n", false, placeholder)
default:
store.Print(format, false, key, placeholder) store.Print(format, false, key, placeholder)
return nil
} }
store.Print(format, binary, key, v) continue
return nil }
}); err != nil { var preparedValue []byte
return err if !keysOnly {
if err := item.Value(func(v []byte) error {
preparedValue = append([]byte(nil), v...)
return nil
}); err != nil {
return err
}
}
switch {
case keysOnly:
store.Print("%s\n", false, key)
case valuesOnly:
store.Print("%s\n", binary, preparedValue)
default:
store.Print(format, binary, key, preparedValue)
} }
} }
return nil return nil
@ -110,5 +143,7 @@ func init() {
listCmd.Flags().BoolP("include-binary", "b", false, "include binary data in text output") listCmd.Flags().BoolP("include-binary", "b", false, "include binary data in text output")
listCmd.Flags().StringP("delimiter", "d", "\t\t", "string written between key and value columns") listCmd.Flags().StringP("delimiter", "d", "\t\t", "string written between key and value columns")
listCmd.Flags().Bool("include-secret", false, "include entries marked as secret") listCmd.Flags().Bool("include-secret", false, "include entries marked as secret")
listCmd.Flags().BoolP("only-keys", "k", false, "only print keys")
listCmd.Flags().BoolP("only-values", "v", false, "only print values")
rootCmd.AddCommand(listCmd) rootCmd.AddCommand(listCmd)
} }