Skip to content

Commit

Permalink
Merge pull request #30 from urcomputeringpal/watch
Browse files Browse the repository at this point in the history
--watch
  • Loading branch information
cplee authored Feb 13, 2019
2 parents af08b30 + 4228018 commit e27d5fa
Show file tree
Hide file tree
Showing 19 changed files with 1,076 additions and 18 deletions.
97 changes: 79 additions & 18 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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")
Expand Down Expand Up @@ -52,34 +56,91 @@ 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)
}
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)
}

// run the event in the RunnerRonfig
return runner.RunEvent()
// 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 {
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ require (
github.com/Microsoft/go-winio v0.4.11 // indirect
github.com/Nvveen/Gotty v0.0.0-20120604004816-cd527374f1e5 // indirect
github.com/actions/workflow-parser v1.0.0
github.com/andreaskoch/go-fswatch v1.0.0
github.com/containerd/continuity v0.0.0-20181203112020-004b46473808 // indirect
github.com/docker/distribution v2.7.1+incompatible // indirect
github.com/docker/docker v1.13.1
Expand All @@ -24,6 +25,7 @@ require (
github.com/opencontainers/image-spec v1.0.1 // indirect
github.com/opencontainers/runc v0.1.1 // indirect
github.com/pkg/errors v0.8.1 // indirect
github.com/sabhiram/go-gitignore v0.0.0-20180611051255-d3107576ba94
github.com/sirupsen/logrus v1.3.0
github.com/smartystreets/assertions v0.0.0-20190116191733-b6c0e53d7304 // indirect
github.com/smartystreets/goconvey v0.0.0-20181108003508-044398e4856c // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ github.com/actions/workflow-parser v1.0.0 h1:Zz2Ke31f3OMYCSzU2pqZSsk/Oz+lWXfEiXM
github.com/actions/workflow-parser v1.0.0/go.mod h1:jz9ZVl8zUIcjMfDQearQjvUHIBhx9l1ys4keDd6be34=
github.com/alcortesm/tgz v0.0.0-20161220082320-9c5fe88206d7 h1:uSoVVbwJiQipAclBbw+8quDsfcvFjOpI5iCf4p/cqCs=
github.com/alcortesm/tgz v0.0.0-20161220082320-9c5fe88206d7/go.mod h1:6zEj6s6u/ghQa61ZWa/C2Aw3RkjiTBOix7dkqa1VLIs=
github.com/andreaskoch/go-fswatch v1.0.0 h1:la8nP/HiaFCxP2IM6NZNUCoxgLWuyNFgH0RligBbnJU=
github.com/andreaskoch/go-fswatch v1.0.0/go.mod h1:r5/iV+4jfwoY2sYqBkg8vpF04ehOvEl4qPptVGdxmqo=
github.com/anmitsu/go-shlex v0.0.0-20161002113705-648efa622239 h1:kFOfPq6dUM1hTo4JG6LR5AXSUEsOjtdm0kw0FtQtMJA=
github.com/anmitsu/go-shlex v0.0.0-20161002113705-648efa622239/go.mod h1:2FmKhYUyUczH0OGQWaF5ceTx0UBShxjsH6f8oGKYe2c=
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
Expand Down Expand Up @@ -91,6 +93,8 @@ github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sabhiram/go-gitignore v0.0.0-20180611051255-d3107576ba94 h1:G04eS0JkAIVZfaJLjla9dNxkJCPiKIGZlw9AfOhzOD0=
github.com/sabhiram/go-gitignore v0.0.0-20180611051255-d3107576ba94/go.mod h1:b18R55ulyQ/h3RaWyloPyER7fWQVZvimKKhnI5OfrJQ=
github.com/sergi/go-diff v1.0.0 h1:Kpca3qRNrduNnOQeazBd0ysaKrUJiIuISHxogkT9RPQ=
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
github.com/sirupsen/logrus v1.3.0 h1:hI/7Q+DtNZ2kINb6qt/lS+IyXnHQe9e90POfeewL/ME=
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit e27d5fa

Please sign in to comment.