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

read a value from a TOML file to use as input to a placeholder #770

Open
eightfilms opened this issue Oct 24, 2022 · 3 comments
Open

read a value from a TOML file to use as input to a placeholder #770

eightfilms opened this issue Oct 24, 2022 · 3 comments

Comments

@eightfilms
Copy link

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 calling cargo 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.

@taurr
Copy link
Collaborator

taurr commented Oct 26, 2022

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.

@sassman
Copy link
Member

sassman commented May 2, 2024

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 some-stuff.toml and would add a dictionary with all values from the file into the variable called scopeX that is put behind a new ext (for extended variables) variable.

So that in templates one can now use those variables like this:

# README.md

This is an example of how to use extended scopes from the new ext context

Title: {{ ext.scopeX.title }}
Owner Name: {{ ext.scopeX.owner.name }}

and so on...

Further we could make the scope optional so that using it like

cargo generate my/template --extended-vars ./some-stuff.toml

would directly inject everything into the ext scope like:

# README.md

This is an example of how to use the new ext context directly without scopes

Title: {{ ext.title }}
Owner Name: {{ ext.owner.name }}

and so on...

@sassman
Copy link
Member

sassman commented May 2, 2024

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:

# README.md

{{ 'some-stuff.toml' | read_config: 'title' }}

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.

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

No branches or pull requests

3 participants