watch_command.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. "strconv"
  21. "strings"
  22. "github.com/coreos/etcd/Godeps/_workspace/src/github.com/spf13/cobra"
  23. "github.com/coreos/etcd/Godeps/_workspace/src/golang.org/x/net/context"
  24. "github.com/coreos/etcd/Godeps/_workspace/src/google.golang.org/grpc"
  25. pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
  26. )
  27. // NewWatchCommand returns the cobra command for "watch".
  28. func NewWatchCommand() *cobra.Command {
  29. return &cobra.Command{
  30. Use: "watch",
  31. Short: "Watch watches the events happening or happened.",
  32. Run: watchCommandFunc,
  33. }
  34. }
  35. // watchCommandFunc executes the "watch" command.
  36. func watchCommandFunc(cmd *cobra.Command, args []string) {
  37. endpoint, err := cmd.Flags().GetString("endpoint")
  38. if err != nil {
  39. ExitWithError(ExitInvalidInput, err)
  40. }
  41. conn, err := grpc.Dial(endpoint)
  42. if err != nil {
  43. ExitWithError(ExitBadConnection, err)
  44. }
  45. wAPI := pb.NewWatchClient(conn)
  46. wStream, err := wAPI.Watch(context.TODO())
  47. if err != nil {
  48. ExitWithError(ExitBadConnection, err)
  49. }
  50. go recvLoop(wStream)
  51. reader := bufio.NewReader(os.Stdin)
  52. for {
  53. l, err := reader.ReadString('\n')
  54. if err != nil {
  55. ExitWithError(ExitInvalidInput, fmt.Errorf("Error reading watch request line: %v", err))
  56. }
  57. l = strings.TrimSuffix(l, "\n")
  58. // TODO: support start and end revision
  59. segs := strings.Split(l, " ")
  60. if len(segs) != 2 {
  61. fmt.Fprintf(os.Stderr, "Invalid watch request format: use \"watch [key]\", \"watchprefix [prefix]\" or \"cancel [watcher ID]\"\n")
  62. continue
  63. }
  64. var r *pb.WatchRequest
  65. switch segs[0] {
  66. case "watch":
  67. r = &pb.WatchRequest{CreateRequest: &pb.WatchCreateRequest{Key: []byte(segs[1])}}
  68. case "watchprefix":
  69. r = &pb.WatchRequest{CreateRequest: &pb.WatchCreateRequest{Prefix: []byte(segs[1])}}
  70. case "cancel":
  71. id, perr := strconv.ParseInt(segs[1], 10, 64)
  72. if perr != nil {
  73. fmt.Fprintf(os.Stderr, "Invalid cancel ID (%v)\n", perr)
  74. continue
  75. }
  76. r = &pb.WatchRequest{CancelRequest: &pb.WatchCancelRequest{WatchId: id}}
  77. default:
  78. fmt.Fprintf(os.Stderr, "Invalid watch request type: use watch, watchprefix or cancel\n")
  79. continue
  80. }
  81. err = wStream.Send(r)
  82. if err != nil {
  83. fmt.Fprintf(os.Stderr, "Error sending request to server: %v\n", err)
  84. }
  85. }
  86. }
  87. func recvLoop(wStream pb.Watch_WatchClient) {
  88. for {
  89. resp, err := wStream.Recv()
  90. if err == io.EOF {
  91. os.Exit(ExitSuccess)
  92. }
  93. if err != nil {
  94. ExitWithError(ExitError, err)
  95. }
  96. switch {
  97. // TODO: handle canceled/compacted and other control response types
  98. case resp.Created:
  99. fmt.Printf("watcher created: id %08x\n", resp.WatchId)
  100. case resp.Canceled:
  101. fmt.Printf("watcher canceled: id %08x\n", resp.WatchId)
  102. default:
  103. for _, ev := range resp.Events {
  104. fmt.Printf("%s: %s %s\n", ev.Type, string(ev.Kv.Key), string(ev.Kv.Value))
  105. }
  106. }
  107. }
  108. }