Skip to content

Commit

Permalink
Add CLI flags for xsessions and wlsessions
Browse files Browse the repository at this point in the history
  • Loading branch information
coastalwhite committed Apr 6, 2024
1 parent fa425e5 commit ff1c89f
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 10 deletions.
16 changes: 16 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ OPTIONS:
--no-log
--preview
--tty <N> Override the configured TTY number
--xsessions <DIR> Override the path to /usr/share/xsessions
--wlsessions <DIR> Override the path to /usr/share/wayland-sessions
-V, --version Print version information
SUBCOMMANDS:
Expand All @@ -37,6 +39,8 @@ pub struct Cli {
pub config: Option<PathBuf>,
pub variables: Option<PathBuf>,
pub command: Option<Commands>,
pub xsessions: Option<PathBuf>,
pub wlsessions: Option<PathBuf>,
}

pub enum Commands {
Expand Down Expand Up @@ -80,6 +84,8 @@ impl Cli {
config: None,
variables: None,
command: None,
xsessions: None,
wlsessions: None,
};

let mut args = args().skip(1).enumerate();
Expand Down Expand Up @@ -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);
Expand Down
30 changes: 20 additions & 10 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -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(),
Expand All @@ -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) => {
Expand All @@ -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(),
Expand All @@ -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() {
Expand Down Expand Up @@ -139,7 +149,7 @@ fn main() -> Result<(), Box<dyn Error>> {
});

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 {
Expand Down

0 comments on commit ff1c89f

Please sign in to comment.