watch_command.go 2.4 KB

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