From f408a32d64e08b092b17635ba19e0f2c00487c58 Mon Sep 17 00:00:00 2001 From: Nathan McCarty Date: Thu, 16 Mar 2023 14:28:45 -0400 Subject: [PATCH] feat: Make init check if the target is non-empty --- src/bin/cli/init.rs | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/src/bin/cli/init.rs b/src/bin/cli/init.rs index 7aaf36a..f5bbf7c 100644 --- a/src/bin/cli/init.rs +++ b/src/bin/cli/init.rs @@ -1,5 +1,5 @@ use clap::Args; -use color_eyre::eyre::Result; +use color_eyre::eyre::{bail, ensure, Context, Result}; use tracing::instrument; use super::GlobalArgs; @@ -10,13 +10,26 @@ pub struct InitArgs { /// Domain name to use #[arg(long, short)] pub doman_name: Option, + /// Force initialization + #[arg(long, short)] + pub force: bool, } impl InitArgs { /// Run the init command #[instrument] - pub fn run(self, _globals: GlobalArgs) -> Result<()> { - println!("Hello world!"); - Ok(()) + pub fn run(self, globals: GlobalArgs) -> Result<()> { + // Get the site directory + let site_dir = globals.site_path().context("Failed getting site dir")?; + // Check to see if the directory is non-empty + let children = site_dir.read_dir().map(Iterator::count).unwrap_or(0); + if !(children == 0 || self.force) { + bail!( + "Site directory ({:?}) is non empty, pass -f/--force to initalize anyway", + site_dir + ); + } + println!("Initalizing new site in {}", site_dir.to_string_lossy()); + todo!() } }