task icon indicating copy to clipboard operation
task copied to clipboard

cannot override vars from console

Open antonio-antuan opened this issue 1 year ago • 5 comments

  • Task version: 3.14.1.r21.g421cb52-1
  • Operating System: archlinux

Example Taskfile showing the issue

version: '3'


tasks:
  foo:
    vars:
      BAR: default
    cmds:
      - echo "{{.BAR}}"

I'm trying to override variable BAR:

➜  task foo         
task: [foo] echo "default"
default
➜  TASK_BAR=baz task foo
task: [foo] echo "default"
default
➜  BAR=baz task foo     
task: [foo] echo "default"
default
➜  task BAR=baz foo
task: [foo] echo "default"
default
➜  task TASK_BAR=baz foo
task: [foo] echo "default"
default
➜  task foo TASK_BAR=baz
task: [foo] echo "default"
default
➜  task foo BAR=baz     
task: [foo] echo "default"
default

Is there any way to do it?

antonio-antuan avatar Aug 26 '22 09:08 antonio-antuan

Hello @aCLr

If you want to override vars, you would be using go templates more like this

version: '3'


tasks:
  foo:
    cmds:
      - echo "{{.BAR | default "my-default-value"}}"

or alternatively this

version: '3'


tasks:
  foo:
    vars:
      BAR: '{{.BAR | default "my-default-value"}}'
    cmds:
      - echo "{{.BAR}}"

mrwormhole avatar Aug 27 '22 15:08 mrwormhole

Thanks for your help, it is more clear now.

BTW is it a good idea to make it possible to override variables in a way, introduced in my previous message?

antonio-antuan avatar Aug 28 '22 05:08 antonio-antuan

yea ofc, it is best to use them with default parameters tho, whole purpose of taskfiles is to shorten commands

mrwormhole avatar Aug 28 '22 11:08 mrwormhole

I mean in my previous message. Intuitively I want to define a variable with vars and after that I want to redefine it with cli-arguments.

default is usable but I want to say that redefining can be an option to. You can consider it as a feature request :)

antonio-antuan avatar Aug 30 '22 10:08 antonio-antuan

@aCLr unless I'm misunderstanding your message, you can achieve exactly what you are asking using @MrWormHole's suggested method. The variables documentation describes this behaviour in the section: sending parameters with environment variables.

See example below:

version: '3'

tasks:
  example:
    cmds:
      - echo "{{.MYVAR | default "foo"}}"

Run task normally (sets default value to "foo"):

❯ task example    
task: [example] echo "foo"
foo

Run task with variable overridden (sets value to "bar"):

❯ MYVAR="bar" task example
task: [example] echo "bar"
bar

If this does not fulfil your needs, please could you give an example of how you would expect this to work. I personally prefer that you need to explicitly describe how the default behaviour should work in the taskfile, rather than implicitly overriding variables. The latter could lead to unexpected behaviour in certain scenarios.

pd93 avatar Sep 09 '22 14:09 pd93

I think that we really need command line options that would allow us to override vars, without any tricks. Vars have a lot of advantages over expansion of shell variables, which is not visible inside the console.

Probably a syntax like --vars VAR1=foo,BAR2=bar would suffice, or similar alternatives.

ssbarnea avatar Oct 09 '22 10:10 ssbarnea

@ssbarnea, why not use the existing way to set variables? Only thing missing would be to override stuff internally.

task build FOO=bar

trallnag avatar Dec 17 '22 22:12 trallnag

I feel that this issue is a too loosely defined.

It is already possible to override vars, but you need to use default in the var declaration.

Changing vars to override without that would be a backward incompatible change, so I'm closing this for now. Changes on this should be pushed to the (far in the future) next major release.

andreynering avatar Jan 05 '23 01:01 andreynering

This is what I ended up doing to give variables passed at runtime priority over dynamic global or task variables:

vars:
  DEFAULT_NAME:
    sh: 'echo world'
  NAME:
    sh: 'echo {{ .NAME | default .DEFAULT_NAME }}'

tasks:
  say_hello:
    cmd: 'echo hello {{ .NAME }}'
> task say_hello
hello world

> NAME=John task say_hello
hello John

vctls avatar Oct 06 '23 11:10 vctls