watch_command.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. "bufio"
  17. "fmt"
  18. "io"
  19. "os"
  20. "strings"
  21. "github.com/coreos/etcd/Godeps/_workspace/src/github.com/codegangsta/cli"
  22. "github.com/coreos/etcd/Godeps/_workspace/src/golang.org/x/net/context"
  23. "github.com/coreos/etcd/Godeps/_workspace/src/google.golang.org/grpc"
  24. pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
  25. )
  26. // NewWatchCommand returns the CLI command for "watch".
  27. func NewWatchCommand() cli.Command {
  28. return cli.Command{
  29. Name: "watch",
  30. Action: func(c *cli.Context) {
  31. watchCommandFunc(c)
  32. },
  33. }
  34. }
  35. // watchCommandFunc executes the "watch" command.
  36. func watchCommandFunc(c *cli.Context) {
  37. conn, err := grpc.Dial(c.GlobalString("endpoint"))
  38. if err != nil {
  39. panic(err)
  40. }
  41. wAPI := pb.NewWatchClient(conn)
  42. wStream, err := wAPI.Watch(context.TODO())
  43. if err != nil {
  44. panic(err)
  45. }
  46. go recvLoop(wStream)
  47. reader := bufio.NewReader(os.Stdin)
  48. for {
  49. l, err := reader.ReadString('\n')
  50. if err != nil {
  51. fmt.Fprintf(os.Stderr, "Error reading watch request line: %v", err)
  52. os.Exit(1)
  53. }
  54. l = strings.TrimSuffix(l, "\n")
  55. // TODO: support start and end revision
  56. segs := strings.Split(l, " ")
  57. if len(segs) != 2 {
  58. fmt.Fprintf(os.Stderr, "Invaild watch request format: use watch key or watchprefix prefix\n")
  59. continue
  60. }
  61. var r *pb.WatchRequest
  62. switch segs[0] {
  63. case "watch":
  64. r = &pb.WatchRequest{Key: []byte(segs[1])}
  65. case "watchprefix":
  66. r = &pb.WatchRequest{Prefix: []byte(segs[1])}
  67. default:
  68. fmt.Fprintf(os.Stderr, "Invaild watch request format: use watch key or watchprefix prefix\n")
  69. continue
  70. }
  71. err = wStream.Send(r)
  72. if err != nil {
  73. fmt.Fprintf(os.Stderr, "Error sending request to server: %v\n", err)
  74. }
  75. }
  76. }
  77. func recvLoop(wStream pb.Watch_WatchClient) {
  78. for {
  79. resp, err := wStream.Recv()
  80. if err == io.EOF {
  81. os.Exit(0)
  82. }
  83. if err != nil {
  84. panic(err)
  85. }
  86. fmt.Printf("%s: %s %s\n", resp.Event.Type, string(resp.Event.Kv.Key), string(resp.Event.Kv.Value))
  87. }
  88. }