From ff1c89fec817cbd2f21c8ebc4a5c77d464a6d623 Mon Sep 17 00:00:00 2001 From: coastalwhite Date: Sat, 6 Apr 2024 17:25:00 +0200 Subject: [PATCH] Add CLI flags for xsessions and wlsessions --- src/cli.rs | 16 ++++++++++++++++ src/main.rs | 30 ++++++++++++++++++++---------- 2 files changed, 36 insertions(+), 10 deletions(-) diff --git a/src/cli.rs b/src/cli.rs index ffe236b..d6da792 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -18,6 +18,8 @@ OPTIONS: --no-log --preview --tty Override the configured TTY number + --xsessions Override the path to /usr/share/xsessions + --wlsessions Override the path to /usr/share/wayland-sessions -V, --version Print version information SUBCOMMANDS: @@ -37,6 +39,8 @@ pub struct Cli { pub config: Option, pub variables: Option, pub command: Option, + pub xsessions: Option, + pub wlsessions: Option, } pub enum Commands { @@ -80,6 +84,8 @@ impl Cli { config: None, variables: None, command: None, + xsessions: None, + wlsessions: None, }; let mut args = args().skip(1).enumerate(); @@ -107,6 +113,16 @@ impl Cli { let arg = PathBuf::from(arg); cli.config = Some(arg); } + (_, "--xsessions") => { + let (_, arg) = args.next().ok_or(CliError::MissingArgument("xsessions"))?; + let arg = PathBuf::from(arg); + cli.xsessions = Some(arg); + } + (_, "--wlsessions") => { + let (_, arg) = args.next().ok_or(CliError::MissingArgument("wlsessions"))?; + let arg = PathBuf::from(arg); + cli.wlsessions = Some(arg); + } (_, "--variables") | (_, "-v") => { let (_, arg) = args.next().ok_or(CliError::MissingArgument("variables"))?; let arg = PathBuf::from(arg); diff --git a/src/main.rs b/src/main.rs index 4648d8c..6509777 100644 --- a/src/main.rs +++ b/src/main.rs @@ -41,12 +41,11 @@ const DEFAULT_VARIABLES_PATH: &str = "/etc/lemurs/variables.toml"; const DEFAULT_CONFIG_PATH: &str = "/etc/lemurs/config.toml"; const PREVIEW_LOG_PATH: &str = "lemurs.log"; -fn merge_in_configuration( - config: &mut Config, - config_path: Option<&Path>, - variables_path: Option<&Path>, -) { - let load_variables_path = variables_path.unwrap_or_else(|| Path::new(DEFAULT_VARIABLES_PATH)); +fn merge_in_configuration(config: &mut Config, cli: &Cli) { + let load_variables_path = cli + .variables + .as_deref() + .unwrap_or_else(|| Path::new(DEFAULT_VARIABLES_PATH)); let variables = match config::Variables::from_file(load_variables_path) { Ok(variables) => { @@ -60,7 +59,7 @@ fn merge_in_configuration( Err(err) => { // If we have given it a specific config path, it should crash if this file cannot be // loaded. If it is the default config location just put a warning in the logs. - if let Some(variables_path) = variables_path { + if let Some(variables_path) = cli.variables.as_ref() { eprintln!( "The variables file '{}' cannot be loaded.\nReason: {}", variables_path.display(), @@ -78,7 +77,10 @@ fn merge_in_configuration( } }; - let load_config_path = config_path.unwrap_or_else(|| Path::new(DEFAULT_CONFIG_PATH)); + let load_config_path = cli + .config + .as_deref() + .unwrap_or_else(|| Path::new(DEFAULT_CONFIG_PATH)); match config::PartialConfig::from_file(load_config_path, variables.as_ref()) { Ok(partial_config) => { @@ -91,7 +93,7 @@ fn merge_in_configuration( Err(err) => { // If we have given it a specific config path, it should crash if this file cannot be // loaded. If it is the default config location just put a warning in the logs. - if let Some(config_path) = config_path { + if let Some(config_path) = cli.config.as_ref() { eprintln!( "The config file '{}' cannot be loaded.\nReason: {}", config_path.display(), @@ -106,6 +108,14 @@ fn merge_in_configuration( } } } + + if let Some(xsessions) = cli.xsessions.as_ref() { + config.x11.xsessions_path = xsessions.display().to_string(); + } + + if let Some(wlsessions) = cli.wlsessions.as_ref() { + config.wayland.wayland_sessions_path = wlsessions.display().to_string(); + } } pub fn initialize_panic_handler() { @@ -139,7 +149,7 @@ fn main() -> Result<(), Box> { }); let mut config = Config::default(); - merge_in_configuration(&mut config, cli.config.as_deref(), cli.variables.as_deref()); + merge_in_configuration(&mut config, &cli); if let Some(cmd) = cli.command { match cmd {