Merge pull request #6501 from sinsharat/feature_add_del_from-key

etcdctlv3: del command from-key feature added
release-3.1
Xiang Li 2016-09-22 09:15:04 -05:00 committed by GitHub
commit b1e4defc48
2 changed files with 23 additions and 5 deletions

View File

@ -192,6 +192,11 @@ func delTest(cx ctlCtx) {
[]string{"key", "--prefix"},
3,
},
{
[]kv{{"zoo1", "bar"}, {"zoo2", "bar2"}, {"zoo3", "bar3"}},
[]string{"zoo1", "--from-key"},
3,
},
}
for i, tt := range tests {

View File

@ -22,8 +22,9 @@ import (
)
var (
delPrefix bool
delPrevKV bool
delPrefix bool
delPrevKV bool
delFromKey bool
)
// 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(&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
}
@ -56,11 +57,16 @@ func getDelOp(cmd *cobra.Command, args []string) (string, []clientv3.OpOption) {
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."))
}
if delPrefix && delFromKey {
ExitWithError(ExitBadArgs, fmt.Errorf("`--prefix` and `--from-key` cannot be set at the same time, choose one."))
}
opts := []clientv3.OpOption{}
key := args[0]
if len(args) > 1 {
if delPrefix {
ExitWithError(ExitBadArgs, fmt.Errorf("too many arguments, only accept one argument when `--prefix` is set."))
if delPrefix || delFromKey {
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]))
}
@ -72,5 +78,12 @@ func getDelOp(cmd *cobra.Command, args []string) (string, []clientv3.OpOption) {
opts = append(opts, clientv3.WithPrevKV())
}
if delFromKey {
if len(key) == 0 {
key = "\x00"
}
opts = append(opts, clientv3.WithFromKey())
}
return key, opts
}