use std::path::PathBuf;
use super::{CONFIG_ENV_VAR, environment::EnvProvider};
#[derive(Debug, PartialEq, Eq)]
pub(super) struct ConfigPathResolution {
pub(super) selector: &'static str,
pub(super) path: Option<PathBuf>,
pub(super) environment_lookups: Vec<(&'static str, Option<PathBuf>)>,
}
#[cfg(test)]
pub(super) fn explicit_config_path_with_env(
cli: &super::Cli,
env: &impl EnvProvider,
) -> Option<PathBuf> {
resolve_config_selector(cli.config.clone(), env).path
}
pub(super) fn resolve_config_selector(
cli_config: Option<PathBuf>,
env: &impl EnvProvider,
) -> ConfigPathResolution {
if let Some(path) = cli_config {
return ConfigPathResolution {
selector: "cli_flag",
path: Some(path),
environment_lookups: Vec::new(),
};
}
let primary_path = env_config_path(env, CONFIG_ENV_VAR);
ConfigPathResolution {
selector: primary_path.as_ref().map_or("none", |_| CONFIG_ENV_VAR),
environment_lookups: vec![(CONFIG_ENV_VAR, primary_path.clone())],
path: primary_path,
}
}
pub(super) fn env_config_path(env: &impl EnvProvider, var_name: &str) -> Option<PathBuf> {
env.get(var_name)
.filter(|value| !value.is_empty())
.map(PathBuf::from)
}