Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

avoid stopping watcher, just add/remove the diff #2

Merged
merged 1 commit into from
May 3, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 29 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/draganm/gorefresh/depdirs"
"github.com/draganm/gosha/gosha"
"github.com/fsnotify/fsnotify"
"github.com/samber/lo"
"github.com/urfave/cli/v2"
"golang.org/x/sync/errgroup"
)
Expand Down Expand Up @@ -59,17 +60,42 @@ func main() {

shaChan <- lastSha

watchedDirs := []string{}

w, err := fsnotify.NewBufferedWatcher(50)
if err != nil {
return fmt.Errorf("failed to create watcher: %w", err)
}

defer w.Close()

for ctx.Err() == nil {
depDirs, err := depdirs.DependencyDirs(moduleDir)
if err != nil {
return fmt.Errorf("failed to get dependency directories: %w", err)
}

w, err := fsnotify.NewBufferedWatcher(50)
if err != nil {
return fmt.Errorf("failed to create watcher: %w", err)
toAdd := lo.Without(depDirs, watchedDirs...)
toRemove := lo.Without(watchedDirs, depDirs...)

for _, d := range toRemove {
err = w.Remove(d)
if err != nil {
return fmt.Errorf("failed to remove %s from watcher: %w", d, err)
}
}

for _, d := range toAdd {
err = w.Add(d)
if err != nil {
return fmt.Errorf("failed to add %s to watcher: %w", d, err)
}
}

watchedDirs = depDirs

fmt.Println("updated watched dirs:", "watching", len(watchedDirs), "added", len(toAdd), "removed", len(toRemove))

for _, dd := range depDirs {
err = w.Add(dd)
if err != nil {
Expand All @@ -92,11 +118,6 @@ func main() {
continue
}

err = w.Close()
if err != nil {
return fmt.Errorf("failed to close watcher: %w", err)
}

lastSha = sha
shaChan <- sha

Expand Down