feat: Validate subcommand

This commit is contained in:
Nathan McCarty 2023-03-19 15:31:25 -04:00
parent 8bdfcaa1d8
commit fc62d8c30c
Signed by: thatonelutenist
GPG Key ID: D70DA3DD4D1E9F96
2 changed files with 39 additions and 0 deletions

View File

@ -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),
}
}
}

33
src/bin/cli/validate.rs Normal file
View File

@ -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(())
}
}