watch_command.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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/spf13/cobra"
  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 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. endpoint, err := cmd.Flags().GetString("endpoint")
  37. if err != nil {
  38. ExitWithError(ExitInvalidInput, err)
  39. }
  40. conn, err := grpc.Dial(endpoint)
  41. if err != nil {
  42. ExitWithError(ExitBadConnection, err)
  43. }
  44. wAPI := pb.NewWatchClient(conn)
  45. wStream, err := wAPI.Watch(context.TODO())
  46. if err != nil {
  47. ExitWithError(ExitBadConnection, err)
  48. }
  49. go recvLoop(wStream)
  50. reader := bufio.NewReader(os.Stdin)
  51. for {
  52. l, err := reader.ReadString('\n')
  53. if err != nil {
  54. ExitWithError(ExitInvalidInput, fmt.Errorf("Error reading watch request line: %v", err))
  55. }
  56. l = strings.TrimSuffix(l, "\n")
  57. // TODO: support start and end revision
  58. segs := strings.Split(l, " ")
  59. if len(segs) != 2 {
  60. fmt.Fprintf(os.Stderr, "Invalid watch request format: use watch key or watchprefix prefix\n")
  61. continue
  62. }
  63. var r *pb.WatchRequest
  64. switch segs[0] {
  65. case "watch":
  66. r = &pb.WatchRequest{CreateRequest: &pb.WatchCreateRequest{Key: []byte(segs[1])}}
  67. case "watchprefix":
  68. r = &pb.WatchRequest{CreateRequest: &pb.WatchCreateRequest{Prefix: []byte(segs[1])}}
  69. default:
  70. fmt.Fprintf(os.Stderr, "Invalid watch request format: use watch key or watchprefix prefix\n")
  71. continue
  72. }
  73. err = wStream.Send(r)
  74. if err != nil {
  75. fmt.Fprintf(os.Stderr, "Error sending request to server: %v\n", err)
  76. }
  77. }
  78. }
  79. func recvLoop(wStream pb.Watch_WatchClient) {
  80. for {
  81. resp, err := wStream.Recv()
  82. if err == io.EOF {
  83. os.Exit(ExitSuccess)
  84. }
  85. if err != nil {
  86. ExitWithError(ExitError, err)
  87. }
  88. evs := resp.Events
  89. for _, ev := range evs {
  90. fmt.Printf("%s: %s %s\n", ev.Type, string(ev.Kv.Key), string(ev.Kv.Value))
  91. }
  92. }
  93. }