Custom Resource provider for shell commands? _or...how to get cheat Pkl and get a UUID for something_ #1006
-
I writing a Unfortunately, UTM
WO I tried using a "Custom Resource Reader" and wiring it up to a My basic thought was some "interpreted-language custom resource" be useful. Thought was to use bash read() to get the msgpack (and pull out an the needed id), get UUID from The no data... made me question whether I'd read the spec right. So also tried some bun and nodejs basic IO code (no libraries) to read the MsgPack provided by pkl pipe via custom resource invocation, that also just "hang" on read. I then realized it must be using non-buffered IO, so char-by-char reading in node or bash would work. While I got to in bash, Instead, I just used read("https://random.org...) for now. But even that has issues. A read(URL) is correctly cache'd, but that logic means you actually get same now "non-random" results when trying to generate a new UUID. Now UTM cares only that there are no dups, so the actual code has to use a sequence number provided else where to make them "locally unique". function RndStr(length : UInt(this < 512)) = read("https://www.random.org/sequences/?min=10&max=99&col=1000&format=plain&rnd=new").text.trim().take((length+(length/2-1)).toInt()).replaceAll("\t","")
function RndUUID(altsalt : Number(this <= 9999)) = "\(RndStr(8))-\(altsalt.toString().padStart(4,"0"))-\(RndStr(4))-\(RndStr(4))-\(RndStr(12))" But what I wanted to do was just run "/usr/bin/uuidgen" but Pkl does not seem to have an API to invoke the shell/process either... I guess my main questions is:
My "feature request" here be some |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Pkl's function uuid(seed: Int) = read("uuid:\(seed)") If you use Pkl as a CLI, you can do this by creating an external reader. We provide helper libraries for writing external readers in pkl-go and pkl-swift. These help you create an executable, which you can then provide to Pkl with the If you use Pkl as a library (with Java, Swift, Go, and many of the community language bindings), you can define a resource reader in the corresponding language binding. However, Pkl will never have a local seed = read("prop:seed") Then run Pkl with: pkl eval --prop seed=$(random) myProgram.pkl If you just need one UUID, you can always just pass it in as an external property, too. pkl eval --prop uuid=$(uuidgen) myProgram.pkl |
Beta Was this translation helpful? Give feedback.
One big obstacle here is determining what
seed
should when you're generating multiple identified resources! I've found a few ways to do this in different situations that might be viable:read("uuid:\(new PcfRenderer {}.renderValue(<value without uuid>).sha1)")
If you're hashing you can also directly generate UUIDs from those hashes if you're clever and save all the trouble of an external reader entirely.