check if directory to clone is empty

This commit is contained in:
grimhilt 2023-06-25 17:56:55 +02:00
parent 20926514d2
commit 39d1032c14
2 changed files with 16 additions and 7 deletions

View File

@ -5,6 +5,7 @@ use std::path::{Path, PathBuf};
use clap::Values;
use regex::Regex;
use crate::utils::api::ApiProps;
use crate::utils::read::read_folder;
use crate::global::global::{DIR_PATH, set_dir_path};
use crate::services::api::ApiError;
use crate::services::req_props::{ReqProps, ObjProps};
@ -80,9 +81,17 @@ pub fn clone(remote: Values<'_>) {
// create folder
if first_iter {
if DirBuilder::new().create(ref_path.clone()).is_err() {
eprintln!("fatal: directory already exist");
// destination path 'path' already exists and is not an empty directory.
//std::process::exit(1);
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);
}
} else {
init::init();
}

View File

@ -17,27 +17,27 @@ pub fn init() {
path.push(".nextsync");
match builder.create(path.clone()) {
Ok(()) => (),
Err(_) => println!("Error: cannot create directory"),
Err(_) => println!("Error: cannot create .nextsync"),
};
path.push("objects");
match builder.create(path.clone()) {
Ok(()) => (),
Err(_) => println!("Error: cannot create directory"),
Err(_) => println!("Error: cannot create objects"),
};
path.pop();
path.push("HEAD");
match File::create(path.clone()) {
Ok(_) => (),
Err(_) => println!("Error: cannot create .nextsyncignore"),
Err(_) => println!("Error: cannot create HEAD"),
}
path.pop();
path.push("index");
match File::create(path.clone()) {
Ok(_) => (),
Err(_) => println!("Error: cannot create .nextsyncignore"),
Err(_) => println!("Error: cannot create index"),
}
path.pop();