Skip to content

Commit

Permalink
feat: adds support to watch only specified suffixes
Browse files Browse the repository at this point in the history
  • Loading branch information
nxtcoder17 committed Nov 24, 2024
1 parent 66e62be commit ebff05b
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 14 deletions.
3 changes: 1 addition & 2 deletions Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
22 changes: 16 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -52,14 +56,19 @@ func main() {
return cwd
}(),
Aliases: []string{"d"},
EnvVars: nil,
},
&cli.StringSliceFlag{
Name: "ignore-suffixes",
Usage: "files suffixes to ignore",
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",
Expand Down Expand Up @@ -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{
Expand All @@ -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"),
Expand Down
30 changes: 24 additions & 6 deletions pkg/watcher/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand All @@ -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
Expand Down Expand Up @@ -169,6 +186,7 @@ func (f *fsnWatcher) Close() error {

type WatcherArgs struct {
Logger *slog.Logger
OnlyWatchSuffixes []string
IgnoreSuffixes []string
ExcludeDirs []string
UseDefaultIgnoreList bool
Expand All @@ -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
}

0 comments on commit ebff05b

Please sign in to comment.