forge
Explore
Log in

Don't you dare go Hollow

Add a validate subcommand for config validation

Browse files at this commit

74ff4f1903b03136bbe33f20834037b6586da270Ivan MolodetskikhParent: 8c3107a
src/main.rsmodified+28 -1
@@ -26,7 +26,7 @@ use std::path::PathBuf;
2626 use std::process::Command;
2727 use std::{env, mem};
2828
29-use clap::Parser;
29+use clap::{Parser, Subcommand};
3030 use config::Config;
3131 #[cfg(not(feature = "xdp-gnome-screencast"))]
3232 use dummy_pw_utils as pw_utils;
@@ -45,6 +45,9 @@ use crate::utils::{REMOVE_ENV_RUST_BACKTRACE, REMOVE_ENV_RUST_LIB_BACKTRACE};
4545
4646 #[derive(Parser)]
4747 #[command(author, version, about, long_about = None)]
48+#[command(args_conflicts_with_subcommands = true)]
49+#[command(subcommand_value_name = "SUBCOMMAND")]
50+#[command(subcommand_help_heading = "Subcommands")]
4851 struct Cli {
4952 /// Path to config file (default: `$XDG_CONFIG_HOME/niri/config.kdl`).
5053 #[arg(short, long)]
@@ -52,6 +55,19 @@ struct Cli {
5255 /// Command to run upon compositor startup.
5356 #[arg(last = true)]
5457 command: Vec<OsString>,
58+
59+ #[command(subcommand)]
60+ subcommand: Option<Sub>,
61+}
62+
63+#[derive(Subcommand)]
64+enum Sub {
65+ /// Validate the config file.
66+ Validate {
67+ /// Path to config file (default: `$XDG_CONFIG_HOME/niri/config.kdl`).
68+ #[arg(short, long)]
69+ config: Option<PathBuf>,
70+ },
5571 }
5672
5773 fn main() -> Result<(), Box<dyn std::error::Error>> {
@@ -95,6 +111,17 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
95111 // Set a better error printer for config loading.
96112 miette::set_hook(Box::new(|_| Box::new(NarratableReportHandler::new()))).unwrap();
97113
114+ // Handle subcommands.
115+ if let Some(subcommand) = cli.subcommand {
116+ match subcommand {
117+ Sub::Validate { config } => {
118+ Config::load(config).context("error loading config")?;
119+ info!("config is valid");
120+ return Ok(());
121+ }
122+ }
123+ }
124+
98125 info!(
99126 "starting version {} ({})",
100127 env!("CARGO_PKG_VERSION"),