Respect XDG_CONFIG_HOME for config.toml location (#41)

* Respect XDG_CONFIG_HOME for config.toml location
* add option to specify config via commandline

closes #39
This commit is contained in:
Ferdinand Schober
2023-12-01 11:16:56 +01:00
committed by GitHub
parent e88241e816
commit e6677c3061

View File

@@ -44,6 +44,7 @@ impl Error for MissingParameter {}
impl ConfigToml {
pub fn new(path: &str) -> Result<ConfigToml, Box<dyn Error>> {
let config = fs::read_to_string(path)?;
log::info!("using config: \"{path}\"");
Ok(toml::from_str::<_>(&config)?)
}
}
@@ -77,10 +78,25 @@ pub struct Config {
impl Config {
pub fn new() -> Result<Self, Box<dyn Error>> {
let config_path = "config.toml";
let config_toml = match ConfigToml::new(config_path) {
let config_file = "config.toml";
#[cfg(unix)] let config_path = {
let xdg_config_home = env::var("XDG_CONFIG_HOME")
.unwrap_or(format!("{}/.config", env::var("HOME")?));
format!("{xdg_config_home}/lan-mouse/{config_file}")
};
#[cfg(not(unix))] let config_path = {
let app_data = env::var("LOCALAPPDATA")
.unwrap_or(format!("{}/.config", env::var("USERPROFILE")?));
format!("{app_data}\\lan-mouse\\{config_file}")
};
// --config <file> overrules default location
let config_path = find_arg("--config")?.unwrap_or(config_path);
let config_toml = match ConfigToml::new(config_path.as_str()) {
Err(e) => {
log::error!("config.toml: {e}");
log::error!("{config_path}: {e}");
log::warn!("Continuing without config file ...");
None
},