You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

63 lines
1.5 KiB

5 years ago
  1. package command
  2. import (
  3. "context"
  4. "fmt"
  5. "io"
  6. "github.com/chrislusf/seaweedfs/weed/pb"
  7. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  8. "github.com/chrislusf/seaweedfs/weed/security"
  9. "github.com/chrislusf/seaweedfs/weed/util"
  10. )
  11. func init() {
  12. cmdWatch.Run = runWatch // break init cycle
  13. }
  14. var cmdWatch = &Command{
  15. UsageLine: "watch <wip> [-filer=localhost:8888] [-target=/]",
  16. Short: "see recent changes on a filer",
  17. Long: `See recent changes on a filer.
  18. `,
  19. }
  20. var (
  21. watchFiler = cmdWatch.Flag.String("filer", "localhost:8888", "filer hostname:port")
  22. watchTarget = cmdWatch.Flag.String("pathPrefix", "/", "path to a folder or file, or common prefix for the folders or files on filer")
  23. )
  24. func runWatch(cmd *Command, args []string) bool {
  25. grpcDialOption := security.LoadClientTLS(util.GetViper(), "grpc.client")
  26. watchErr := pb.WithFilerClient(*watchFiler, grpcDialOption, func(client filer_pb.SeaweedFilerClient) error {
  27. stream, err := client.SubscribeMetadata(context.Background(), &filer_pb.SubscribeMetadataRequest{
  28. ClientName: "watch",
  29. PathPrefix: *watchTarget,
  30. SinceNs: 0,
  31. })
  32. if err != nil {
  33. return fmt.Errorf("listen: %v", err)
  34. }
  35. for {
  36. resp, listenErr := stream.Recv()
  37. if listenErr == io.EOF {
  38. return nil
  39. }
  40. if listenErr != nil {
  41. return listenErr
  42. }
  43. fmt.Printf("events: %+v\n", resp.EventNotification)
  44. }
  45. })
  46. if watchErr != nil {
  47. fmt.Printf("watch %s: %v\n", *watchFiler, watchErr)
  48. }
  49. return true
  50. }