Add a validate subcommand for config validation
Browse files at this commit74ff4f1903b03136bbe33f20834037b6586da270Parent: 8c3107a1 changed file +28 -1
- src/main.rs +28 -1
src/main.rsmodified+28 -1
| @@ -26,7 +26,7 @@ use std::path::PathBuf; | |||
| 26 | use std::process::Command; | 26 | use std::process::Command; |
| 27 | use std::{env, mem}; | 27 | use std::{env, mem}; |
| 28 | 28 | ||
| 29 | -use clap::Parser; | 29 | +use clap::{Parser, Subcommand}; |
| 30 | use config::Config; | 30 | use config::Config; |
| 31 | #[cfg(not(feature = "xdp-gnome-screencast"))] | 31 | #[cfg(not(feature = "xdp-gnome-screencast"))] |
| 32 | use dummy_pw_utils as pw_utils; | 32 | use dummy_pw_utils as pw_utils; |
| @@ -45,6 +45,9 @@ use crate::utils::{REMOVE_ENV_RUST_BACKTRACE, REMOVE_ENV_RUST_LIB_BACKTRACE}; | |||
| 45 | 45 | ||
| 46 | #[derive(Parser)] | 46 | #[derive(Parser)] |
| 47 | #[command(author, version, about, long_about = None)] | 47 | #[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")] | ||
| 48 | struct Cli { | 51 | struct Cli { |
| 49 | /// Path to config file (default: `$XDG_CONFIG_HOME/niri/config.kdl`). | 52 | /// Path to config file (default: `$XDG_CONFIG_HOME/niri/config.kdl`). |
| 50 | #[arg(short, long)] | 53 | #[arg(short, long)] |
| @@ -52,6 +55,19 @@ struct Cli { | |||
| 52 | /// Command to run upon compositor startup. | 55 | /// Command to run upon compositor startup. |
| 53 | #[arg(last = true)] | 56 | #[arg(last = true)] |
| 54 | command: Vec<OsString>, | 57 | 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 | + }, | ||
| 55 | } | 71 | } |
| 56 | 72 | ||
| 57 | fn main() -> Result<(), Box<dyn std::error::Error>> { | 73 | fn main() -> Result<(), Box<dyn std::error::Error>> { |
| @@ -95,6 +111,17 @@ fn main() -> Result<(), Box<dyn std::error::Error>> { | |||
| 95 | // Set a better error printer for config loading. | 111 | // Set a better error printer for config loading. |
| 96 | miette::set_hook(Box::new(|_| Box::new(NarratableReportHandler::new()))).unwrap(); | 112 | miette::set_hook(Box::new(|_| Box::new(NarratableReportHandler::new()))).unwrap(); |
| 97 | 113 | ||
| 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 | + | ||
| 98 | info!( | 125 | info!( |
| 99 | "starting version {} ({})", | 126 | "starting version {} ({})", |
| 100 | env!("CARGO_PKG_VERSION"), | 127 | env!("CARGO_PKG_VERSION"), |