This commit is contained in:
Jesse Newland
2019-02-09 20:39:09 -06:00
parent de62a2eece
commit 3198627879
19 changed files with 1077 additions and 19 deletions

View File

@@ -4,9 +4,12 @@ import (
"context"
"fmt"
"os"
"path/filepath"
fswatch "github.com/andreaskoch/go-fswatch"
"github.com/nektos/act/actions"
"github.com/nektos/act/common"
gitignore "github.com/sabhiram/go-gitignore"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
@@ -23,6 +26,7 @@ func Execute(ctx context.Context, version string) {
Version: version,
SilenceUsage: true,
}
rootCmd.Flags().BoolP("watch", "w", false, "watch the contents of the local repo and run when files change")
rootCmd.Flags().BoolP("list", "l", false, "list actions")
rootCmd.Flags().StringP("action", "a", "", "run action")
rootCmd.Flags().BoolVarP(&runnerConfig.ReuseContainers, "reuse", "r", false, "reuse action containers to maintain state")
@@ -52,36 +56,93 @@ func newRunCommand(runnerConfig *actions.RunnerConfig) func(*cobra.Command, []st
runnerConfig.EventName = args[0]
}
// create the runner
runner, err := actions.NewRunner(runnerConfig)
err := parseAndRun(cmd, runnerConfig)
if err != nil {
return err
}
defer runner.Close()
// check if we should just print the graph
list, err := cmd.Flags().GetBool("list")
watch, err := cmd.Flags().GetBool("watch")
if err != nil {
return err
}
if list {
return drawGraph(runner)
if watch {
return watchAndRun(runnerConfig.Ctx, func() {
err = parseAndRun(cmd, runnerConfig)
})
}
// check if we are running just a single action
actionName, err := cmd.Flags().GetString("action")
if err != nil {
return err
}
if actionName != "" {
return runner.RunActions(actionName)
}
// run the event in the RunnerRonfig
return runner.RunEvent()
return err
}
}
func parseAndRun(cmd *cobra.Command, runnerConfig *actions.RunnerConfig) error {
// create the runner
runner, err := actions.NewRunner(runnerConfig)
if err != nil {
return err
}
defer runner.Close()
// check if we should just print the graph
list, err := cmd.Flags().GetBool("list")
if err != nil {
return err
}
if list {
return drawGraph(runner)
}
// check if we are running just a single action
actionName, err := cmd.Flags().GetString("action")
if err != nil {
return err
}
if actionName != "" {
return runner.RunActions(actionName)
}
// run the event in the RunnerRonfig
return runner.RunEvent()
}
func watchAndRun(ctx context.Context, fn func()) 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{}
}
folderWatcher := fswatch.NewFolderWatcher(
dir,
recurse,
ignore.MatchesPath,
checkIntervalInSeconds,
)
folderWatcher.Start()
log.Debugf("Watching %s for changes", dir)
go func() {
for folderWatcher.IsRunning() {
for changes := range folderWatcher.ChangeDetails() {
log.Debugf("%s", changes.String())
fn()
log.Debugf("Watching %s for changes", dir)
}
}
}()
<-ctx.Done()
folderWatcher.Stop()
return nil
}
func drawGraph(runner actions.Runner) error {
eventNames := runner.ListEvents()
for _, eventName := range eventNames {