[args] Add the `--config` option

This commit is contained in:
Anas Elgarhy 2023-04-22 04:16:51 +02:00
parent 588ca11113
commit 0090da70ae
No known key found for this signature in database
GPG Key ID: 0501802A1D496528
1 changed files with 11 additions and 3 deletions

View File

@ -254,6 +254,10 @@ pub struct Settings {
#[arg(long, hide = true)] #[arg(long, hide = true)]
#[serde(skip)] #[serde(skip)]
markdown_help: bool, markdown_help: bool,
/// Use a custom config path
#[arg(long = "config")]
#[serde(skip)]
config_path: Option<String>,
} }
impl Default for Settings { impl Default for Settings {
@ -307,6 +311,7 @@ impl Default for Settings {
status_notification_timeout: Some(DEFAULT_STATUS_CHANGE_NOTIFICATION_TIMEOUT), status_notification_timeout: Some(DEFAULT_STATUS_CHANGE_NOTIFICATION_TIMEOUT),
#[cfg(feature = "docs")] #[cfg(feature = "docs")]
markdown_help: false, markdown_help: false,
config_path: None,
} }
} }
} }
@ -324,6 +329,7 @@ impl Settings {
// parse the args // parse the args
let args = Settings::parse(); let args = Settings::parse();
#[cfg(feature = "debug")] debug!("Args: {:?}", args);
#[cfg(feature = "docs")] #[cfg(feature = "docs")]
if args.markdown_help { if args.markdown_help {
@ -333,16 +339,19 @@ impl Settings {
std::process::exit(0); std::process::exit(0);
} }
let config_path = args.config_path.unwrap_or(
confy::get_configuration_file_path("cmus-notify", "config")
.unwrap().to_str().unwrap().to_string());
#[cfg(feature = "debug")] #[cfg(feature = "debug")]
{ {
info!("Loading config..."); info!("Loading config...");
debug!( debug!(
"Config file path: {:?}", "Config file path: {:?}",
confy::get_configuration_file_path("cmus-notify", "config") config_path
); );
} }
// load config file // load config file
let mut cfg: Self = match confy::load("cmus-notify", "config") { let mut cfg: Self = match confy::load_path(config_path) {
Ok(cfg) => cfg, Ok(cfg) => cfg,
Err(err) => { Err(err) => {
eprintln!("Failed to load config: {}", err); eprintln!("Failed to load config: {}", err);
@ -351,7 +360,6 @@ impl Settings {
}; };
#[cfg(feature = "debug")] #[cfg(feature = "debug")]
{ {
debug!("Args: {:?}", args);
debug!("Config: {:?}", cfg); debug!("Config: {:?}", cfg);
info!("Combining config and args...") info!("Combining config and args...")
} }