From 62ee484d12c2753402e67a4a8ba8490324e4731b Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Mon, 11 Jan 2021 00:03:13 -0800 Subject: refactoring --- weed/command/watch.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'weed/command/watch.go') diff --git a/weed/command/watch.go b/weed/command/watch.go index fd7dd6fb2..431a326d7 100644 --- a/weed/command/watch.go +++ b/weed/command/watch.go @@ -76,6 +76,10 @@ func runWatch(cmd *Command, args []string) bool { return false } + eachEntryFunc := func(resp *filer_pb.SubscribeMetadataResponse) { + fmt.Printf("dir:%s %+v\n", resp.Directory, resp.EventNotification) + } + watchErr := pb.WithFilerClient(*watchFiler, grpcDialOption, func(client filer_pb.SeaweedFilerClient) error { ctx, cancel := context.WithCancel(context.Background()) @@ -101,7 +105,7 @@ func runWatch(cmd *Command, args []string) bool { if !shouldPrint(resp) { continue } - fmt.Printf("dir:%s %+v\n", resp.Directory, resp.EventNotification) + eachEntryFunc(resp) } }) -- cgit v1.2.3 From 6e12a3a490fe5bc4f57fccf51a9fbdc9d1675ca5 Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Mon, 11 Jan 2021 02:08:26 -0800 Subject: refactoring --- weed/command/watch.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'weed/command/watch.go') diff --git a/weed/command/watch.go b/weed/command/watch.go index 431a326d7..8aad22019 100644 --- a/weed/command/watch.go +++ b/weed/command/watch.go @@ -76,8 +76,9 @@ func runWatch(cmd *Command, args []string) bool { return false } - eachEntryFunc := func(resp *filer_pb.SubscribeMetadataResponse) { + eachEntryFunc := func(resp *filer_pb.SubscribeMetadataResponse) error { fmt.Printf("dir:%s %+v\n", resp.Directory, resp.EventNotification) + return nil } watchErr := pb.WithFilerClient(*watchFiler, grpcDialOption, func(client filer_pb.SeaweedFilerClient) error { @@ -105,7 +106,9 @@ func runWatch(cmd *Command, args []string) bool { if !shouldPrint(resp) { continue } - eachEntryFunc(resp) + if err = eachEntryFunc(resp); err != nil { + return err + } } }) -- cgit v1.2.3 From 90c507761084821692fcbd25a0a729ce4bbc4806 Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Tue, 12 Jan 2021 18:48:01 -0800 Subject: rename "weed watch" to "weed filer.meta.tail" --- weed/command/watch.go | 120 -------------------------------------------------- 1 file changed, 120 deletions(-) delete mode 100644 weed/command/watch.go (limited to 'weed/command/watch.go') diff --git a/weed/command/watch.go b/weed/command/watch.go deleted file mode 100644 index 8aad22019..000000000 --- a/weed/command/watch.go +++ /dev/null @@ -1,120 +0,0 @@ -package command - -import ( - "context" - "fmt" - "io" - "path/filepath" - "strings" - "time" - - "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() { - cmdWatch.Run = runWatch // break init cycle -} - -var cmdWatch = &Command{ - UsageLine: "watch [-filer=localhost:8888] [-target=/]", - Short: "see recent changes on a filer", - Long: `See recent changes on a filer. - - `, -} - -var ( - watchFiler = cmdWatch.Flag.String("filer", "localhost:8888", "filer hostname:port") - watchTarget = cmdWatch.Flag.String("pathPrefix", "/", "path to a folder or file, or common prefix for the folders or files on filer") - watchStart = cmdWatch.Flag.Duration("timeAgo", 0, "start time before now. \"300ms\", \"1.5h\" or \"2h45m\". Valid time units are \"ns\", \"us\" (or \"µs\"), \"ms\", \"s\", \"m\", \"h\"") - watchPattern = cmdWatch.Flag.String("pattern", "", "full path or just filename pattern, ex: \"/home/?opher\", \"*.pdf\", see https://golang.org/pkg/path/filepath/#Match ") -) - -func runWatch(cmd *Command, args []string) bool { - - grpcDialOption := security.LoadClientTLS(util.GetViper(), "grpc.client") - - var filterFunc func(dir, fname string) bool - if *watchPattern != "" { - if strings.Contains(*watchPattern, "/") { - println("watch path pattern", *watchPattern) - filterFunc = func(dir, fname string) bool { - matched, err := filepath.Match(*watchPattern, dir+"/"+fname) - if err != nil { - fmt.Printf("error: %v", err) - } - return matched - } - } else { - println("watch file pattern", *watchPattern) - filterFunc = func(dir, fname string) bool { - matched, err := filepath.Match(*watchPattern, fname) - if err != nil { - fmt.Printf("error: %v", err) - } - return matched - } - } - } - - shouldPrint := func(resp *filer_pb.SubscribeMetadataResponse) bool { - if filterFunc == nil { - return true - } - if resp.EventNotification.OldEntry == nil && resp.EventNotification.NewEntry == nil { - return false - } - if resp.EventNotification.OldEntry != nil && filterFunc(resp.Directory, resp.EventNotification.OldEntry.Name) { - return true - } - if resp.EventNotification.NewEntry != nil && filterFunc(resp.EventNotification.NewParentPath, resp.EventNotification.NewEntry.Name) { - return true - } - return false - } - - eachEntryFunc := func(resp *filer_pb.SubscribeMetadataResponse) error { - fmt.Printf("dir:%s %+v\n", resp.Directory, resp.EventNotification) - return nil - } - - watchErr := pb.WithFilerClient(*watchFiler, grpcDialOption, func(client filer_pb.SeaweedFilerClient) error { - - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() - - stream, err := client.SubscribeMetadata(ctx, &filer_pb.SubscribeMetadataRequest{ - ClientName: "watch", - PathPrefix: *watchTarget, - SinceNs: time.Now().Add(-*watchStart).UnixNano(), - }) - 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 - } - if !shouldPrint(resp) { - continue - } - if err = eachEntryFunc(resp); err != nil { - return err - } - } - - }) - if watchErr != nil { - fmt.Printf("watch %s: %v\n", *watchFiler, watchErr) - } - - return true -} -- cgit v1.2.3