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.4 KiB

  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. cmdTail.Run = runTail // break init cycle
  13. }
  14. var cmdTail = &Command{
  15. UsageLine: "tail <wip> [-filer=localhost:8888]",
  16. Short: "see recent changes on a filer",
  17. Long: `See recent changes on a filer.
  18. `,
  19. }
  20. var (
  21. tailFiler = cmdTail.Flag.String("filer", "localhost:8888", "filer hostname:port")
  22. tailTarget = cmdTail.Flag.String("target", "/", "a folder or file on filer")
  23. )
  24. func runTail(cmd *Command, args []string) bool {
  25. grpcDialOption := security.LoadClientTLS(util.GetViper(), "grpc.client")
  26. tailErr := pb.WithFilerClient(*tailFiler, grpcDialOption, func(client filer_pb.SeaweedFilerClient) error {
  27. stream, err := client.ListenForEvents(context.Background(), &filer_pb.ListenForEventsRequest{
  28. ClientName: "tail",
  29. Directory: *tailTarget,
  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 tailErr != nil {
  47. fmt.Printf("tail %s: %v\n", *tailFiler, tailErr)
  48. }
  49. return true
  50. }