From 74ff4f1903b03136bbe33f20834037b6586da270 Mon Sep 17 00:00:00 2001 From: Ivan Molodetskikh Date: Mon, 18 Dec 2023 10:19:58 +0400 Subject: Add a validate subcommand for config validation --- src/main.rs | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/main.rs b/src/main.rs index b36def2e..101ef674 100644 --- a/src/main.rs +++ b/src/main.rs @@ -26,7 +26,7 @@ use std::path::PathBuf; use std::process::Command; use std::{env, mem}; -use clap::Parser; +use clap::{Parser, Subcommand}; use config::Config; #[cfg(not(feature = "xdp-gnome-screencast"))] use dummy_pw_utils as pw_utils; @@ -45,6 +45,9 @@ use crate::utils::{REMOVE_ENV_RUST_BACKTRACE, REMOVE_ENV_RUST_LIB_BACKTRACE}; #[derive(Parser)] #[command(author, version, about, long_about = None)] +#[command(args_conflicts_with_subcommands = true)] +#[command(subcommand_value_name = "SUBCOMMAND")] +#[command(subcommand_help_heading = "Subcommands")] struct Cli { /// Path to config file (default: `$XDG_CONFIG_HOME/niri/config.kdl`). #[arg(short, long)] @@ -52,6 +55,19 @@ struct Cli { /// Command to run upon compositor startup. #[arg(last = true)] command: Vec, + + #[command(subcommand)] + subcommand: Option, +} + +#[derive(Subcommand)] +enum Sub { + /// Validate the config file. + Validate { + /// Path to config file (default: `$XDG_CONFIG_HOME/niri/config.kdl`). + #[arg(short, long)] + config: Option, + }, } fn main() -> Result<(), Box> { @@ -95,6 +111,17 @@ fn main() -> Result<(), Box> { // Set a better error printer for config loading. miette::set_hook(Box::new(|_| Box::new(NarratableReportHandler::new()))).unwrap(); + // Handle subcommands. + if let Some(subcommand) = cli.subcommand { + match subcommand { + Sub::Validate { config } => { + Config::load(config).context("error loading config")?; + info!("config is valid"); + return Ok(()); + } + } + } + info!( "starting version {} ({})", env!("CARGO_PKG_VERSION"), -- cgit