feat: Make init check if the target is non-empty

This commit is contained in:
Nathan McCarty 2023-03-16 14:28:45 -04:00
parent 617acba799
commit f408a32d64
Signed by: thatonelutenist
GPG Key ID: D70DA3DD4D1E9F96
1 changed files with 17 additions and 4 deletions

View File

@ -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<String>,
/// 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!()
}
}