feat(cmd): made error messaging closer to coreutils
This commit is contained in:
parent
221d4cd921
commit
60d74e5ec1
4 changed files with 62 additions and 27 deletions
52
cmd/del.go
52
cmd/del.go
|
|
@ -22,8 +22,8 @@ THE SOFTWARE.
|
|||
package cmd
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/dgraph-io/badger/v4"
|
||||
|
|
@ -32,11 +32,12 @@ import (
|
|||
|
||||
// delCmd represents the set command
|
||||
var delCmd = &cobra.Command{
|
||||
Use: "del KEY[@DB]",
|
||||
Short: "Delete a key. Optionally specify a db.",
|
||||
Aliases: []string{"delete", "rm", "remove"},
|
||||
Args: cobra.ExactArgs(1),
|
||||
RunE: del,
|
||||
Use: "del KEY[@DB]",
|
||||
Short: "Delete a key. Optionally specify a db.",
|
||||
Aliases: []string{"delete", "rm", "remove"},
|
||||
Args: cobra.ExactArgs(1),
|
||||
RunE: del,
|
||||
SilenceUsage: true,
|
||||
}
|
||||
|
||||
func del(cmd *cobra.Command, args []string) error {
|
||||
|
|
@ -47,6 +48,14 @@ func del(cmd *cobra.Command, args []string) error {
|
|||
return err
|
||||
}
|
||||
|
||||
exists, err := keyExists(store, args[0])
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot remove '%s': %v", args[0], err)
|
||||
}
|
||||
if !exists {
|
||||
return fmt.Errorf("cannot remove '%s': No such key", args[0])
|
||||
}
|
||||
|
||||
targetKey, err := formatKeyForPrompt(store, args[0])
|
||||
if err != nil {
|
||||
return err
|
||||
|
|
@ -54,13 +63,12 @@ func del(cmd *cobra.Command, args []string) error {
|
|||
|
||||
if !force {
|
||||
var confirm string
|
||||
message := fmt.Sprintf("Are you sure you want to delete %q? (y/n)", targetKey)
|
||||
message := fmt.Sprintf("remove %q: are you sure? (y/n)", targetKey)
|
||||
fmt.Println(message)
|
||||
if _, err := fmt.Scanln(&confirm); err != nil {
|
||||
return err
|
||||
return fmt.Errorf("cannot remove '%s': %v", args[0], err)
|
||||
}
|
||||
if strings.ToLower(confirm) != "y" {
|
||||
fmt.Fprintf(os.Stderr, "Did not delete %q\n", targetKey)
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
|
@ -70,7 +78,10 @@ func del(cmd *cobra.Command, args []string) error {
|
|||
readonly: false,
|
||||
sync: false,
|
||||
transact: func(tx *badger.Txn, k []byte) error {
|
||||
return tx.Delete(k)
|
||||
if err := tx.Delete(k); errors.Is(err, badger.ErrKeyNotFound) {
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("cannot remove '%s': %v", args[0], err)
|
||||
},
|
||||
}
|
||||
|
||||
|
|
@ -82,6 +93,27 @@ func init() {
|
|||
rootCmd.AddCommand(delCmd)
|
||||
}
|
||||
|
||||
func keyExists(store *Store, arg string) (bool, error) {
|
||||
var notFound bool
|
||||
trans := TransactionArgs{
|
||||
key: arg,
|
||||
readonly: true,
|
||||
sync: false,
|
||||
transact: func(tx *badger.Txn, k []byte) error {
|
||||
if _, err := tx.Get(k); errors.Is(err, badger.ErrKeyNotFound) {
|
||||
notFound = true
|
||||
return nil
|
||||
} else {
|
||||
return err
|
||||
}
|
||||
},
|
||||
}
|
||||
if err := store.Transaction(trans); err != nil {
|
||||
return false, err
|
||||
}
|
||||
return !notFound, nil
|
||||
}
|
||||
|
||||
func formatKeyForPrompt(store *Store, arg string) (string, error) {
|
||||
_, db, err := store.parse(arg, true)
|
||||
if err != nil {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue