From 35ff70656b241d7939f79a55615e3ed55ed76dc1 Mon Sep 17 00:00:00 2001 From: sharat Date: Thu, 22 Sep 2016 14:52:35 +0530 Subject: [PATCH] etcdctlv3: del command from-key feature added --- e2e/ctl_v3_kv_test.go | 5 +++++ etcdctl/ctlv3/command/del_command.go | 23 ++++++++++++++++++----- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/e2e/ctl_v3_kv_test.go b/e2e/ctl_v3_kv_test.go index 12a1c805e..6039837a9 100644 --- a/e2e/ctl_v3_kv_test.go +++ b/e2e/ctl_v3_kv_test.go @@ -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 { diff --git a/etcdctl/ctlv3/command/del_command.go b/etcdctl/ctlv3/command/del_command.go index 5952b11b6..111ab0209 100644 --- a/etcdctl/ctlv3/command/del_command.go +++ b/etcdctl/ctlv3/command/del_command.go @@ -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 }