Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Example of passing environment variables from cli #1055

Answered by bioball
BusyByte asked this question in Q&A
Discussion options

Pkl 0.28.2 (macOS 14.7.2, native) (installed with homebrew)

I've defined a bash script

#!/bin/bash
# Check if the correct number of arguments are provided
if [ "$#" -ne 2 ]; then
 echo "Usage: 0ドル <service_name> <component>"
 exit 1
fi
# Set environment variables
export SERVICE_NAME=1ドル
export COMPONENT=2ドル
# Run the pkl command
pkl eval -f json -e SERVICE_NAME -e COMPONENT ./pkl-templates/my-template.pkl

and in the pkl file

COMPONENT = read("env:COMPONENT")
SERVICE_NAME = read("env:SERVICE_NAME")
...
 Tags = new Dynamic {
 component = List(
 "${COMPONENT}"
 )
 }
...

but it is not rendering it correctly here's what I get:

{
...
"Tags": {
 "component": [
 "${COMPONENT}"
 ]
 }
}

Any help? The docs were not very clear that I could find about how to fit the cli and the pkl files together.

You must be logged in to vote

Your syntax for variable interpolation is incorrect:

-"${COMPONENT}"
+"\(COMPONENT)"

You actually don't even need interpolation here, because COMPONENT is already a string. So it can just be component = List(COMPONENT).

In case you aren't already, I highly recommend using one of our editor plugins for writing Pkl, because it they will provide feedback about this to you.

Some other notes:

  1. List is a data type that's meant for in-language operations (map/flatmap/filter/etc). If you want to describe data, you should use Listing.

  2. If your module only makes sense as JSON, you should go ahead and set the output.renderer. Then, you don't need to add -f json anymore as a CLI flag.

    output {
     re...

Replies: 2 comments 2 replies

Comment options

Your syntax for variable interpolation is incorrect:

-"${COMPONENT}"
+"\(COMPONENT)"

You actually don't even need interpolation here, because COMPONENT is already a string. So it can just be component = List(COMPONENT).

In case you aren't already, I highly recommend using one of our editor plugins for writing Pkl, because it they will provide feedback about this to you.

Some other notes:

  1. List is a data type that's meant for in-language operations (map/flatmap/filter/etc). If you want to describe data, you should use Listing.

  2. If your module only makes sense as JSON, you should go ahead and set the output.renderer. Then, you don't need to add -f json anymore as a CLI flag.

    output {
     renderer = new JsonRenderer {}
    }
  3. I think it makes more sense to model your inputs as external properties instead of env vars.

    pkl eval -p SERVICE_NAME="1ドル" -p COMPONENT="2ドル" pkl-templates/my-template.pkl

    With this, you'd read("prop:SERVICE_NAME")

  4. Instead of using Dynamic, try defining classes for your schema. Dynamic is sort of an escape hatch and has its uses, but generally you wouldn't use it for describing data. Also, try separating your data and schema into different files

    // MySchema.pkl
    Tags: Tags
    class Tags {
     component: Listing<String>
    }
    output {
     renderer = new JsonRenderer {}
    }
    // myData.pkl
    amends "MySchema.pkl"
    tags {
     component {
     read("prop:COMPONENT")
     }
    }
You must be logged in to vote
1 reply
Comment options

@bioball I've updated my files based on your feedback and everything is working now, thanks for the prompt help.

Answer selected by BusyByte
Comment options

@bioball I do have to interpolate it into a string somewhere else I omitted :

 PolicyName = "my-github-org-${SERVICE_NAME}-deploy"

So I assume that is not how I should interpolate it.

Updated: I found the doc say \(<expr>) . This is what I get for trusting AI generated content.

You must be logged in to vote
1 reply
Comment options

Ah, yeah, as far as I can tell, none of the LLMs are good at Pkl

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants

AltStyle によって変換されたページ (->オリジナル) /