Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Why does LoadFromPath() function of generated .pkl.go file needs .pkl file at runtime when there is already generated .pkl.go files. #375

Open
hnrNeha opened this issue Mar 28, 2024 · 2 comments

Comments

@hnrNeha
Copy link

hnrNeha commented Mar 28, 2024

Iam using pkl files for config and generated go files for it. In the process of loading config Iam using LoadFromPath() this function expects a pkl file as parameter so for this I need to have pkl executable in my system.My query is when we are already generating go file from pkl why do we need to give pkl file path again.Is there any way to load pkl files from generated code without giving path to .pkl file??

@holzensp
Copy link
Contributor

The generated Go code is the structure of the configuration (the schema, or template). It does not contain the values of the configuration. Those, you load at runtime (so that you can change configuration out-of-sync with your application code).

@bioball
Copy link
Contributor

bioball commented Mar 28, 2024

Phil is right about the generated code only being the structure, and not the values.

With that said, though, there are a couple options that you have to embed values from Pkl into your application.

Embed pkl files

One option is to use go:embed to embed those Pkl files as an embed.FS. You can then evaluate those modules straight from the embedded file system. Here is a commit that demonstrates that: bioball/pkl-go-examples@3b843c3

The benefit of this approach is that because Pkl happens at runtime, you can pass in external variables into the Pkl program. For example, you can pass in environment variables and use read("env:") within Pkl to read those environment variables. This is especially helpful for things like reading in secret values into Pkl.

The drawback is that you still need to include the pkl CLI in the deployed environment of your application.

Pre-evaluate Pkl into binary

If you really don't want to include the pkl CLI at runtime, there's another option here, which is to first evaluate Pkl into a binary format, and then embed that binary into your application. Then, you can use pkl.Unmarshal to turn that binary into your app config.

That approach is demonstrated here: bioball/pkl-go-examples@841315b

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants