yaml formatted data

Assuming a variable ARRAY like in https://taskfile.dev/blog/any-variables/#arrays , but YAML formatted. Is it possible, loop over a variable containing YAML formatted data (in this case a list) either inline or loaded from a .yaml file with templating functions? example data:
mydata:
- one
- two
- three
mydata:
- one
- two
- three
Any Variables | Task
Task variables are no longer limited to strings!
From An unknown user
From An unknown user
4 Replies
pbitty
pbitty4mo ago
@cloudneopren , I want to make sure I understand your question. The example in the docs shows:
tasks:
foo:
vars:
ARRAY: [1, 2, 3]
tasks:
foo:
vars:
ARRAY: [1, 2, 3]
Are you asking about whether you can format the array like this?
tasks:
foo:
vars:
ARRAY:
- 1
- 2
- 3
tasks:
foo:
vars:
ARRAY:
- 1
- 2
- 3
If so, yes. Both formats are valid YAML. If that is not your question, could you expand on it?
cloudneopren
cloudneoprenOP3mo ago
Hi @pbitty, this goes into the right direction. My use case: In case there is a file file.json with a list of tools to be installed each with configurable package managers: [{"cosign":{"pkm":"arkade"}},{"flux":{"pkm":"go"}},{"kubectl":{"pkm":"arkade"}}] Not sure, how to reference the key of the map and the "pkm" field:
json-from-file:
vars:
FILE:
sh: cat file.json
ARRAY:
ref: "fromJson .FILE"
cmds:
- echo FILE={{.FILE}}
- echo ARRAY={{.ARRAY}}
- for:
var: ARRAY
cmd: echo ITEM={{.ITEM.pkm}} KEY={{.KEY}}
json-from-file:
vars:
FILE:
sh: cat file.json
ARRAY:
ref: "fromJson .FILE"
cmds:
- echo FILE={{.FILE}}
- echo ARRAY={{.ARRAY}}
- for:
var: ARRAY
cmd: echo ITEM={{.ITEM.pkm}} KEY={{.KEY}}
pbitty
pbitty3mo ago
I think your issue is that the top-level type in file.json is not a map but an array. If I use the following file.json:
{
"cosign":{"pkm":"arkade"},
"flux":{"pkm":"go"},
"kubectl":{"pkm":"arkade"}
}
{
"cosign":{"pkm":"arkade"},
"flux":{"pkm":"go"},
"kubectl":{"pkm":"arkade"}
}
And the following Taskfile:
version: '3'

tasks:
default:
vars:
FILE:
sh: cat file.json
ARRAY:
ref: "fromJson .FILE"
cmds:
- for:
var: ARRAY
cmd: |
echo '{{.KEY}}: {{.ITEM.pkm}}'
version: '3'

tasks:
default:
vars:
FILE:
sh: cat file.json
ARRAY:
ref: "fromJson .FILE"
cmds:
- for:
var: ARRAY
cmd: |
echo '{{.KEY}}: {{.ITEM.pkm}}'
I get the output:
task: [default] echo 'cosign: arkade'

cosign: arkade
task: [default] echo 'flux: go'

flux: go
task: [default] echo 'kubectl: arkade'

kubectl: arkade
task: [default] echo 'cosign: arkade'

cosign: arkade
task: [default] echo 'flux: go'

flux: go
task: [default] echo 'kubectl: arkade'

kubectl: arkade
Would something like that work for your use-case?
cloudneopren
cloudneoprenOP3mo ago
Thanks, looks good
Want results from more Discord servers?
Add your server