<@1072591948499664996> Can I split the `providers....
# 🤖|ask-kapa
f
@microscopic-river-74022 Can I split the
providers.exec.outputs.initScript.log
and use the last line as context for the kubernetes provider? Something like this:
context: ${split("${providers.exec.outputs.initScript.log}", "\n")[-1]}
m
Please stand by @fresh-angle-76971 as I search the knowledge base. I normally take about 15 seconds or so. :wave:
---------------------- Yes, you can use the split helper function to split the output of the
initScript
log and access its elements, including the last line. The split function works as follows:
Copy code
${split("a,b,c", ",")} -> ["a","b","c"]
[source]() The output of the
initScript
is available as
${providers.exec.outputs.initScript.log}
for the exec provider [source](). However, the documentation does not explicitly confirm support for negative indices (e.g.,
[-1]
for the last element) in template string array access. The examples provided only show positive indices and do not mention negative indexing. So, while you can split the log into lines, it is unclear from the documentation whether you can directly use
[-1]
to get the last line. If negative indices are not supported, you would need to use a workaround, such as ensuring your script outputs only the desired context line as the last line, and then always referencing a known positive index. In summary: - Splitting the log is supported. - Accessing the last element with
[-1]
is not explicitly documented, so it may not work as expected. Example usage (assuming positive indexing):
Copy code
yaml
context: ${split("${providers.exec.outputs.initScript.log}", "\n")[0]}
If you want to use the last line and negative indices are not supported, you may need to adjust your script or use a different approach. If you want to try negative indexing, you can test it, but be aware it is not guaranteed by the documentation. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
message has been deleted
f
@microscopic-river-74022 With the example you gave me, I got the following error:
Copy code
16:50:03 󰈺  ❯ garden deploy
[verbose] garden version: 0.14.9
Deploy 🚀

ℹ garden                    → Initializing...
/Users/spike/gits/demo/project.garden.yml:31
...
30  |   # https://github.com/garden-io/garden/issues/1477
31  |   context: ${split("${providers.exec.outputs.initScript.log}", "\n")[0]}
-----------------^
Unable to parse as valid template string.
If I move the index reference outside the {} like so:
context: ${split("${providers.exec.outputs.initScript.log}", "\n")}[0]
the parser passes but I get the following error:
Copy code
Could not read cluster from kubeconfig for context kubernetes-admin@dev-cluster-c21d8db[0]
If I remove the index reference completely, I get the following error:
Copy code
Error validating provider configuration:

context must be a string
So splitting seems to be working but index references do not...
m
Let me check the knowledge base for you again. One moment! :books:
---------------------- Thank you for providing the detailed error messages and context. Based on the knowledge sources, Garden’s template engine supports the split function to split strings into arrays, but direct array indexing within the template string (e.g.,
${split(...)[0]}
or
${split(...)[-1]}
) is not supported
. This is confirmed by a [GitHub issue]() where users encountered the same error when trying to use array indexing directly after split. The workaround suggested is to assign the result of split to a variable and then access the desired index:
Copy code
yaml
variables:
  arr: ${split("something.else", ".")}
Then use:
Copy code
yaml
context: ${var.arr[1]}
But directly using an index on the result of split inside a template string is not supported and will result in a parsing error, as you have experienced. Additionally, negative indices (e.g.,
[-1]
for the last element) are not supported in Garden’s templating engine, and there is no documented way to get the last element of an array in a single template expression. Summary: - You can split a string and assign the result to a variable. - You can access array elements by index from a variable (e.g.,
${var.arr[1]}
). - You cannot use array indexing directly on the result of split in a template string (e.g.,
${split(...)[0]}
will fail). - Negative indices are not supported. Workaround: If you need the last line, you must ensure your script outputs only the desired line, or you must know the exact index in advance. There is no built-in way to get the last element of an array in a single template expression in Garden as of now.
For more details, see the discussion and workarounds in [this GitHub issue](). ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
message has been deleted