-
Notifications
You must be signed in to change notification settings - Fork 215
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
Support dotenv file format #16
Comments
Didn't think someone would actually want this when I thought through it more. The dotenv lib could be used alongside this one easy enough. |
Cool it's up to you. I just wanted to make sure this is tracked somewhere because you listed it in the roadmap but there was no ticket. |
I'm still planning to do this. There are many tools out there that use "generic" Sorry for not being clear. I should have included that bit in my response. I just meant a thing "for reading .env files" is a bit odd when I think about it more. |
And I just found a good use case for supporting specifically the c.merge(config::File::new(".env", config::FileFormat::Shell)).unwrap() Remember that we don't support the above format yet. If anyone wants to work on it, follow this spec: https://github.com/bkeepers/dotenv#usage and use nom. Probably should be its own crate and with coordination from @slapresta, perhaps it can replace the hand-rolled parser in rust-dotenv (lots of TODOs in the parser so I'd guess @slapresta would be open to it). |
Hi! Improvements to the parser (which is basically a regex plus quote handling logic) are definitely welcome. Reading from a specific file should be feasible with dotenv's |
Any update about this decision? |
I needed this for myself so i created a little adapter to loading dotenv files #[derive(Debug, Clone)]
struct DotEnv;
impl config::Source for DotEnv {
fn clone_into_box(&self) -> Box<dyn config::Source + Send + Sync> {
Box::new((*self).clone())
}
fn collect(&self) -> Result<HashMap<String, config::Value>, config::ConfigError> {
let mut m = HashMap::new();
let uri: String = "the dotenv".into();
#[allow(deprecated)]
for item in dotenv::dotenv_iter().unwrap() {
let (key, value) = item.unwrap();
m.insert(
key.to_string().replace("_", ".").to_lowercase(),
config::Value::new(Some(&uri), value),
);
}
Ok(m)
}
} That you can then merge into your config: s.merge(DotEnv)?; It should probably follow the Environment options. |
Can I also add that it would be nice to support dotenv format? If you don't have time, would you be willing to accept a PR for that? |
I'd be more than happy to get a PR for that, yes! Make sure to add extensive tests! |
I have been tackling this problem for a few days now, and I think that in a sufficiently complex config file, it's impossible to automatically derive the position of the variable from the env variable. I think it's best to allow the user to interpolate any arbitrary env_variable in the config (e.g a = "$var") and then replace these with an env var (either In my project, I approached this in the following way:
What do y'all think? |
No description provided.
The text was updated successfully, but these errors were encountered: