1
0
mirror of https://github.com/go-task/task.git synced 2025-01-06 03:53:54 +02:00

fix(deferred): add EXIT_CODE to deferred tasks

This commit is contained in:
Nikita COEUR 2024-12-21 13:36:01 +01:00
parent c5be676555
commit e4a8600fa9
No known key found for this signature in database
3 changed files with 39 additions and 8 deletions

16
task.go
View File

@ -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())
}

View File

@ -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

View File

@ -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