Add a -c/--config option to use a config file different from the default

This commit is contained in:
2022-05-24 17:29:50 +02:00
parent b63d4cbac8
commit 09e3dbcb9e
4 changed files with 27 additions and 7 deletions

View File

@@ -1,4 +1,5 @@
use std::fs;
use std::path::PathBuf;
use serde::Deserialize;
@@ -14,17 +15,23 @@ struct CompleteConfig {
seasonal_clock: Config,
}
pub fn get_config() -> Option<Config> {
let xdg_dirs = xdg::BaseDirectories::new().unwrap();
let config_path = xdg_dirs
.place_config_file("seasonal-clock.toml")
.expect("cannot create configuration directory");
pub fn get_config(filename: Option<String>) -> Option<Config> {
let config_path = match filename {
Some(v) => PathBuf::from(v),
None => {
let xdg_dirs = xdg::BaseDirectories::new().unwrap();
xdg_dirs
.place_config_file("seasonal-clock.toml")
.expect("cannot create configuration directory")
}
};
let data: std::io::Result<String> = fs::read_to_string(config_path);
if let Ok(..) = data {
let complete_config: CompleteConfig = toml::from_str(&data.unwrap()).unwrap();
Some(complete_config.seasonal_clock)
} else {
eprintln!("Could not parse config file {:?}", data);
None
}
}

View File

@@ -21,6 +21,10 @@ struct Args {
/// Print the current hours name and exit
#[clap(short, long)]
now: bool,
/// Use FILE as the configuration file
#[clap(short, long, value_name = "FILE")]
config: Option<String>,
}
sctk::default_environment!(SeasonalClock, desktop);
@@ -32,7 +36,8 @@ fn main() {
println!("{}", get_current_hour_name());
std::process::exit(0);
}
let config = get_config();
let config = get_config(args.config);
run_windowed(&config);
}