act/cmd/root.go

194 lines
5.2 KiB
Go
Raw Normal View History

package cmd
import (
"context"
"fmt"
"os"
2019-02-09 20:39:09 -06:00
"path/filepath"
2019-02-09 20:39:09 -06:00
fswatch "github.com/andreaskoch/go-fswatch"
"github.com/nektos/act/actions"
"github.com/nektos/act/common"
2019-02-09 20:39:09 -06:00
gitignore "github.com/sabhiram/go-gitignore"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
// Execute is the entry point to running the CLI
func Execute(ctx context.Context, version string) {
runnerConfig := &actions.RunnerConfig{Ctx: ctx}
var rootCmd = &cobra.Command{
Use: "act [event name to run]",
Short: "Run Github actions locally by specifying the event name (e.g. `push`) or an action name directly.",
Args: cobra.MaximumNArgs(1),
RunE: newRunCommand(runnerConfig),
PersistentPreRun: setupLogging,
Version: version,
SilenceUsage: true,
}
2019-02-09 20:39:09 -06:00
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")
rootCmd.Flags().StringVarP(&runnerConfig.EventPath, "event", "e", "", "path to event JSON file")
rootCmd.PersistentFlags().BoolP("verbose", "v", false, "verbose output")
rootCmd.PersistentFlags().BoolVarP(&runnerConfig.Dryrun, "dryrun", "n", false, "dryrun mode")
rootCmd.PersistentFlags().StringVarP(&runnerConfig.WorkflowPath, "file", "f", "./.github/main.workflow", "path to workflow file")
rootCmd.PersistentFlags().StringVarP(&runnerConfig.WorkingDir, "directory", "C", ".", "working directory")
if err := rootCmd.Execute(); err != nil {
os.Exit(1)
}
}
func setupLogging(cmd *cobra.Command, args []string) {
verbose, _ := cmd.Flags().GetBool("verbose")
if verbose {
log.SetLevel(log.DebugLevel)
}
}
func newRunCommand(runnerConfig *actions.RunnerConfig) func(*cobra.Command, []string) error {
return func(cmd *cobra.Command, args []string) error {
2019-02-15 10:34:19 -06:00
if len(args) > 0 {
runnerConfig.EventName = args[0]
}
2019-02-09 20:39:09 -06:00
watch, err := cmd.Flags().GetBool("watch")
if err != nil {
return err
}
2019-02-09 20:39:09 -06:00
if watch {
2019-02-12 22:32:54 -06:00
return watchAndRun(runnerConfig.Ctx, func() error {
return parseAndRun(cmd, runnerConfig)
2019-02-09 20:39:09 -06:00
})
}
2019-02-12 22:32:54 -06:00
return parseAndRun(cmd, runnerConfig)
2019-02-09 20:39:09 -06:00
}
}
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()
2019-02-15 10:34:19 -06:00
// set default event type if we only have a single workflow in the file.
// this way user dont have to specify the event.
if runnerConfig.EventName == "" {
if events := runner.ListEvents(); len(events) == 1 {
log.Debugf("Using detected workflow event: %s", events[0])
runnerConfig.EventName = events[0]
}
}
// fall back to default event name if we could not detect one.
if runnerConfig.EventName == "" {
runnerConfig.EventName = "push"
}
2019-02-09 20:39:09 -06:00
// check if we should just print the graph
list, err := cmd.Flags().GetBool("list")
if err != nil {
return err
}
if list {
return drawGraph(runner)
}
2019-02-09 20:39:09 -06:00
// check if we are running just a single action
actionName, err := cmd.Flags().GetString("action")
if err != nil {
return err
}
2019-02-09 20:39:09 -06:00
if actionName != "" {
return runner.RunActions(actionName)
}
// run the event in the RunnerRonfig
return runner.RunEvent()
}
2019-02-12 22:32:54 -06:00
func watchAndRun(ctx context.Context, fn func() error) error {
2019-02-09 20:39:09 -06:00
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()
go func() {
for folderWatcher.IsRunning() {
2019-02-12 22:32:54 -06:00
if err = fn(); err != nil {
break
}
log.Debugf("Watching %s for changes", dir)
2019-02-09 20:39:09 -06:00
for changes := range folderWatcher.ChangeDetails() {
log.Debugf("%s", changes.String())
2019-02-12 22:32:54 -06:00
if err = fn(); err != nil {
break
}
2019-02-09 20:39:09 -06:00
log.Debugf("Watching %s for changes", dir)
}
}
}()
<-ctx.Done()
folderWatcher.Stop()
2019-02-12 22:32:54 -06:00
return err
}
func drawGraph(runner actions.Runner) error {
eventNames := runner.ListEvents()
for _, eventName := range eventNames {
graph, err := runner.GraphEvent(eventName)
if err != nil {
return err
}
drawings := make([]*common.Drawing, 0)
eventPen := common.NewPen(common.StyleDoubleLine, 91 /*34*/)
drawings = append(drawings, eventPen.DrawBoxes(fmt.Sprintf("EVENT: %s", eventName)))
actionPen := common.NewPen(common.StyleSingleLine, 96)
arrowPen := common.NewPen(common.StyleNoLine, 97)
drawings = append(drawings, arrowPen.DrawArrow())
for i, stage := range graph {
if i > 0 {
drawings = append(drawings, arrowPen.DrawArrow())
}
drawings = append(drawings, actionPen.DrawBoxes(stage...))
}
maxWidth := 0
for _, d := range drawings {
if d.GetWidth() > maxWidth {
maxWidth = d.GetWidth()
}
}
for _, d := range drawings {
d.Draw(os.Stdout, maxWidth)
}
}
return nil
}