Add autodetect event flag (#486)

* Add autodetect event flag

* Add new flag to README.md

* Make help more clear
This commit is contained in:
Cat™ 2021-01-18 19:42:55 +00:00 committed by GitHub
parent 7f6a808262
commit 9bbf35e88e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 15 additions and 8 deletions

View file

@ -52,7 +52,8 @@ or in a shell by running
``` ```
# Command structure: # 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: # List the actions for the default event:
act -l act -l
@ -85,6 +86,7 @@ act -v
-C, --directory string working directory (default ".") -C, --directory string working directory (default ".")
-n, --dryrun dryrun mode -n, --dryrun dryrun mode
--env-file string environment file to read and use as env in the containers (default ".env") --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 -e, --eventpath string path to event JSON file
-g, --graph draw workflows -g, --graph draw workflows
-h, --help help for act -h, --help help for act

View file

@ -10,6 +10,7 @@ type Input struct {
actor string actor string
workdir string workdir string
workflowsPath string workflowsPath string
autodetectEvent bool
eventPath string eventPath string
reuseContainers bool reuseContainers bool
bindWorkdir bool bindWorkdir bool

View file

@ -24,7 +24,7 @@ import (
func Execute(ctx context.Context, version string) { func Execute(ctx context.Context, version string) {
input := new(Input) input := new(Input)
var rootCmd = &cobra.Command{ 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.", Short: "Run Github actions locally by specifying the event name (e.g. `push`) or an action name directly.",
Args: cobra.MaximumNArgs(1), Args: cobra.MaximumNArgs(1),
RunE: newRunCommand(ctx, input), 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.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.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.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().StringVarP(&input.eventPath, "eventpath", "e", "", "path to event JSON file")
rootCmd.Flags().StringVar(&input.defaultBranch, "defaultbranch", "", "the name of the main branch") rootCmd.Flags().StringVar(&input.defaultBranch, "defaultbranch", "", "the name of the main branch")
rootCmd.Flags().BoolVar(&input.privileged, "privileged", false, "use privileged mode") 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 // Determine the event name
var eventName string var eventName string
if len(args) > 0 { events := planner.GetEvents()
eventName = args[0] if input.autodetectEvent && len(events) > 0 {
} else if plan := planner.PlanEvent("push"); plan != nil { // set default event type to first event
eventName = "push"
} else if events := planner.GetEvents(); len(events) > 0 {
// set default event type to first event
// this way user dont have to specify the event. // this way user dont have to specify the event.
log.Debugf("Using detected workflow event: %s", events[0]) log.Debugf("Using detected workflow event: %s", events[0])
eventName = 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 // build the plan for this run