go-task / slim-sprig

Useful template functions for Go templates.
https://go-task.github.io/slim-sprig/
MIT License
62 stars 16 forks source link

Expand struct fields in default string #1

Closed bryceschober closed 4 years ago

bryceschober commented 4 years ago

How would I expand struct fields in a default string value? In the context of go-task, I was trying something like:

version: 3

vars:
  DATE:
    sh: date +%Y%m%d

tasks:
  print-test:
    vars:
      LABEL: >-
        {{default "latest-{{.DATE}}" .LABEL}}
    silent: true
    cmds:
      - echo "label '{{.LABEL}}'"

Which results in behavior like this:

$ task print-test
label 'latest-{{.DATE}}'
$ task print-test LABEL=hello
label 'hello'

So the usage of {{default}} is working, but it's not expanding in the default string value.

andreynering commented 4 years ago

Hi @bryceschober,

That's by design. You could do something like this instead:

version: 3

vars:
  DATE:
    sh: date +%Y%m%d

tasks:
  print-test:
    vars:
      LABEL: >-
        {{if .LABEL}}{{.LABEL}}{{else}}latest-{{.DATE}}{{end}}
    silent: true
    cmds:
      - echo "label '{{.LABEL}}'"