Browse Source

Merge pull request #5582 from gyuho/watch_range_end

etcdctl: support watch with range_end
Gyu-Ho Lee 9 years ago
parent
commit
9a14b796e0
3 changed files with 31 additions and 12 deletions
  1. 8 3
      e2e/ctl_v3_watch_test.go
  2. 2 2
      etcdctl/README.md
  3. 21 7
      etcdctl/ctlv3/command/watch_command.go

+ 8 - 3
e2e/ctl_v3_watch_test.go

@@ -45,21 +45,26 @@ func watchTest(cx ctlCtx) {
 
 		wkv []kv
 	}{
-		{
+		{ // watch 1 key
 			[]kv{{"sample", "value"}},
 			[]string{"sample", "--rev", "1"},
 			[]kv{{"sample", "value"}},
 		},
-		{
+		{ // watch 3 keys by prefix
 			[]kv{{"key1", "val1"}, {"key2", "val2"}, {"key3", "val3"}},
 			[]string{"key", "--rev", "1", "--prefix"},
 			[]kv{{"key1", "val1"}, {"key2", "val2"}, {"key3", "val3"}},
 		},
-		{
+		{ // watch by revision
 			[]kv{{"etcd", "revision_1"}, {"etcd", "revision_2"}, {"etcd", "revision_3"}},
 			[]string{"etcd", "--rev", "2"},
 			[]kv{{"etcd", "revision_2"}, {"etcd", "revision_3"}},
 		},
+		{ // watch 3 keys by range
+			[]kv{{"key1", "val1"}, {"key2", "val2"}, {"key3", "val3"}},
+			[]string{"key", "key3", "--rev", "1"},
+			[]kv{{"key1", "val1"}, {"key2", "val2"}},
+		},
 	}
 
 	for i, tt := range tests {

+ 2 - 2
etcdctl/README.md

@@ -219,9 +219,9 @@ OK
 OK
 ````
 
-### WATCH [options] [key or prefix]
+### WATCH [options] [key or prefix] [range_end]
 
-Watch watches events stream on keys or prefixes. The watch command runs until it encounters an error or is terminated by the user.
+Watch watches events stream on keys or prefixes, [key or prefix, range_end) if `range-end` is given. The watch command runs until it encounters an error or is terminated by the user.
 
 #### Options
 

+ 21 - 7
etcdctl/ctlv3/command/watch_command.go

@@ -34,7 +34,7 @@ var (
 // NewWatchCommand returns the cobra command for "watch".
 func NewWatchCommand() *cobra.Command {
 	cmd := &cobra.Command{
-		Use:   "watch [key or prefix]",
+		Use:   "watch [options] [key or prefix] [range_end]",
 		Short: "Watch watches events stream on keys or prefixes.",
 		Run:   watchCommandFunc,
 	}
@@ -52,17 +52,24 @@ func watchCommandFunc(cmd *cobra.Command, args []string) {
 		watchInteractiveFunc(cmd, args)
 		return
 	}
-
-	if len(args) != 1 {
-		ExitWithError(ExitBadArgs, fmt.Errorf("watch in non-interactive mode requires an argument as key or prefix"))
+	if len(args) < 1 || len(args) > 2 {
+		ExitWithError(ExitBadArgs, fmt.Errorf("watch in non-interactive mode requires one or two arguments as key or prefix, with range end"))
 	}
 
 	opts := []clientv3.OpOption{clientv3.WithRev(watchRev)}
+	key := args[0]
+	if len(args) == 2 {
+		if watchPrefix {
+			ExitWithError(ExitBadArgs, fmt.Errorf("`range_end` and `--prefix` cannot be set at the same time, choose one"))
+		}
+		opts = append(opts, clientv3.WithRange(args[1]))
+	}
+
 	if watchPrefix {
 		opts = append(opts, clientv3.WithPrefix())
 	}
 	c := mustClientFromCmd(cmd)
-	wc := c.Watch(context.TODO(), args[0], opts...)
+	wc := c.Watch(context.TODO(), key, opts...)
 	printWatchCh(wc)
 	err := c.Close()
 	if err == nil {
@@ -101,8 +108,8 @@ func watchInteractiveFunc(cmd *cobra.Command, args []string) {
 			continue
 		}
 		moreargs := flagset.Args()
-		if len(moreargs) != 1 {
-			fmt.Fprintf(os.Stderr, "Invalid command %s (Too many arguments)\n", l)
+		if len(moreargs) < 1 || len(moreargs) > 2 {
+			fmt.Fprintf(os.Stderr, "Invalid command %s (Too few or many arguments)\n", l)
 			continue
 		}
 		var key string
@@ -111,6 +118,13 @@ func watchInteractiveFunc(cmd *cobra.Command, args []string) {
 			key = moreargs[0]
 		}
 		opts := []clientv3.OpOption{clientv3.WithRev(watchRev)}
+		if len(moreargs) == 2 {
+			if watchPrefix {
+				fmt.Fprintf(os.Stderr, "`range_end` and `--prefix` cannot be set at the same time, choose one\n")
+				continue
+			}
+			opts = append(opts, clientv3.WithRange(moreargs[1]))
+		}
 		if watchPrefix {
 			opts = append(opts, clientv3.WithPrefix())
 		}