From ebff05bd4bfd88412c66c3074fc0b32420fc90e5 Mon Sep 17 00:00:00 2001 From: nxtcoder17 Date: Sun, 24 Nov 2024 21:24:15 +0530 Subject: [PATCH] feat: adds support to watch only specified suffixes --- Taskfile.yml | 3 +-- main.go | 22 ++++++++++++++++------ pkg/watcher/watcher.go | 30 ++++++++++++++++++++++++------ 3 files changed, 41 insertions(+), 14 deletions(-) diff --git a/Taskfile.yml b/Taskfile.yml index 01dfaf6..b1405bd 100644 --- a/Taskfile.yml +++ b/Taskfile.yml @@ -32,9 +32,8 @@ tasks: example:http-server: cmds: - - task: dev:build - |+ - {{.Bin}} --debug --ext '.go' --exec 'go run ./examples/http-server/main.go' + go run ./examples/http-server/main.go install: cmds: diff --git a/main.go b/main.go index 65c14c1..7d46046 100644 --- a/main.go +++ b/main.go @@ -35,10 +35,14 @@ func main() { Value: false, }, &cli.StringFlag{ - Name: "command", - Usage: "specifies command to execute on file change", - Required: false, - Aliases: []string{"c"}, + Name: "command", + Usage: "specifies command to execute on file change", + HasBeenSet: false, + Value: "", + Destination: new(string), + Aliases: []string{"c"}, + EnvVars: []string{}, + TakesFile: false, }, &cli.PathFlag{ Name: "dir", @@ -52,7 +56,6 @@ func main() { return cwd }(), Aliases: []string{"d"}, - EnvVars: nil, }, &cli.StringSliceFlag{ Name: "ignore-suffixes", @@ -60,6 +63,12 @@ func main() { Required: false, Aliases: []string{"i"}, }, + &cli.StringSliceFlag{ + Name: "only-watch-suffixes", + Usage: "files suffixes to watch", + Required: false, + Aliases: []string{"w"}, + }, &cli.StringSliceFlag{ Name: "exclude-dir", Usage: "directory to exclude from watching", @@ -104,13 +113,13 @@ func main() { execArgs = strings.Split(s[1], " ") } } else { - logger.Debug("no command specified, using args") if cctx.Args().Len() == 0 { return fmt.Errorf("no command specified") } execCmd = cctx.Args().First() execArgs = cctx.Args().Tail() + logger.Debug("no command specified, using", "command", execCmd, "args", execArgs) } ex := executor.NewExecutor(executor.ExecutorArgs{ @@ -126,6 +135,7 @@ func main() { watcher, err := fswatcher.NewWatcher(fswatcher.WatcherArgs{ Logger: logger, + OnlyWatchSuffixes: cctx.StringSlice("only-watch-suffixes"), IgnoreSuffixes: cctx.StringSlice("ignore-suffixes"), ExcludeDirs: cctx.StringSlice("exclude-dir"), UseDefaultIgnoreList: !cctx.Bool("no-global-ignore"), diff --git a/pkg/watcher/watcher.go b/pkg/watcher/watcher.go index 0fc537e..85fe549 100644 --- a/pkg/watcher/watcher.go +++ b/pkg/watcher/watcher.go @@ -29,9 +29,10 @@ type fsnWatcher struct { directoryCount int - Logger *slog.Logger - IgnoreSuffixes []string - ExcludeDirs map[string]struct{} + Logger *slog.Logger + OnlyWatchSuffixes []string + IgnoreSuffixes []string + ExcludeDirs map[string]struct{} } type Event fsnotify.Event @@ -65,7 +66,18 @@ func (f fsnWatcher) ignoreEvent(event fsnotify.Event) bool { return true } } - return false + + if len(f.OnlyWatchSuffixes) == 0 { + return false + } + + for _, suffix := range f.OnlyWatchSuffixes { + if strings.HasSuffix(event.Name, suffix) { + return false + } + } + + return true } func (f *fsnWatcher) WatchEvents(watcherFunc func(event Event, fp string) error) { @@ -85,9 +97,14 @@ func (f *fsnWatcher) WatchEvents(watcherFunc func(event Event, fp string) error) continue } + fi, err := os.Stat(event.Name) + if err != nil { + return + } + eInfo, ok := f.eventMap[event.Name] if !ok { - eInfo = eventInfo{Time: time.Time{}, FileInfo: nil, Counter: 0} + eInfo = eventInfo{Time: time.Now(), FileInfo: nil, Counter: 0} } eInfo.Counter += 1 f.eventMap[event.Name] = eInfo @@ -169,6 +186,7 @@ func (f *fsnWatcher) Close() error { type WatcherArgs struct { Logger *slog.Logger + OnlyWatchSuffixes []string IgnoreSuffixes []string ExcludeDirs []string UseDefaultIgnoreList bool @@ -193,5 +211,5 @@ func NewWatcher(args WatcherArgs) (Watcher, error) { args.Logger.Error("failed to create watcher, got", "err", err) return nil, err } - return &fsnWatcher{watcher: watcher, Logger: args.Logger, ExcludeDirs: excludeDirs, IgnoreSuffixes: args.IgnoreSuffixes}, nil + return &fsnWatcher{watcher: watcher, Logger: args.Logger, ExcludeDirs: excludeDirs, IgnoreSuffixes: args.IgnoreSuffixes, OnlyWatchSuffixes: args.OnlyWatchSuffixes}, nil }