task icon indicating copy to clipboard operation
task copied to clipboard

Allow `defer` to know if the task succeded or failed

Open andreynering opened this issue 1 year ago • 3 comments

Originally asked in #1472 by @your-diary.

A possible way to allow this is to expose one/some variables to defer. One possibility is to expose a DEFER_EXIT_CODE var which would contain 0 for success or the exit code of the task that failed.

Perhaps we could also have DEFER_SUCCESS (true/false) and DEFER_FAIL (true/false) to make a simpler check easier? (Not sure, DEFER_EXIT_CODE is probably enough).

andreynering avatar Jan 26 '24 00:01 andreynering

@andreynering Could you explain a bit deeper how you would the yml to be? I thought about

  task-2:
    - defer_failure: echo Failure!
    - defer_failure: { task: "task-1", vars: { PARAM: "failure" } }
    - defer_success: echo Success!
    - defer_success: { task: "task-1", vars: { PARAM: "success" } }
    - echo 'cmd ran'
    - exit 1

making it very similar to the regular defer, but a bit different from what @your-diray suggested

dorimon-1 avatar Mar 22 '24 13:03 dorimon-1

@dorimon-1 One can ignore my original suggestion. It is just an example.

As for your suggestion, if we add defer_success and defer_failure while keeping defer keyword, we can make the change backward-compatible.

Personally I prefer

A possible way to allow this is to expose one/some variables to defer. One possibility is to expose a DEFER_EXIT_CODE var which would contain 0 for success or the exit code of the task that failed.

in the OP though, because no new syntax is needed:

version: "3"

#This should be automatically set.
env:
    DEFER_EXIT_STATUS: 0

tasks:
    default:
        cmds:
            - defer: |
                  if test ${DEFER_EXIT_STATUS} == 0; then
                      echo success
                  else
                      echo failure
                  fi

your-diary avatar Mar 22 '24 14:03 your-diary

Exactly what I have been searching for.

I think @dorimon-1 idea looks neat and allows for backward compatibility with regular defer. Also having env var for defer status code could be helpful for some ppl. If someone could implement this it would be much appreciated 😉.

KJone1 avatar Mar 23 '24 15:03 KJone1

This was just implemented on #1762.

andreynering avatar Aug 15 '24 01:08 andreynering