Add configuration-file support to the 'task' command

This commit is contained in:
Dustin J. Mitchell
2020-11-28 18:18:43 -05:00
parent 8af7ba286d
commit 0e926df578
6 changed files with 236 additions and 45 deletions

35
cli/src/settings.rs Normal file
View File

@@ -0,0 +1,35 @@
use config::{Config, Environment, File, FileSourceFile};
use failure::Fallible;
use std::env;
use std::path::PathBuf;
pub(crate) fn read_settings() -> Fallible<Config> {
let mut settings = Config::default();
// set up defaults
if let Some(mut dir) = dirs::data_local_dir() {
dir.push("taskchampion");
settings.set_default(
"data_dir",
// the config crate does not support non-string paths
dir.to_str().expect("data_local_dir is not utf-8"),
)?;
}
// load either from the path in TASKCHAMPION_CONFIG, or from CONFIG_DIR/taskchampion
if let Some(config_file) = env::var_os("TASKCHAMPION_CONFIG") {
let config_file: PathBuf = config_file.into();
let config_file: File<FileSourceFile> = config_file.into();
settings.merge(config_file.required(true))?;
env::remove_var("TASKCHAMPION_CONFIG");
} else if let Some(mut dir) = dirs::config_dir() {
dir.push("taskchampion");
let config_file: File<FileSourceFile> = dir.into();
settings.merge(config_file.required(false))?;
}
// merge environment variables
settings.merge(Environment::with_prefix("TASKCHAMPION"))?;
Ok(settings)
}