feat(parser): adds explicit DB parser for when we don't want a key - should probably reject keyinput on match

This commit is contained in:
Lewis Wynne 2025-11-06 22:47:01 +00:00
parent af049a6ade
commit fa224d9b38
2 changed files with 21 additions and 3 deletions

View file

@ -28,7 +28,7 @@ import (
// listCmd represents the set command
var listCmd = &cobra.Command{
Use: "list [@DB]",
Use: "list [DB]",
Short: "List the contents of a db.",
Args: cobra.MaximumNArgs(1),
RunE: list,
@ -38,7 +38,11 @@ func list(cmd *cobra.Command, args []string) error {
store := &Store{}
targetDB := "@default"
if len(args) == 1 {
targetDB = args[0]
dbName, err := store.parseDB(args[0], false)
if err != nil {
return err
}
targetDB = "@" + dbName
}
trans := TransactionArgs{

View file

@ -123,7 +123,7 @@ func (s *Store) AllStores() ([]string, error) {
}
func (s *Store) FindStore(k string) (string, error) {
_, n, err := s.parse(k, false)
n, err := s.parseDB(k, false)
if err != nil {
return "", err
}
@ -176,6 +176,20 @@ func (s *Store) parse(k string, defaults bool) ([]byte, string, error) {
return []byte(key), db, nil
}
func (s *Store) parseDB(v string, defaults bool) (string, error) {
db := strings.TrimSpace(v)
if strings.HasPrefix(db, "@") {
db = strings.TrimPrefix(db, "@")
}
if db == "" {
if defaults {
return "default", nil
}
return "", fmt.Errorf("bad db format, use DB or @DB")
}
return strings.ToLower(db), nil
}
func (s *Store) open(name string) (*badger.DB, error) {
if name == "" {
name = "default"