From 09c9d55695c1510cb444c6036d4adaa518674675 Mon Sep 17 00:00:00 2001 From: Jacob McCollum Date: Sun, 2 Jan 2022 16:38:06 -0500 Subject: [PATCH] Changes from PR Review: - Remove ^task syntax from `defer` - Support task call syntax in defer --- task_test.go | 4 ++-- taskfile/cmd.go | 16 ++++++++++---- taskfile/defer.go | 38 ++++++++++++++++++++++++++++++++++ taskfile/taskfile_test.go | 11 +++++++--- testdata/deferred/Taskfile.yml | 4 ++-- 5 files changed, 62 insertions(+), 11 deletions(-) create mode 100644 taskfile/defer.go diff --git a/task_test.go b/task_test.go index 4d647a0b..b82322ec 100644 --- a/task_test.go +++ b/task_test.go @@ -1062,8 +1062,8 @@ task: [task-2] echo 'failing' && exit 2 failing task: [task-2] echo 'echo ran' echo ran -task: [task-1] echo 'task-1 ran' -task-1 ran +task: [task-1] echo 'task-1 ran successfully' +task-1 ran successfully `) assert.Error(t, e.Run(context.Background(), taskfile.Call{Task: "task-2"})) fmt.Println(buff.String()) diff --git a/taskfile/cmd.go b/taskfile/cmd.go index 8d9d5d20..4da7aadb 100644 --- a/taskfile/cmd.go +++ b/taskfile/cmd.go @@ -39,11 +39,19 @@ func (c *Cmd) UnmarshalYAML(unmarshal func(interface{}) error) error { } if err := unmarshal(&deferredCmd); err == nil && deferredCmd.Defer != "" { c.Defer = true - if strings.HasPrefix(deferredCmd.Defer, "^") { - c.Task = strings.TrimPrefix(deferredCmd.Defer, "^") - } else { - c.Cmd = deferredCmd.Defer + c.Cmd = deferredCmd.Defer + return nil + } + var deferredCall struct { + Defer struct { + Task string + Vars *Vars } + } + if err := unmarshal(&deferredCall); err == nil && deferredCall.Defer.Task != "" { + c.Defer = true + c.Task = deferredCall.Defer.Task + c.Vars = deferredCall.Defer.Vars return nil } var taskCall struct { diff --git a/taskfile/defer.go b/taskfile/defer.go new file mode 100644 index 00000000..5787fac2 --- /dev/null +++ b/taskfile/defer.go @@ -0,0 +1,38 @@ +package taskfile + +// Defer is the parameters to a defer operation. +// It can be exactly one of: +// - A string command +// - A task call +type Defer struct { + Cmd string + Call *Call +} + +// isValid returns true when Defer describes a valid action. +// In order for a Defer to be valid, one of Cmd or Call.Task +// must be non-empty. +func (d *Defer) isValid() bool { + return d.Cmd != "" || (d.Call != nil && d.Call.Task != "") +} + +// UnmarshalYAML implements yaml.Unmarshaler interface +func (d *Defer) UnmarshalYAML(unmarshal func(interface{}) error) error { + var cmd string + if err := unmarshal(&cmd); err == nil { + d.Cmd = cmd + return nil + } + var taskCall struct { + Task string + Vars *Vars + } + if err := unmarshal(&taskCall); err != nil { + return err + } + d.Call = &Call{ + Task: taskCall.Task, + Vars: taskCall.Vars, + } + return nil +} diff --git a/taskfile/taskfile_test.go b/taskfile/taskfile_test.go index c7adf5c6..bb29d350 100644 --- a/taskfile/taskfile_test.go +++ b/taskfile/taskfile_test.go @@ -19,7 +19,7 @@ vars: PARAM1: VALUE1 PARAM2: VALUE2 ` - yamlDeferredTask = `defer: ^some_task` + yamlDeferredCall = `defer: { task: some_task, vars: { PARAM1: "var" } }` yamlDeferredCmd = `defer: echo 'test'` ) tests := []struct { @@ -49,9 +49,14 @@ vars: &taskfile.Cmd{Cmd: "echo 'test'", Defer: true}, }, { - yamlDeferredTask, + yamlDeferredCall, &taskfile.Cmd{}, - &taskfile.Cmd{Task: "some_task", Defer: true}, + &taskfile.Cmd{Task: "some_task", Vars: &taskfile.Vars{ + Keys: []string{"PARAM1"}, + Mapping: map[string]taskfile.Var{ + "PARAM1": taskfile.Var{Static: "var"}, + }, + }, Defer: true}, }, { yamlDep, diff --git a/testdata/deferred/Taskfile.yml b/testdata/deferred/Taskfile.yml index 1bd4295e..50392883 100644 --- a/testdata/deferred/Taskfile.yml +++ b/testdata/deferred/Taskfile.yml @@ -2,10 +2,10 @@ version: "3" tasks: task-1: - - echo 'task-1 ran' + - echo 'task-1 ran {{.PARAM}}' task-2: - - defer: "^task-1" + - defer: { task: "task-1", vars: { PARAM: "successfully" } } - defer: echo 'echo ran' - defer: echo 'failing' && exit 2 - echo 'cmd ran'