Bläddra i källkod

etcdctl: watch use etcd/client

Xiang Li 10 år sedan
förälder
incheckning
5b01b3877f
1 ändrade filer med 21 tillägg och 39 borttagningar
  1. 21 39
      etcdctl/command/watch_command.go

+ 21 - 39
etcdctl/command/watch_command.go

@@ -20,7 +20,8 @@ import (
 	"os/signal"
 
 	"github.com/coreos/etcd/Godeps/_workspace/src/github.com/codegangsta/cli"
-	"github.com/coreos/etcd/Godeps/_workspace/src/github.com/coreos/go-etcd/etcd"
+	"github.com/coreos/etcd/Godeps/_workspace/src/golang.org/x/net/context"
+	"github.com/coreos/etcd/client"
 )
 
 // NewWatchCommand returns the CLI command for "watch".
@@ -34,66 +35,47 @@ func NewWatchCommand() cli.Command {
 			cli.BoolFlag{Name: "recursive", Usage: "returns all values for key and child keys"},
 		},
 		Action: func(c *cli.Context) {
-			handleKey(c, watchCommandFunc)
+			watchCommandFunc(c, mustNewKeyAPI(c))
 		},
 	}
 }
 
 // watchCommandFunc executes the "watch" command.
-func watchCommandFunc(c *cli.Context, client *etcd.Client) (*etcd.Response, error) {
+func watchCommandFunc(c *cli.Context, ki client.KeysAPI) {
 	if len(c.Args()) == 0 {
-		return nil, errors.New("Key required")
+		handleError(ExitBadArgs, errors.New("key required"))
 	}
 	key := c.Args()[0]
 	recursive := c.Bool("recursive")
 	forever := c.Bool("forever")
-
 	index := 0
 	if c.Int("after-index") != 0 {
 		index = c.Int("after-index") + 1
 	}
 
-	if forever {
-		sigch := make(chan os.Signal, 1)
-		signal.Notify(sigch, os.Interrupt)
-		stop := make(chan bool)
-
-		go func() {
-			<-sigch
-			os.Exit(0)
-		}()
-
-		receiver := make(chan *etcd.Response)
-		errCh := make(chan error, 1)
-
-		go func() {
-			_, err := client.Watch(key, uint64(index), recursive, receiver, stop)
-			errCh <- err
-		}()
+	stop := false
+	w := ki.Watcher(key, &client.WatcherOptions{AfterIndex: uint64(index), Recursive: recursive})
 
-		for {
-			select {
-			case resp := <-receiver:
-				printAll(resp, c.GlobalString("output"))
-			case err := <-errCh:
-				handleError(-1, err)
-			}
-		}
+	sigch := make(chan os.Signal, 1)
+	signal.Notify(sigch, os.Interrupt)
 
-	} else {
-		var resp *etcd.Response
-		var err error
-		resp, err = client.Watch(key, uint64(index), recursive, nil, nil)
+	go func() {
+		<-sigch
+		os.Exit(0)
+	}()
 
+	for !stop {
+		resp, err := w.Next(context.TODO())
 		if err != nil {
 			handleError(ExitServerError, err)
 		}
+		if resp.Node.Dir {
+			continue
+		}
+		printResponseKey(resp, c.GlobalString("output"))
 
-		if err != nil {
-			return nil, err
+		if !forever {
+			stop = true
 		}
-		printAll(resp, c.GlobalString("output"))
 	}
-
-	return nil, nil
 }