Clean up of env config PR

This commit is contained in:
ViViDboarder 2021-07-23 15:27:45 -07:00
parent f25278ea3c
commit 5fd947f127
1 changed files with 11 additions and 13 deletions

View File

@ -26,7 +26,7 @@ pub fn read_config() -> Config {
println!("{}", err); println!("{}", err);
match read_config_from_env() { match read_config_from_env() {
Ok(config) => config, Ok(config) => config,
Err(err) => panic!("{}", err) Err(err) => panic!("{}", err),
} }
} }
} }
@ -36,27 +36,25 @@ pub fn read_config() -> Config {
pub fn read_config_from_file() -> Result<Config, String> { pub fn read_config_from_file() -> Result<Config, String> {
let config_path = get_config_path(); let config_path = get_config_path();
let contents = fs::read_to_string(&config_path).map_err(|_| { let contents = fs::read_to_string(&config_path)
format!("Failed to open config file at {}", config_path) .map_err(|_| format!("Failed to open config file at {}", config_path))?;
})?; let config: Config = toml::from_str(contents.as_str())
let config: Config = toml::from_str(contents.as_str()).map_err(|_| { .map_err(|_| format!("Failed to parse config file at {}", config_path))?;
format!("Failed to parse config file at {}", config_path)
})?;
println!("Reading config from file at {}", config_path); println!("Config read from file at {}", config_path);
Ok(config) Ok(config)
} }
// Tries to read configuration from environment // Tries to read configuration from environment
pub fn read_config_from_env() -> Result<Config, String> { pub fn read_config_from_env() -> Result<Config, String> {
let config = envy::from_env().map_err(|err| { let config = envy::prefixed("APP_")
format!("error parsing config from env: {}", err) .from_env()
})?; .map_err(|err| format!("Error parsing config from env: {}", err))?;
println!("Reading config from environment"); println!("Config read from environment");
Ok(config) Ok(config)
} }
#[derive(Deserialize, Debug)] #[derive(Deserialize)]
#[serde(deny_unknown_fields)] #[serde(deny_unknown_fields)]
/// Contains all config values for LDAP syncing /// Contains all config values for LDAP syncing
pub struct Config { pub struct Config {