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,21 +80,12 @@ pub fn clone(remote: Values<'_>) {
// create folder // create folder
if first_iter { if first_iter {
if DirBuilder::new().create(ref_path.clone()).is_err() { if DirBuilder::new().recursive(true).create(ref_path.clone()).is_err() {
if let Ok(entries) = read_folder(ref_path.clone()) { eprintln!("fatal: unable to create the destination directory");
if entries.len() != 0 {
eprintln!("fatal: destination path '{}' already exists and is not an empty directory.", ref_path.display());
std::process::exit(1); std::process::exit(1);
} else { } else {
init::init(); init::init();
} }
} else {
eprintln!("fatal: cannot open the destination directory");
std::process::exit(1);
}
} else {
init::init();
}
} else { } else {
// create folder // create folder
let p = ref_path.clone().join(Path::new(&relative_s)); let p = ref_path.clone().join(Path::new(&relative_s));

View File

@ -1,6 +1,7 @@
use std::env; use std::env;
use std::fs::{DirBuilder, File}; use std::fs::{DirBuilder, File};
use std::path::PathBuf; use std::path::PathBuf;
use crate::utils::read::read_folder;
use crate::global::global::DIR_PATH; use crate::global::global::DIR_PATH;
pub fn init() { pub fn init() {
@ -10,6 +11,17 @@ pub fn init() {
Some(dir) => PathBuf::from(dir), Some(dir) => PathBuf::from(dir),
None => env::current_dir().unwrap(), 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(); let builder = DirBuilder::new();
// todo check if dir empty // todo check if dir empty