2020-04-05 07:51:16 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/pb"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/security"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/util"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2020-04-05 19:39:20 +00:00
|
|
|
cmdWatch.Run = runWatch // break init cycle
|
2020-04-05 07:51:16 +00:00
|
|
|
}
|
|
|
|
|
2020-04-05 19:39:20 +00:00
|
|
|
var cmdWatch = &Command{
|
|
|
|
UsageLine: "watch <wip> [-filer=localhost:8888] [-target=/]",
|
2020-04-05 07:51:16 +00:00
|
|
|
Short: "see recent changes on a filer",
|
|
|
|
Long: `See recent changes on a filer.
|
|
|
|
|
|
|
|
`,
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
2020-04-05 19:39:20 +00:00
|
|
|
watchFiler = cmdWatch.Flag.String("filer", "localhost:8888", "filer hostname:port")
|
|
|
|
watchTarget = cmdWatch.Flag.String("target", "/", "a folder or file on filer")
|
2020-04-05 07:51:16 +00:00
|
|
|
)
|
|
|
|
|
2020-04-05 19:39:20 +00:00
|
|
|
func runWatch(cmd *Command, args []string) bool {
|
2020-04-05 07:51:16 +00:00
|
|
|
|
|
|
|
grpcDialOption := security.LoadClientTLS(util.GetViper(), "grpc.client")
|
|
|
|
|
2020-04-05 19:39:20 +00:00
|
|
|
watchErr := pb.WithFilerClient(*watchFiler, grpcDialOption, func(client filer_pb.SeaweedFilerClient) error {
|
2020-04-05 07:51:16 +00:00
|
|
|
|
|
|
|
stream, err := client.ListenForEvents(context.Background(), &filer_pb.ListenForEventsRequest{
|
2020-04-05 19:39:20 +00:00
|
|
|
ClientName: "watch",
|
|
|
|
Directory: *watchTarget,
|
2020-04-05 07:51:16 +00:00
|
|
|
SinceNs: 0,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("listen: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
|
|
|
resp, listenErr := stream.Recv()
|
|
|
|
if listenErr == io.EOF {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if listenErr != nil {
|
|
|
|
return listenErr
|
|
|
|
}
|
|
|
|
fmt.Printf("events: %+v\n", resp.EventNotification)
|
|
|
|
}
|
|
|
|
|
|
|
|
})
|
2020-04-05 19:39:20 +00:00
|
|
|
if watchErr != nil {
|
|
|
|
fmt.Printf("watch %s: %v\n", *watchFiler, watchErr)
|
2020-04-05 07:51:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|