Skip to content

Commit 09c9d55

Browse files
committed
Changes from PR Review:
- Remove ^task syntax from `defer` - Support task call syntax in defer
1 parent 69e9eff commit 09c9d55

File tree

5 files changed

+62
-11
lines changed

5 files changed

+62
-11
lines changed

task_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1062,8 +1062,8 @@ task: [task-2] echo 'failing' && exit 2
10621062
failing
10631063
task: [task-2] echo 'echo ran'
10641064
echo ran
1065-
task: [task-1] echo 'task-1 ran'
1066-
task-1 ran
1065+
task: [task-1] echo 'task-1 ran successfully'
1066+
task-1 ran successfully
10671067
`)
10681068
assert.Error(t, e.Run(context.Background(), taskfile.Call{Task: "task-2"}))
10691069
fmt.Println(buff.String())

taskfile/cmd.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,19 @@ func (c *Cmd) UnmarshalYAML(unmarshal func(interface{}) error) error {
3939
}
4040
if err := unmarshal(&deferredCmd); err == nil && deferredCmd.Defer != "" {
4141
c.Defer = true
42-
if strings.HasPrefix(deferredCmd.Defer, "^") {
43-
c.Task = strings.TrimPrefix(deferredCmd.Defer, "^")
44-
} else {
45-
c.Cmd = deferredCmd.Defer
42+
c.Cmd = deferredCmd.Defer
43+
return nil
44+
}
45+
var deferredCall struct {
46+
Defer struct {
47+
Task string
48+
Vars *Vars
4649
}
50+
}
51+
if err := unmarshal(&deferredCall); err == nil && deferredCall.Defer.Task != "" {
52+
c.Defer = true
53+
c.Task = deferredCall.Defer.Task
54+
c.Vars = deferredCall.Defer.Vars
4755
return nil
4856
}
4957
var taskCall struct {

taskfile/defer.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package taskfile
2+
3+
// Defer is the parameters to a defer operation.
4+
// It can be exactly one of:
5+
// - A string command
6+
// - A task call
7+
type Defer struct {
8+
Cmd string
9+
Call *Call
10+
}
11+
12+
// isValid returns true when Defer describes a valid action.
13+
// In order for a Defer to be valid, one of Cmd or Call.Task
14+
// must be non-empty.
15+
func (d *Defer) isValid() bool {
16+
return d.Cmd != "" || (d.Call != nil && d.Call.Task != "")
17+
}
18+
19+
// UnmarshalYAML implements yaml.Unmarshaler interface
20+
func (d *Defer) UnmarshalYAML(unmarshal func(interface{}) error) error {
21+
var cmd string
22+
if err := unmarshal(&cmd); err == nil {
23+
d.Cmd = cmd
24+
return nil
25+
}
26+
var taskCall struct {
27+
Task string
28+
Vars *Vars
29+
}
30+
if err := unmarshal(&taskCall); err != nil {
31+
return err
32+
}
33+
d.Call = &Call{
34+
Task: taskCall.Task,
35+
Vars: taskCall.Vars,
36+
}
37+
return nil
38+
}

taskfile/taskfile_test.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ vars:
1919
PARAM1: VALUE1
2020
PARAM2: VALUE2
2121
`
22-
yamlDeferredTask = `defer: ^some_task`
22+
yamlDeferredCall = `defer: { task: some_task, vars: { PARAM1: "var" } }`
2323
yamlDeferredCmd = `defer: echo 'test'`
2424
)
2525
tests := []struct {
@@ -49,9 +49,14 @@ vars:
4949
&taskfile.Cmd{Cmd: "echo 'test'", Defer: true},
5050
},
5151
{
52-
yamlDeferredTask,
52+
yamlDeferredCall,
5353
&taskfile.Cmd{},
54-
&taskfile.Cmd{Task: "some_task", Defer: true},
54+
&taskfile.Cmd{Task: "some_task", Vars: &taskfile.Vars{
55+
Keys: []string{"PARAM1"},
56+
Mapping: map[string]taskfile.Var{
57+
"PARAM1": taskfile.Var{Static: "var"},
58+
},
59+
}, Defer: true},
5560
},
5661
{
5762
yamlDep,

testdata/deferred/Taskfile.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ version: "3"
22

33
tasks:
44
task-1:
5-
- echo 'task-1 ran'
5+
- echo 'task-1 ran {{.PARAM}}'
66

77
task-2:
8-
- defer: "^task-1"
8+
- defer: { task: "task-1", vars: { PARAM: "successfully" } }
99
- defer: echo 'echo ran'
1010
- defer: echo 'failing' && exit 2
1111
- echo 'cmd ran'

0 commit comments

Comments
 (0)