From fc62d8c30c1f7fa2acc7e81aaf524645fd3dcd60 Mon Sep 17 00:00:00 2001 From: Nathan McCarty Date: Sun, 19 Mar 2023 15:31:25 -0400 Subject: [PATCH] feat: Validate subcommand --- src/bin/cli/mod.rs | 6 ++++++ src/bin/cli/validate.rs | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 src/bin/cli/validate.rs diff --git a/src/bin/cli/mod.rs b/src/bin/cli/mod.rs index 473d0f0..2a1d98c 100644 --- a/src/bin/cli/mod.rs +++ b/src/bin/cli/mod.rs @@ -4,9 +4,12 @@ use snafu::{ensure, ResultExt, Snafu}; use std::{env::current_dir, path::PathBuf}; mod init; +mod validate; pub use init::InitArgs; +use self::validate::ValidateArgs; + /// Error encountered while parsing CLI #[derive(Debug, Snafu)] pub enum CliError { @@ -81,6 +84,8 @@ impl GlobalArgs { pub enum Command { /// Initialize a site Init(InitArgs), + /// Valiate the structure and configuration of a site + Validate(ValidateArgs), } impl Command { @@ -88,6 +93,7 @@ impl Command { pub fn run(self, globals: GlobalArgs) -> Result<()> { match self { Command::Init(args) => args.run(globals), + Command::Validate(args) => args.run(globals), } } } diff --git a/src/bin/cli/validate.rs b/src/bin/cli/validate.rs new file mode 100644 index 0000000..37959b0 --- /dev/null +++ b/src/bin/cli/validate.rs @@ -0,0 +1,33 @@ +use clap::Args; +use color_eyre::eyre::{Context, Result}; +use stranger_site_gen::site::Site; +use tracing::{debug, instrument}; + +use super::GlobalArgs; + +/// Arguments for the validate subcommand +#[derive(Args, Debug)] +pub struct ValidateArgs { + /// Dump the site configuration to the console + #[arg(long, short)] + pub dump_config: bool, +} + +impl ValidateArgs { + /// Run the validate command + #[instrument] + pub fn run(self, globals: GlobalArgs) -> Result<()> { + // Get the site directory + let site_dir = globals.site_path().context("Failed getting site dir")?; + debug!(?site_dir, "Attempting to load site directory"); + // Attempt to load the site + let site = Site::read(&site_dir).context("Failed to validate site!"); + // If requested, dump the configuration to the console + println!("Site is valid"); + if self.dump_config { + println!("Site state:"); + println!("{:#?}", site); + } + Ok(()) + } +}