mirror of
https://github.com/etcd-io/etcd.git
synced 2024-09-27 06:25:44 +00:00
etcdctlv3: del command from-key feature added
This commit is contained in:
parent
4de39d3683
commit
35ff70656b
@ -192,6 +192,11 @@ func delTest(cx ctlCtx) {
|
|||||||
[]string{"key", "--prefix"},
|
[]string{"key", "--prefix"},
|
||||||
3,
|
3,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
[]kv{{"zoo1", "bar"}, {"zoo2", "bar2"}, {"zoo3", "bar3"}},
|
||||||
|
[]string{"zoo1", "--from-key"},
|
||||||
|
3,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for i, tt := range tests {
|
for i, tt := range tests {
|
||||||
|
@ -22,8 +22,9 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
delPrefix bool
|
delPrefix bool
|
||||||
delPrevKV bool
|
delPrevKV bool
|
||||||
|
delFromKey bool
|
||||||
)
|
)
|
||||||
|
|
||||||
// NewDelCommand returns the cobra command for "del".
|
// NewDelCommand returns the cobra command for "del".
|
||||||
@ -36,7 +37,7 @@ func NewDelCommand() *cobra.Command {
|
|||||||
|
|
||||||
cmd.Flags().BoolVar(&delPrefix, "prefix", false, "delete keys with matching prefix")
|
cmd.Flags().BoolVar(&delPrefix, "prefix", false, "delete keys with matching prefix")
|
||||||
cmd.Flags().BoolVar(&delPrevKV, "prev-kv", false, "return deleted key-value pairs")
|
cmd.Flags().BoolVar(&delPrevKV, "prev-kv", false, "return deleted key-value pairs")
|
||||||
|
cmd.Flags().BoolVar(&delFromKey, "from-key", false, "delete keys that are greater than or equal to the given key using byte compare")
|
||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -56,11 +57,16 @@ func getDelOp(cmd *cobra.Command, args []string) (string, []clientv3.OpOption) {
|
|||||||
if len(args) == 0 || len(args) > 2 {
|
if len(args) == 0 || len(args) > 2 {
|
||||||
ExitWithError(ExitBadArgs, fmt.Errorf("del command needs one argument as key and an optional argument as range_end."))
|
ExitWithError(ExitBadArgs, fmt.Errorf("del command needs one argument as key and an optional argument as range_end."))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if delPrefix && delFromKey {
|
||||||
|
ExitWithError(ExitBadArgs, fmt.Errorf("`--prefix` and `--from-key` cannot be set at the same time, choose one."))
|
||||||
|
}
|
||||||
|
|
||||||
opts := []clientv3.OpOption{}
|
opts := []clientv3.OpOption{}
|
||||||
key := args[0]
|
key := args[0]
|
||||||
if len(args) > 1 {
|
if len(args) > 1 {
|
||||||
if delPrefix {
|
if delPrefix || delFromKey {
|
||||||
ExitWithError(ExitBadArgs, fmt.Errorf("too many arguments, only accept one argument when `--prefix` is set."))
|
ExitWithError(ExitBadArgs, fmt.Errorf("too many arguments, only accept one argument when `--prefix` or `--from-key` is set."))
|
||||||
}
|
}
|
||||||
opts = append(opts, clientv3.WithRange(args[1]))
|
opts = append(opts, clientv3.WithRange(args[1]))
|
||||||
}
|
}
|
||||||
@ -72,5 +78,12 @@ func getDelOp(cmd *cobra.Command, args []string) (string, []clientv3.OpOption) {
|
|||||||
opts = append(opts, clientv3.WithPrevKV())
|
opts = append(opts, clientv3.WithPrevKV())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if delFromKey {
|
||||||
|
if len(key) == 0 {
|
||||||
|
key = "\x00"
|
||||||
|
}
|
||||||
|
opts = append(opts, clientv3.WithFromKey())
|
||||||
|
}
|
||||||
|
|
||||||
return key, opts
|
return key, opts
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user