From 32d2836743b22f94ced68b9d0a07647fe981fb3d Mon Sep 17 00:00:00 2001 From: Jason Song Date: Wed, 19 Apr 2023 10:36:32 +0800 Subject: [PATCH 1/3] fix: improve watchAndRun --- cmd/root.go | 51 ++++++++++++++++++++++++++------------------------- 1 file changed, 26 insertions(+), 25 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index 02afb50b343..b6736271b94 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -18,13 +18,13 @@ import ( gitignore "github.com/sabhiram/go-gitignore" log "github.com/sirupsen/logrus" "github.com/spf13/cobra" + "gopkg.in/yaml.v3" "github.com/nektos/act/pkg/artifacts" "github.com/nektos/act/pkg/common" "github.com/nektos/act/pkg/container" "github.com/nektos/act/pkg/model" "github.com/nektos/act/pkg/runner" - "gopkg.in/yaml.v3" ) // Execute is the entry point to running the CLI @@ -623,45 +623,46 @@ func defaultImageSurvey(actrc string) error { } func watchAndRun(ctx context.Context, fn common.Executor) error { - recurse := true - checkIntervalInSeconds := 2 dir, err := os.Getwd() if err != nil { return err } - var ignore *gitignore.GitIgnore - if _, err := os.Stat(filepath.Join(dir, ".gitignore")); !os.IsNotExist(err) { - ignore, _ = gitignore.CompileIgnoreFile(filepath.Join(dir, ".gitignore")) - } else { - ignore = &gitignore.GitIgnore{} + ignore := &gitignore.GitIgnore{} + if info, err := os.Stat(filepath.Join(dir, ".gitignore")); err == nil && !info.IsDir() { + ignore, err = gitignore.CompileIgnoreFile(filepath.Join(dir, ".gitignore")) + if err != nil { + return fmt.Errorf("compile .gitignore: %w", err) + } } folderWatcher := fswatch.NewFolderWatcher( dir, - recurse, + true, ignore.MatchesPath, - checkIntervalInSeconds, + 2, // 2 seconds ) folderWatcher.Start() + defer folderWatcher.Stop() - go func() { - for folderWatcher.IsRunning() { + // run once before watching + if err := fn(ctx); err != nil { + return err + } + + for folderWatcher.IsRunning() { + log.Debugf("Watching %s for changes", dir) + select { + case <-ctx.Done(): + return nil + case changes := <-folderWatcher.ChangeDetails(): + log.Debugf("%s", changes.String()) if err = fn(ctx); err != nil { - break - } - log.Debugf("Watching %s for changes", dir) - for changes := range folderWatcher.ChangeDetails() { - log.Debugf("%s", changes.String()) - if err = fn(ctx); err != nil { - break - } - log.Debugf("Watching %s for changes", dir) + return err } } - }() - <-ctx.Done() - folderWatcher.Stop() - return err + } + + return nil } From 1e2746764ba8f092933d9ef293d2c9cf7a8fca6a Mon Sep 17 00:00:00 2001 From: Jason Song Date: Wed, 19 Apr 2023 10:40:42 +0800 Subject: [PATCH 2/3] fix: lint --- cmd/root.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index b6736271b94..e500da80b2b 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -628,11 +628,12 @@ func watchAndRun(ctx context.Context, fn common.Executor) error { return err } + ignoreFile := filepath.Join(dir, ".gitignore") ignore := &gitignore.GitIgnore{} - if info, err := os.Stat(filepath.Join(dir, ".gitignore")); err == nil && !info.IsDir() { - ignore, err = gitignore.CompileIgnoreFile(filepath.Join(dir, ".gitignore")) + if info, err := os.Stat(ignoreFile); err == nil && !info.IsDir() { + ignore, err = gitignore.CompileIgnoreFile(ignoreFile) if err != nil { - return fmt.Errorf("compile .gitignore: %w", err) + return fmt.Errorf("compile %q: %w", ignoreFile, err) } } From e5976174b4cd39aa1cdeaaf3cf65f0c5de27daaa Mon Sep 17 00:00:00 2001 From: Jason Song Date: Wed, 19 Apr 2023 10:43:06 +0800 Subject: [PATCH 3/3] fix: lint --- cmd/root.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/root.go b/cmd/root.go index e500da80b2b..e217bcfeb57 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -659,7 +659,7 @@ func watchAndRun(ctx context.Context, fn common.Executor) error { return nil case changes := <-folderWatcher.ChangeDetails(): log.Debugf("%s", changes.String()) - if err = fn(ctx); err != nil { + if err := fn(ctx); err != nil { return err } }