From 0f5e9b0a60bad51abb80bf481e41dbbcdeae31d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cat=E2=84=A2?= Date: Mon, 18 Jan 2021 19:42:55 +0000 Subject: [PATCH] Add autodetect event flag (#486) * Add autodetect event flag * Add new flag to README.md * Make help more clear --- cmd/input.go | 1 + cmd/root.go | 18 +++++++++++------- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/cmd/input.go b/cmd/input.go index 808be0e1..5a61d2b5 100644 --- a/cmd/input.go +++ b/cmd/input.go @@ -10,6 +10,7 @@ type Input struct { actor string workdir string workflowsPath string + autodetectEvent bool eventPath string reuseContainers bool bindWorkdir bool diff --git a/cmd/root.go b/cmd/root.go index dc28317d..afd3fbba 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -24,7 +24,7 @@ import ( func Execute(ctx context.Context, version string) { input := new(Input) var rootCmd = &cobra.Command{ - Use: "act [event name to run]", + Use: "act [event name to run]\nIf no event name passed, will default to \"on: push\"", Short: "Run Github actions locally by specifying the event name (e.g. `push`) or an action name directly.", Args: cobra.MaximumNArgs(1), RunE: newRunCommand(ctx, input), @@ -42,6 +42,7 @@ func Execute(ctx context.Context, version string) { rootCmd.Flags().BoolVarP(&input.reuseContainers, "reuse", "r", false, "reuse action containers to maintain state") rootCmd.Flags().BoolVarP(&input.bindWorkdir, "bind", "b", false, "bind working directory to container, rather than copy") rootCmd.Flags().BoolVarP(&input.forcePull, "pull", "p", false, "pull docker image(s) if already present") + rootCmd.Flags().BoolVarP(&input.autodetectEvent, "detect-event", "", false, "Use first event type from workflow as event that triggered the workflow") rootCmd.Flags().StringVarP(&input.eventPath, "eventpath", "e", "", "path to event JSON file") rootCmd.Flags().StringVar(&input.defaultBranch, "defaultbranch", "", "the name of the main branch") rootCmd.Flags().BoolVar(&input.privileged, "privileged", false, "use privileged mode") @@ -145,15 +146,18 @@ func newRunCommand(ctx context.Context, input *Input) func(*cobra.Command, []str // Determine the event name var eventName string - if len(args) > 0 { - eventName = args[0] - } else if plan := planner.PlanEvent("push"); plan != nil { - eventName = "push" - } else if events := planner.GetEvents(); len(events) > 0 { - // set default event type to first event + events := planner.GetEvents() + if input.autodetectEvent && len(events) > 0 { + // set default event type to first event // this way user dont have to specify the event. log.Debugf("Using detected workflow event: %s", events[0]) eventName = events[0] + } else { + if len(args) > 0 { + eventName = args[0] + } else if plan := planner.PlanEvent("push"); plan != nil { + eventName = "push" + } } // build the plan for this run