From d3592a5209f25929dbf382e0b8e722a4c934d869 Mon Sep 17 00:00:00 2001 From: grimhilt Date: Sun, 25 Jun 2023 18:03:56 +0200 Subject: [PATCH] check if directory where the init command is clone is empty --- src/commands/clone.rs | 15 +++------------ src/commands/init.rs | 12 ++++++++++++ 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/src/commands/clone.rs b/src/commands/clone.rs index 4c6b91c..9e1a05f 100644 --- a/src/commands/clone.rs +++ b/src/commands/clone.rs @@ -80,18 +80,9 @@ pub fn clone(remote: Values<'_>) { // create folder if first_iter { - if DirBuilder::new().create(ref_path.clone()).is_err() { - if let Ok(entries) = read_folder(ref_path.clone()) { - if entries.len() != 0 { - eprintln!("fatal: destination path '{}' already exists and is not an empty directory.", ref_path.display()); - std::process::exit(1); - } else { - init::init(); - } - } else { - eprintln!("fatal: cannot open the destination directory"); - std::process::exit(1); - } + if DirBuilder::new().recursive(true).create(ref_path.clone()).is_err() { + eprintln!("fatal: unable to create the destination directory"); + std::process::exit(1); } else { init::init(); } diff --git a/src/commands/init.rs b/src/commands/init.rs index 840f9a7..ab794b9 100644 --- a/src/commands/init.rs +++ b/src/commands/init.rs @@ -1,6 +1,7 @@ use std::env; use std::fs::{DirBuilder, File}; use std::path::PathBuf; +use crate::utils::read::read_folder; use crate::global::global::DIR_PATH; pub fn init() { @@ -10,6 +11,17 @@ pub fn init() { Some(dir) => PathBuf::from(dir), None => env::current_dir().unwrap(), }; + + if let Ok(entries) = read_folder(path.clone()) { + if entries.len() != 0 { + eprintln!("fatal: destination path '{}' already exists and is not an empty directory.", path.display()); + std::process::exit(1); + } + } else { + eprintln!("fatal: cannot open the destination directory"); + std::process::exit(1); + } + let builder = DirBuilder::new(); // todo check if dir empty