-
-
Notifications
You must be signed in to change notification settings - Fork 155
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
read a value from a TOML file to use as input to a placeholder #770
Comments
nopez, no support for reading your external file - you are correct in your assumption that it should probably be a Rhai extension... only we've never had the time/reason to look into provideed functions before. If you feel like looking into it, I will look forward to review the proposed functions for reading files in a meaningfull way. |
Just came back to this issue and had an idea on how we can generically yet safely support this. Let's start with this given config file # some-stuff.toml
title = "TOML Example"
[owner]
name = "Tom Preston-Werner"
[database]
enabled = true
ports = [ 8000, 8001, 8002 ]
data = [ ["delta", "phi"], [3.14] ] Now let's imagine cargo generate has this new CLI option to extend the templating context, like so cargo generate my/template --extended-vars "scopeX=./some-stuff.toml" where now cargo-generate would read the config file So that in templates one can now use those variables like this:
Further we could make the scope optional so that using it like
would directly inject everything into the
|
An alternative but less safe approach would be something like a template filter to first load the config file and second extract and output the variables content like so:
In order to make this safe, we must ensure that the named config file cannot be taken from all over the disk but only within the subtree of the CWD or generated template files. |
Hi! Thank you for this helpful tool!
I currently have a use case which I think
cargo generate
currently does not support, after going through your book and looking at the Rhai extensions.I want to read a TOML file (not
Cargo.toml
) within the directory I am currently callingcargo generate
from, grab a value from the TOML file and inject it into the{{project-name}}
placeholder.It seems like Rhai extensions support a couple of file-based APIs, but it does not support reading from a file. Is there some way I can support my use case with the current way that
cargo-generate
works that I might be missing? Otherwise, would be happy to help contribute this feature myself.The text was updated successfully, but these errors were encountered: