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

Substitute environment variables in config file #41

Open
LouneCode opened this issue Jan 16, 2025 · 0 comments
Open

Substitute environment variables in config file #41

LouneCode opened this issue Jan 16, 2025 · 0 comments

Comments

@LouneCode
Copy link

LouneCode commented Jan 16, 2025

Substitute environment variables in config file

The Substitution of the environment variables in config file enables easy implementation for the dynamic switching from Border Gateway to Relay Gateway ( same matter as in issue/feature request #28). This change will eliminate the need to duplicate configuration file or direct string manipulation/substitution in toml file. The "substitute environment variables" functionality works well in ChirpStack LNS so it might be a good solution also in the ChirpStack-gateway-mesh module?

The proposed implementation can be found, for example in
chirpstack/chirpstack/src/config.rs – line799.

Please see line // substitute environment variables in config file .

pub fn load(config_dir: &Path) -> Result<()> {
    let mut content: String = String::new();

    let paths = fs::read_dir(config_dir)?;
    for path in paths {
        let path = path.unwrap().path();

        if let Some(ext) = path.extension() {
            if ext == "toml" {
                content.push_str(
                    &fs::read_to_string(&path)
                        .context(format!("Read config file: {}", path.display()))?,
                );
            }
        }
    }

    // substitute environment variables in config file
    for (k, v) in env::vars() {
        content = content.replace(&format!("${}", k), &v);
    }

    let conf: Configuration = toml::from_str(&content)?;
    set(conf);

    Ok(())
}

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

1 participant