Skip to content

Commit

Permalink
fix(deferred): add EXIT_CODE to deferred tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
NikitaCOEUR committed Dec 21, 2024
1 parent c5be676 commit e4a8600
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 8 deletions.
16 changes: 16 additions & 0 deletions task.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,22 @@ func (e *Executor) runDeferred(t *ast.Task, call *ast.Call, i int, deferredExitC

cmd.Cmd = templater.ReplaceWithExtra(cmd.Cmd, cache, extra)

if cmd.Vars != nil && cmd.Vars.Len() > 0 {
for _, key := range cmd.Vars.Keys() {
if v := cmd.Vars.Get(key); v.Value != "" || v.Sh != nil {
// Handle Value field
if str, isStr := v.Value.(string); isStr {
v.Value = templater.ReplaceWithExtra(str, cache, extra)
}
// Handle Sh field if it exists
if v.Sh != nil {
*v.Sh = templater.ReplaceWithExtra(*v.Sh, cache, extra)
}
cmd.Vars.Set(key, v)
}
}
}

if err := e.runCommand(ctx, t, call, i); err != nil {
e.Logger.VerboseErrf(logger.Yellow, "task: ignored error in deferred cmd: %s\n", err.Error())
}
Expand Down
4 changes: 4 additions & 0 deletions task_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2238,6 +2238,10 @@ task: [task-2] exit 1
task: [task-2] echo 'failing' && exit 2
failing
echo ran
task: [task-1] echo 'task-1 ran Variable with SH - EXIT_CODE : 1'
task-1 ran Variable with SH - EXIT_CODE : 1
task: [task-1] echo 'task-1 ran EXIT_CODE : 1'
task-1 ran EXIT_CODE : 1
task-1 ran successfully
task: [task-1] echo 'task-1 ran successfully'
task-1 ran successfully
Expand Down
27 changes: 19 additions & 8 deletions testdata/deferred/Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,24 @@ version: '3'

tasks:
task-1:
- echo 'task-1 ran {{.PARAM}}'
cmds:
- echo 'task-1 ran {{.PARAM}}'

task-2:
- defer: { task: 'task-1', vars: { PARAM: 'successfully' } }
- defer: { task: 'task-1', vars: { PARAM: 'successfully' }, silent: true }
- defer: echo 'echo ran'
silent: true
- defer: echo 'failing' && exit 2
- echo 'cmd ran'
- exit 1
cmds:
- defer: { task: 'task-1', vars: { PARAM: 'successfully' } }
- defer: { task: 'task-1', vars: { PARAM: 'successfully' }, silent: true }
- defer:
task: 'task-1'
vars:
PARAM: "{{if .EXIT_CODE}}EXIT_CODE : {{.EXIT_CODE}}{{else}}Success{{end}}"
- defer:
task: 'task-1'
vars:
PARAM:
sh: "{{if .EXIT_CODE}}echo Variable with SH - EXIT_CODE : {{.EXIT_CODE}}{{else}}echo Variable with SH : Success{{end}}"
- defer: echo 'echo ran'
silent: true
- defer: echo 'failing' && exit 2
- echo 'cmd ran'
- exit 1

0 comments on commit e4a8600

Please sign in to comment.