Skip to content

Substitute environment variables in config file #41

Closed
@LouneCode

Description

@LouneCode

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(())
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions