MartinsAccount
MartinsAccount
TTask
Created by MartinsAccount on 10/19/2024 in #help
Is this the best way to create a list (that can be empty) for use with templates?
VENV_TO_INSTALL_STR:
sh: |-
for pkg in "{{join "\" \"" .PYTHON_PACKAGES}}"; do
echo '{{.VENV_ALL_PACKAGES}}' | grep -q "{\"name\": \"$pkg\"" || echo "$pkg"
done
echo ""
VENV_TO_INSTALL:
ref: initial (splitList "\n" .VENV_TO_INSTALL_STR)
VENV_TO_INSTALL_STR:
sh: |-
for pkg in "{{join "\" \"" .PYTHON_PACKAGES}}"; do
echo '{{.VENV_ALL_PACKAGES}}' | grep -q "{\"name\": \"$pkg\"" || echo "$pkg"
done
echo ""
VENV_TO_INSTALL:
ref: initial (splitList "\n" .VENV_TO_INSTALL_STR)
I need to do echo "" and use initial because if the script in VENV_TO_INSTALL_STR returns nothing, splitList would create a list with one item of an empty string (this causes problems with range , for example). To work around this I use initial, which in this case would return an empty list, however, if the shell script prints some items, that would drop one of those, so I always add an empty echo to account for that case. Is there a better way of handling this? Also, is there any way to use if statements in ref: keys? Edit: I think this parses the stuff in ref: keys: https://github.com/go-task/task/blob/27056a9827ca36f44df72cbd89a98c77c101cb07/internal/templater/templater.go#L47
t, err := template.New("resolver").Funcs(templateFuncs).Parse(fmt.Sprintf("{{%s}}", ref))
t, err := template.New("resolver").Funcs(templateFuncs).Parse(fmt.Sprintf("{{%s}}", ref))
I tried tricking it by doing }}{{ [if statement stuff] }}{{ but I kept getting various errors, seems like it is expecting it to be only one {{ }} node? I also checked if they can be embedded in parenthesis, but that also didn't work out.
3 replies