watch_command.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. Copyright 2014 CoreOS, Inc.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package command
  14. import (
  15. "errors"
  16. "os"
  17. "os/signal"
  18. "github.com/coreos/etcd/Godeps/_workspace/src/github.com/codegangsta/cli"
  19. "github.com/coreos/etcd/Godeps/_workspace/src/github.com/coreos/go-etcd/etcd"
  20. )
  21. // NewWatchCommand returns the CLI command for "watch".
  22. func NewWatchCommand() cli.Command {
  23. return cli.Command{
  24. Name: "watch",
  25. Usage: "watch a key for changes",
  26. Flags: []cli.Flag{
  27. cli.BoolFlag{Name: "forever", Usage: "forever watch a key until CTRL+C"},
  28. cli.IntFlag{Name: "after-index", Value: 0, Usage: "watch after the given index"},
  29. cli.BoolFlag{Name: "recursive", Usage: "returns all values for key and child keys"},
  30. },
  31. Action: func(c *cli.Context) {
  32. handleKey(c, watchCommandFunc)
  33. },
  34. }
  35. }
  36. // watchCommandFunc executes the "watch" command.
  37. func watchCommandFunc(c *cli.Context, client *etcd.Client) (*etcd.Response, error) {
  38. if len(c.Args()) == 0 {
  39. return nil, errors.New("Key required")
  40. }
  41. key := c.Args()[0]
  42. recursive := c.Bool("recursive")
  43. forever := c.Bool("forever")
  44. index := 0
  45. if c.Int("after-index") != 0 {
  46. index = c.Int("after-index") + 1
  47. }
  48. if forever {
  49. sigch := make(chan os.Signal, 1)
  50. signal.Notify(sigch, os.Interrupt)
  51. stop := make(chan bool)
  52. go func() {
  53. <-sigch
  54. os.Exit(0)
  55. }()
  56. receiver := make(chan *etcd.Response)
  57. errCh := make(chan error, 1)
  58. go func() {
  59. _, err := client.Watch(key, uint64(index), recursive, receiver, stop)
  60. errCh <- err
  61. }()
  62. for {
  63. select {
  64. case resp := <-receiver:
  65. printAll(resp, c.GlobalString("output"))
  66. case err := <-errCh:
  67. handleError(-1, err)
  68. }
  69. }
  70. } else {
  71. var resp *etcd.Response
  72. var err error
  73. resp, err = client.Watch(key, uint64(index), recursive, nil, nil)
  74. if err != nil {
  75. handleError(ErrorFromEtcd, err)
  76. }
  77. if err != nil {
  78. return nil, err
  79. }
  80. printAll(resp, c.GlobalString("output"))
  81. }
  82. return nil, nil
  83. }