diff --git a/etcdctl/command/watch_command.go b/etcdctl/command/watch_command.go index fa987c826..81f69972f 100644 --- a/etcdctl/command/watch_command.go +++ b/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) + stop := false + w := ki.Watcher(key, &client.WatcherOptions{AfterIndex: uint64(index), Recursive: recursive}) - go func() { - <-sigch - os.Exit(0) - }() + sigch := make(chan os.Signal, 1) + signal.Notify(sigch, os.Interrupt) - receiver := make(chan *etcd.Response) - errCh := make(chan error, 1) - - go func() { - _, err := client.Watch(key, uint64(index), recursive, receiver, stop) - errCh <- err - }() - - for { - select { - case resp := <-receiver: - printAll(resp, c.GlobalString("output")) - case err := <-errCh: - handleError(-1, err) - } - } - - } 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 err != nil { - return nil, err + if resp.Node.Dir { + continue } - printAll(resp, c.GlobalString("output")) - } + printResponseKey(resp, c.GlobalString("output")) - return nil, nil + if !forever { + stop = true + } + } }