Closed vfarcic closed 6 months ago
I think you would be looking for something like the file package in combination with something else like the yaml package. So you could have something like:
# config.yaml file
foo: bar
struct:
key: value
# main.k file
import file
import yaml
_config_filename: str = "${file.modpath()}/config.yaml"
_config = yaml.decode(file.read(_config_filename))
my_item = _config.foo
my_item2 = _config.struct.key
kcl run main.k
# Output:
# my_item: bar
# my_item2: value
You can also combine it with with the template package, which uses handlebarsjs under the hood.
Extra: This doesn't answer the question and it's a bit off-topic but I think it can be of interest for some people with my use case (And also I'm telling you because I blame you for making me fall in love with KCL through your videos :P):
I'm currently using KCL in combination with Crossplane and because of my use case I was really looking for these kind features that allowed me to split Composition
s, Function
s and inline code into multiple files in order to preserve syntax and improve the developer experience a little bit.
So thanks to these features you can do some crazy stuff like having a Compositon
written in KCL, which uses a KCL Function
which creates a Terraform Workspace
with some TF code stored in a different file, while having a neat folder organization and preserving syntax:
It's probably not the best of the approaches to take, but it's just an example of what I think as a great synergy between KCL and Crossplane in part thanks to these system packages :)
❤️ 👋 @vfarcic Thank you for your feedback and attention. @jgascon-nx Thank you for sharing your use case. I think it would be very helpful for the community.
@jgascon-nx Your use case is very similar to mine. I completely missed the "System Package" section in the docs. That's what I needed. Thanks a ton for the tips.
@Peefy You're doing a great job both with KCL and with the Crossplane function.
Hey @jgascon-nx, just FYI, here's how I deal with kcl and crossplane resources https://github.com/mindwm/mindwm-gitops/blob/b6adfdae54704e17a0e85751c9e52343755d2480/crossplane/xrd_kcl_function.k
General Question
Is it possible to read the contents of a file and use it as a value?