Improve watchAndRun (#1743)
* fix: improve watchAndRun * fix: lint * fix: lint --------- Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
This commit is contained in:
parent
35d6e9f71e
commit
3715266494
1 changed files with 28 additions and 26 deletions
56
cmd/root.go
56
cmd/root.go
|
@ -18,13 +18,13 @@ import (
|
||||||
gitignore "github.com/sabhiram/go-gitignore"
|
gitignore "github.com/sabhiram/go-gitignore"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
"gopkg.in/yaml.v3"
|
||||||
|
|
||||||
"github.com/nektos/act/pkg/artifacts"
|
"github.com/nektos/act/pkg/artifacts"
|
||||||
"github.com/nektos/act/pkg/common"
|
"github.com/nektos/act/pkg/common"
|
||||||
"github.com/nektos/act/pkg/container"
|
"github.com/nektos/act/pkg/container"
|
||||||
"github.com/nektos/act/pkg/model"
|
"github.com/nektos/act/pkg/model"
|
||||||
"github.com/nektos/act/pkg/runner"
|
"github.com/nektos/act/pkg/runner"
|
||||||
"gopkg.in/yaml.v3"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Execute is the entry point to running the CLI
|
// Execute is the entry point to running the CLI
|
||||||
|
@ -623,45 +623,47 @@ func defaultImageSurvey(actrc string) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func watchAndRun(ctx context.Context, fn common.Executor) error {
|
func watchAndRun(ctx context.Context, fn common.Executor) error {
|
||||||
recurse := true
|
|
||||||
checkIntervalInSeconds := 2
|
|
||||||
dir, err := os.Getwd()
|
dir, err := os.Getwd()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
var ignore *gitignore.GitIgnore
|
ignoreFile := filepath.Join(dir, ".gitignore")
|
||||||
if _, err := os.Stat(filepath.Join(dir, ".gitignore")); !os.IsNotExist(err) {
|
ignore := &gitignore.GitIgnore{}
|
||||||
ignore, _ = gitignore.CompileIgnoreFile(filepath.Join(dir, ".gitignore"))
|
if info, err := os.Stat(ignoreFile); err == nil && !info.IsDir() {
|
||||||
} else {
|
ignore, err = gitignore.CompileIgnoreFile(ignoreFile)
|
||||||
ignore = &gitignore.GitIgnore{}
|
if err != nil {
|
||||||
|
return fmt.Errorf("compile %q: %w", ignoreFile, err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
folderWatcher := fswatch.NewFolderWatcher(
|
folderWatcher := fswatch.NewFolderWatcher(
|
||||||
dir,
|
dir,
|
||||||
recurse,
|
true,
|
||||||
ignore.MatchesPath,
|
ignore.MatchesPath,
|
||||||
checkIntervalInSeconds,
|
2, // 2 seconds
|
||||||
)
|
)
|
||||||
|
|
||||||
folderWatcher.Start()
|
folderWatcher.Start()
|
||||||
|
defer folderWatcher.Stop()
|
||||||
|
|
||||||
go func() {
|
// run once before watching
|
||||||
for folderWatcher.IsRunning() {
|
if err := fn(ctx); err != nil {
|
||||||
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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
<-ctx.Done()
|
|
||||||
folderWatcher.Stop()
|
|
||||||
return err
|
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 {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue