check if directory where the init command is clone is empty

This commit is contained in:
grimhilt 2023-06-25 18:03:56 +02:00
parent 39d1032c14
commit d3592a5209
2 changed files with 15 additions and 12 deletions

View File

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

View File

@ -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