ssh_vault/
config.rs

1use crate::tools;
2use anyhow::Result;
3use config::Config;
4
5/// Load configuration from environment and config file.
6///
7/// # Errors
8///
9/// Returns an error if the configuration cannot be built or parsed.
10pub fn get() -> Result<Config> {
11    let home = tools::get_home()?;
12    let config_file = home.join(".config").join("ssh-vault").join("config.yml");
13
14    let builder = Config::builder()
15        .add_source(config::Environment::with_prefix("SSH_VAULT"))
16        .add_source(config::File::from(config_file));
17
18    match builder.build() {
19        Ok(config) => Ok(config),
20        Err(_) => Ok(Config::builder()
21            .add_source(config::Environment::with_prefix("SSH_VAULT"))
22            .build()?),
23    }
24}
25
26#[cfg(test)]
27#[allow(clippy::unwrap_used)]
28mod tests {
29    use super::*;
30
31    #[test]
32    fn test_config_get() {
33        temp_env::with_vars([("SSH_VAULT_SSHKEYS_ONLINE", Some("localhost"))], || {
34            let config = get().unwrap();
35            assert_eq!(config.get_string("sshkeys_online").unwrap(), "localhost");
36        });
37    }
38}