Open tommyalatalo opened 4 years ago
Hi @tommyalatalo,
This seems like an useful feature request, but shouldn't be my priority for the near future, unless someone else decides to contribute with this.
Since #405 is a duplication, let me paste my workaround [1, 2] here too:
There is already a way to reuse preconditions by the use of YAML anchors:
tasks:
task1:
cmds:
- "echo task1"
preconditions:
- &test-precondition
sh: "[ 1 = 0 ]"
msg: "One doesn't equal Zero, Halting"
task2:
cmds:
- "echo task2"
preconditions:
- *test-precondition
task3:
cmds:
- "echo task3"
preconditions:
- *test-precondition
However, the anchor have to be on the same YAML file. Adding global preconditions would only reduce duplications, if they are used on multiple imported taskfiles.
Since we do not use strict YAML unmarshal, even this is possible:
preconditions:
<<: &test-precondition
sh: "[ 1 = 0 ]"
msg: "One doesn't equal Zero, Halting"
tasks:
task1:
cmds:
- "echo task1"
preconditions:
- *test-precondition
task2:
cmds:
- "echo task2"
preconditions:
- *test-precondition
task3:
cmds:
- "echo task3"
preconditions:
- *test-precondition
I'm aware of yaml anchors, but as you said yourself, it doesn't work across multiple taskfiles, and it requires a lot of duplication which is error prone.
I'm aware of yaml anchors, but as you said yourself, it doesn't work across multiple taskfiles, and it requires a lot of duplication which is error prone.
sure, I understand. I prefer a proper solution for this issue, too. This is just a comment for anyone who is not aware of yaml anchors and are looking for a workaround solution.
I prefer to initially advocate for seeking a solution that doesn't rely on anchors. If that's not possible, having a key to store anchors would be helpful to prevent syntax highlighting errors.
I just came across a use-case that would have made my taskfiles so much more DRY, do not let the taskfile be run as root.
preconditions:
- msg: must not be run as root
sh: '[[ $LOGNAME != "root" ]]'
The anchor solution works in basic use cases but since you cannot use them across files it makes that solution less than ideal for me.
It would be great to be able to set global preconditions to prevent running any tasks in a Taskfile and its children if the precondition is not met. This would be a simple way to add a precondition to all tasks without having to add the same precondition to every task individually.
The use case for this is for example to enforce that a repository has been initialized somehow before allowing to run any other tasks, like setting up release targets for goreleaser or similar. In this example I would have the .goreleaser.yml contain a string like "INIT_PACKAGE" which should be replaced by the package name of the repository, and until that's done all tasks should fail (precondition greps for "INIT_PACKAGE"), and return the same message "This repository hasn't been initialized, please run 'task init'".
Obviously to be able to run the initial
task init
there needs to be a property to disable the global precondition for that specific task. So the Taskfile would end up something like this:Also the logic should probably be that any preconditions from the parent Taskfile which includes other taskfiles should be applied on all the child taskfiles' tasks as well, i.e. they inherit the parent's global preconditions. The preconditions should only be propagated downwards in the hierarchy meaning that the parent Taskfile doesn't inherit global preconditions from it's children. Or maybe there should be a property to select this behavior as well, "include_preconditions: yes/no"?