feat(cmd): improves error messaging for globs

This commit is contained in:
Lewis Wynne 2025-12-18 01:53:09 +00:00
parent 20294a9279
commit 3d4cd40a17
8 changed files with 20 additions and 14 deletions

View file

@ -66,6 +66,10 @@ func del(cmd *cobra.Command, args []string) error {
return err return err
} }
if len(targetKeys) == 0 {
return fmt.Errorf("cannot remove: No such key")
}
if !force { if !force {
var confirm string var confirm string
quotedTargets := make([]string, 0, len(targetKeys)) quotedTargets := make([]string, 0, len(targetKeys))
@ -153,14 +157,13 @@ func resolveDeleteTargets(store *Store, exactArgs []string, globPatterns []strin
var targetKeys []string var targetKeys []string
var deleteTargets []string var deleteTargets []string
var matched bool
for _, arg := range exactArgs { for _, arg := range exactArgs {
exists, err := keyExists(store, arg) exists, err := keyExists(store, arg)
if err != nil { if err != nil {
return nil, nil, fmt.Errorf("cannot remove '%s': %v", arg, err) return nil, nil, fmt.Errorf("cannot remove '%s': %v", arg, err)
} }
if !exists { if !exists {
return nil, nil, fmt.Errorf("cannot remove '%s': No such key", arg) continue
} }
formatted, err := formatKeyForPrompt(store, arg) formatted, err := formatKeyForPrompt(store, arg)
if err != nil { if err != nil {
@ -170,7 +173,6 @@ func resolveDeleteTargets(store *Store, exactArgs []string, globPatterns []strin
targetSet[arg] = struct{}{} targetSet[arg] = struct{}{}
targetKeys = append(targetKeys, formatted) targetKeys = append(targetKeys, formatted)
deleteTargets = append(deleteTargets, arg) deleteTargets = append(deleteTargets, arg)
matched = true
} }
} }
@ -233,14 +235,9 @@ func resolveDeleteTargets(store *Store, exactArgs []string, globPatterns []strin
} }
targetKeys = append(targetKeys, display) targetKeys = append(targetKeys, display)
deleteTargets = append(deleteTargets, full) deleteTargets = append(deleteTargets, full)
matched = true
} }
} }
} }
if len(globPatterns) > 0 && !matched {
return nil, nil, fmt.Errorf("cannot remove '%s': No matches for pattern", globPatterns[0])
}
return targetKeys, deleteTargets, nil return targetKeys, deleteTargets, nil
} }

View file

@ -142,7 +142,7 @@ func dump(cmd *cobra.Command, args []string) error {
} }
if len(matchers) > 0 && !matched { if len(matchers) > 0 && !matched {
return fmt.Errorf("cannot dump '%s': No matches for pattern", displayTarget) return fmt.Errorf("cannot dump '%s': No matches for pattern %s", displayTarget, formatGlobPatterns(globPatterns))
} }
return nil return nil
} }

View file

@ -1,6 +1,7 @@
package cmd package cmd
import ( import (
"fmt"
"strings" "strings"
"github.com/gobwas/glob" "github.com/gobwas/glob"
@ -52,3 +53,11 @@ func globMatch(matchers []glob.Glob, key string) bool {
} }
return false return false
} }
func formatGlobPatterns(patterns []string) string {
quoted := make([]string, 0, len(patterns))
for _, pattern := range patterns {
quoted = append(quoted, fmt.Sprintf("'%s'", pattern))
}
return strings.Join(quoted, ", ")
}

View file

@ -162,7 +162,7 @@ func list(cmd *cobra.Command, args []string) error {
} }
if len(matchers) > 0 && matchedCount == 0 { if len(matchers) > 0 && matchedCount == 0 {
return fmt.Errorf("cannot ls '%s': No matches for pattern", targetDB) return fmt.Errorf("cannot ls '%s': No matches for pattern %s", targetDB, formatGlobPatterns(globPatterns))
} }
applyColumnConstraints(tw, columnKinds, output, maxContentWidths) applyColumnConstraints(tw, columnKinds, output, maxContentWidths)

View file

@ -116,7 +116,7 @@ func restore(cmd *cobra.Command, args []string) error {
} }
if len(matchers) > 0 && !matched { if len(matchers) > 0 && !matched {
return fmt.Errorf("cannot restore '%s': No matches for pattern", displayTarget) return fmt.Errorf("cannot restore '%s': No matches for pattern %s", displayTarget, formatGlobPatterns(globPatterns))
} }
fmt.Fprintf(cmd.ErrOrStderr(), "Restored %d entries into @%s\n", restored, dbName) fmt.Fprintf(cmd.ErrOrStderr(), "Restored %d entries into @%s\n", restored, dbName)

View file

@ -5,4 +5,4 @@ $ pda dump --glob a*
{"key":"a1","value":"1","encoding":"text"} {"key":"a1","value":"1","encoding":"text"}
{"key":"a2","value":"2","encoding":"text"} {"key":"a2","value":"2","encoding":"text"}
$ pda dump --glob c* --> FAIL $ pda dump --glob c* --> FAIL
Error: cannot dump '@default': No matches for pattern Error: cannot dump '@default': No matches for pattern 'c*'

View file

@ -7,4 +7,4 @@ a2 2
$ pda ls lg --glob b* --format tsv $ pda ls lg --glob b* --format tsv
b1 3 b1 3
$ pda ls lg --glob c* --> FAIL $ pda ls lg --glob c* --> FAIL
Error: cannot ls '@lg': No matches for pattern Error: cannot ls '@lg': No matches for pattern 'c*'

View file

@ -12,4 +12,4 @@ $ pda get a2
$ pda get b1 --> FAIL $ pda get b1 --> FAIL
Error: cannot get 'b1': Key not found Error: cannot get 'b1': Key not found
$ pda restore --glob c* --file dumpfile --> FAIL $ pda restore --glob c* --file dumpfile --> FAIL
Error: cannot restore '@default': No matches for pattern Error: cannot restore '@default': No matches for pattern 'c*'