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

Writing values out to a config file? #39

Closed
Swandog opened this issue Dec 16, 2021 · 2 comments
Closed

Writing values out to a config file? #39

Swandog opened this issue Dec 16, 2021 · 2 comments

Comments

@Swandog
Copy link

Swandog commented Dec 16, 2021

I'm curious if there's any support or way built in to write out values to a config file?

I'd like to write something similar to how git config works, where you can do

`git config --global A.B=C"

and it writes that value out to the global git config. Does anything close to that exist, or will I have to roll my own?

@dhardy
Copy link

dhardy commented Jan 31, 2022

So, roughly, I think this could work as follows:

  1. Construct a provider representing data to be written
  2. Construct a figment to read the config file
  3. Merge the provider into the figment
  4. Extract (complete) data from figment
  5. Write data

But:

  • this writes out default values for all parameters not specified via the provider or input file
  • this loses any comments in the source
  • output order of file bears no resemblance to input order

There are two parts to fixing this:

  • filtering data to that which needs to be written, which could perhaps be done with Figment::find_metadata but probably requires a new method or using Provider::data directly
  • merging this into the file, in order, and ideally including comments: requires using something like toml_edit instead of the toml crate

@SergioBenitez
Copy link
Owner

Does anything close to that exist, or will I have to roll my own?

Not in this crate, at least not specifically. You could certainly serialize any structure into any kind of format and write it out (as the comment above), but that would mean rewriting the user's config with your own equivalent version which wouldn't maintain formatting. To do so while maintaining formatting is rather specialized, and crates like toml-edit exist for that reason.

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