How to reuse code in taskfile?
I have a lot of commands which all begin with the same code, and I'd like some clever way to reuse them. I found out I can do it with
vars
, but the problem is that I would like to keep the calls short and sweet, and using var
changes the height of each method from 1 line to 4 😮 Is there some other, more concise method?4 Replies
Hi @Dnaron,
Do you an example to explain how you're trying to do it?
Sure!
Tasklist is great! It's so much better than
make
. I especially like the listing of all possible lists, the silent mode, the deps
, the dir
, and the shorthand for cmd
, all of that is awesome.
Currently, I have a file like this:
You can see that most of them all begin with docker compose exec -T -u nginx php
. I wish there was some clean way to extract the reused parts.
Now, I know I can extract those commands using internal command and vars, and I did that but look how verbose it becomes! It's soo long:
I also tried extracting the command as var, like this:
but then I get an error:
Another idea might be to call the task shell from within the task:
But now in-docker
is not internal (i would like to keep it internal) and it feels a litle too hacky :/Some thoughts:
1. You could try a shorter syntax for calling a different task if you want:
2. Regarding this syntax error: it's just a matter of quoting the command:
bash: '{{.TASK_EXE}} in-docker -- bash'
3. You might be interested into this proposal: https://github.com/go-task/task/issues/448
GitHub
Add option to customise interpreter that executes commands · Issue ...
This feature, or something similar, has been discussed previously (see #243 and here in the v3 release tracking ticket), however was never implemented. This is a feature I personally would like to ...