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

Overriding files #41

Open
frederikhors opened this issue Mar 30, 2020 · 4 comments
Open

Overriding files #41

frederikhors opened this issue Mar 30, 2020 · 4 comments

Comments

@frederikhors
Copy link

Coming from https://github.com/bkeepers/dotenv, is there a way to use these files too: https://github.com/bkeepers/dotenv#what-other-env-files-can-i-use?

Hierarchy Priority Filename Environment Should I .gitignoreit? Notes
1st (highest) .env.development.local Development Yes! Local overrides of environment-specific settings.
1st .env.test.local Test Yes! Local overrides of environment-specific settings.
1st .env.production.local Production Yes! Local overrides of environment-specific settings.
2nd .env.local Wherever the file is Definitely. Local overrides. This file is loaded for all environments except test.
3rd .env.development Development No. Shared environment-specific settings
3rd .env.test Test No. Shared environment-specific settings
3rd .env.production Production No. Shared environment-specific settings
Last .env All Environments Depends (See below) The Original®
@logankeenan
Copy link

I've done this as a work around. This is only part of the table you've referenced.

fn load_environment_variables() {
    let environment = env::var("RUST_ENV").unwrap_or_else(|_| "".to_string());
    
    if environment == "development" {
        dotenv::from_filename(".env.development").ok();
    }
    if environment == "test" {
        dotenv::from_filename(".env.test").ok();
    }
    if environment == "production" {
        dotenv::from_filename(".env.production").ok();
    }
    dotenv::dotenv().ok();
}

@MalpenZibo
Copy link

I've done this in one of my projects. (Not tested)

#[derive(Deserialize, Debug, Clone)]
pub struct Config {
    auth_url: String,
    client_id: String,
    scope: String,
    redirect_uri: String,
}

fn load_config() -> Result<Config, envy::Error> {
    let profile = if cfg!(test) {
        "test"
    } else if cfg!(debug_assertions) {
        "development"
    } else {
        "production"
    };

    dotenv::from_filename(format!(".env.{}.local", profile)).ok();
    dotenv::from_filename(".env.local").ok();
    dotenv::from_filename(format!(".env.{}", profile)).ok();
    dotenv::dotenv().ok();

    envy::from_env::<Config>()
}

I've also used envy -> https://github.com/softprops/envy

@pksunkara
Copy link

I have my implementation here.

@billy1624
Copy link

Nice workarounds! I was looking for this just now. But I wonder why this is not part of dotenv core features?

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

5 participants