watch_command.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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{
  68. RequestUnion: &pb.WatchRequest_CreateRequest{
  69. CreateRequest: &pb.WatchCreateRequest{
  70. Key: []byte(segs[1])}}}
  71. case "watchprefix":
  72. r = &pb.WatchRequest{
  73. RequestUnion: &pb.WatchRequest_CreateRequest{
  74. CreateRequest: &pb.WatchCreateRequest{
  75. Prefix: []byte(segs[1])}}}
  76. case "cancel":
  77. id, perr := strconv.ParseInt(segs[1], 10, 64)
  78. if perr != nil {
  79. fmt.Fprintf(os.Stderr, "Invalid cancel ID (%v)\n", perr)
  80. continue
  81. }
  82. r = &pb.WatchRequest{
  83. RequestUnion: &pb.WatchRequest_CancelRequest{
  84. CancelRequest: &pb.WatchCancelRequest{
  85. WatchId: id}}}
  86. default:
  87. fmt.Fprintf(os.Stderr, "Invalid watch request type: use watch, watchprefix or cancel\n")
  88. continue
  89. }
  90. err = wStream.Send(r)
  91. if err != nil {
  92. fmt.Fprintf(os.Stderr, "Error sending request to server: %v\n", err)
  93. }
  94. }
  95. }
  96. func recvLoop(wStream pb.Watch_WatchClient) {
  97. for {
  98. resp, err := wStream.Recv()
  99. if err == io.EOF {
  100. os.Exit(ExitSuccess)
  101. }
  102. if err != nil {
  103. ExitWithError(ExitError, err)
  104. }
  105. switch {
  106. // TODO: handle canceled/compacted and other control response types
  107. case resp.Created:
  108. fmt.Printf("watcher created: id %08x\n", resp.WatchId)
  109. case resp.Canceled:
  110. fmt.Printf("watcher canceled: id %08x\n", resp.WatchId)
  111. default:
  112. for _, ev := range resp.Events {
  113. fmt.Printf("%s: %s %s\n", ev.Type, string(ev.Kv.Key), string(ev.Kv.Value))
  114. }
  115. }
  116. }
  117. }