store the filename of the loaded config file

This commit is contained in:
Dustin J. Mitchell 2021-05-05 14:41:25 -04:00
parent 09efb33073
commit a778423cbc

View file

@ -12,6 +12,9 @@ use toml::value::Table;
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
pub(crate) struct Settings { pub(crate) struct Settings {
// filename from which this configuration was loaded, if any
pub(crate) filename: Option<PathBuf>,
// replica // replica
pub(crate) data_dir: PathBuf, pub(crate) data_dir: PathBuf,
@ -33,15 +36,24 @@ impl Settings {
log::debug!("Loading configuration from {:?}", config_file); log::debug!("Loading configuration from {:?}", config_file);
env::remove_var("TASKCHAMPION_CONFIG"); env::remove_var("TASKCHAMPION_CONFIG");
Self::load_from_file(config_file.into(), true) Self::load_from_file(config_file.into(), true)
} else if let Some(mut dir) = dirs_next::config_dir() { } else if let Some(filename) = Settings::default_filename() {
dir.push("taskchampion.toml"); log::debug!("Loading configuration from {:?} (optional)", filename);
log::debug!("Loading configuration from {:?} (optional)", dir); Self::load_from_file(filename, false)
Self::load_from_file(dir, false)
} else { } else {
Ok(Default::default()) Ok(Default::default())
} }
} }
/// Get the default filename for the configuration, or None if that cannot
/// be determined.
pub(crate) fn default_filename() -> Option<PathBuf> {
if let Some(dir) = dirs_next::config_dir() {
Some(dir.join("taskchampion.toml"))
} else {
None
}
}
fn load_from_file(config_file: PathBuf, required: bool) -> Result<Self> { fn load_from_file(config_file: PathBuf, required: bool) -> Result<Self> {
let mut settings = Self::default(); let mut settings = Self::default();
@ -60,6 +72,8 @@ impl Settings {
let config_toml = config_toml let config_toml = config_toml
.parse::<toml::Value>() .parse::<toml::Value>()
.with_context(|| format!("error while reading {:?}", config_file))?; .with_context(|| format!("error while reading {:?}", config_file))?;
settings.filename = Some(config_file.clone());
settings settings
.update_from_toml(&config_toml) .update_from_toml(&config_toml)
.with_context(|| format!("error while parsing {:?}", config_file))?; .with_context(|| format!("error while parsing {:?}", config_file))?;
@ -213,6 +227,7 @@ impl Default for Settings {
); );
Self { Self {
filename: None,
data_dir, data_dir,
server_client_key: None, server_client_key: None,
server_origin: None, server_origin: None,
@ -247,10 +262,12 @@ mod test {
#[test] #[test]
fn test_load_from_file_exists() { fn test_load_from_file_exists() {
let cfg_dir = TempDir::new().unwrap(); let cfg_dir = TempDir::new().unwrap();
fs::write(cfg_dir.path().join("foo.toml"), "data_dir = \"/nowhere\"").unwrap(); let cfg_file = cfg_dir.path().join("foo.toml");
fs::write(cfg_file.clone(), "data_dir = \"/nowhere\"").unwrap();
let settings = Settings::load_from_file(cfg_dir.path().join("foo.toml"), true).unwrap(); let settings = Settings::load_from_file(cfg_file.clone(), true).unwrap();
assert_eq!(settings.data_dir, PathBuf::from("/nowhere")); assert_eq!(settings.data_dir, PathBuf::from("/nowhere"));
assert_eq!(settings.filename, Some(cfg_file));
} }
#[test] #[test]
@ -283,5 +300,6 @@ mod test {
settings.update_from_toml(&val).unwrap(); settings.update_from_toml(&val).unwrap();
assert!(settings.reports.get("foo").is_some()); assert!(settings.reports.get("foo").is_some());
// beyond existence of this report, we can rely on Report's unit tests
} }
} }