From 9bbf35e88ef6a26f19bd53e29b15b67fe34d135b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Cat=E2=84=A2?= <me@hackerc.at>
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
---
 README.md    |  4 +++-
 cmd/input.go |  1 +
 cmd/root.go  | 18 +++++++++++-------
 3 files changed, 15 insertions(+), 8 deletions(-)

diff --git a/README.md b/README.md
index 0d72e7b..2b995a8 100644
--- a/README.md
+++ b/README.md
@@ -52,7 +52,8 @@ or in a shell by running
 
 ```
 # Command structure:
-act [event name to run] [flags]
+act [<event>] [options]
+If no event name passed, will default to "on: push"
 
 # List the actions for the default event:
 act -l
@@ -85,6 +86,7 @@ act -v
   -C, --directory string       working directory (default ".")
   -n, --dryrun                 dryrun mode
       --env-file string        environment file to read and use as env in the containers (default ".env")
+      --detect-event           Use first event type from workflow as event that triggered the workflow
   -e, --eventpath string       path to event JSON file
   -g, --graph                  draw workflows
   -h, --help                   help for act
diff --git a/cmd/input.go b/cmd/input.go
index 808be0e..5a61d2b 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 dc28317..afd3fbb 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