Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

why is the following not working? #1492

Closed
bayeslearner opened this issue Feb 3, 2024 · 2 comments
Closed

why is the following not working? #1492

bayeslearner opened this issue Feb 3, 2024 · 2 comments
Labels
type: question Further information is requested.

Comments

@bayeslearner
Copy link

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}}!"
@task-bot task-bot added the state: needs triage Waiting to be triaged by a maintainer. label Feb 3, 2024
@mihakrumpestar
Copy link

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.

@vmaerten
Copy link
Collaborator

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

@vmaerten vmaerten added type: question Further information is requested. state: awaiting response Waiting for issue author to respond. and removed state: needs triage Waiting to be triaged by a maintainer. labels May 13, 2024
@vmaerten vmaerten removed the state: awaiting response Waiting for issue author to respond. label May 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: question Further information is requested.
Projects
None yet
Development

No branches or pull requests

4 participants