task
task copied to clipboard
why is the following not working?
It seems
USER_NAME=abc task ctx:greet_user works on linux
but
task ctx:greet_user USER_NAME=abc does not work on linux.
version: '3'
tasks:
greet_user:
desc: "Greet the user with a name."
vars:
USER_NAME:
sh: 'echo "${USER_NAME:-DefaultUser}"'
cmds:
- echo "Hello, {{.USER_NAME}}!"
I use it like this and it works:
task deploy PROJECT=caddy
version: '3'
vars:
ENV: prod
PROJECT: # Task parameter/arguments are not yet available in root interpolation/templating, Taskfile also does not support top level require
sh: '[ -z "${PROJECT+x}" ] && { echo "Error: PROJECT is not defined" >&2; exit 1; } || echo "$PROJECT"'
It is possible that it is not available as environment variable in tasks, but only at top level.
I am doing the above, because command defined VARS/arguments (this is what you define after task command, before it, you define an ENV in shell and pass it to task) are not immediately available in top level VARS.
Hello @bayeslearner If you want to have a default value for a variable, the recommanded way is to use the go templating :
greet_user:
desc: "Greet the user with a name."
vars:
USER_NAME: '{{.USER_NAME| default "DefaultUser"}}'
cmds:
- echo "Hello, {{.USER_NAME}}!"
Both USER_NAME=abc task ctx:greet_user and task ctx:greet_user USER_NAME=abc works