watch_command.go 3.3 KB

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