diff --git a/src/clone.rs b/src/clone.rs new file mode 100644 index 0000000000000000000000000000000000000000..281ae7b9fae1bfa5de05bf4c73579655e1bd1008 --- /dev/null +++ b/src/clone.rs @@ -0,0 +1,54 @@ +use std::path::PathBuf; +use std::process::exit; + +use colored::Colorize; + +use crate::configt; +use crate::init; +use crate::push; + +fn check_remote_dir(url: &PathBuf, adress: &str) { + if !push::check_dir_exist(url, adress) { + eprintln!("{}: {} no such directory", "error".red(), url.display()); + exit(1); + } + let v = vec!["data", "config", "archive_list"]; + for file in v { + let mut path = url.clone(); + path.push(file); + let exists = match file { + "data" => push::check_dir_exist(&path, adress), + _ => push::check_file_exist(&path, adress), + }; + if !exists { + let kind = if file == "data" { "folder" } else { "file" }; + eprintln!( + "{}: no {} {} inside {} directory", + "error".red(), + file, + kind, + url.display() + ); + exit(1); + } + } +} + +/// Create a push the tar achive on the selected remote path +/// # Arguments +/// - `remote`: The name of a remote +pub fn clone(path: &str) -> () { + let (adress, url) = push::split_path(path); + let (borg_path, _) = init::get_borg_folder(); + if borg_path.is_dir() { + eprintln!("{}: {} already exits.", "error".red(), &borg_path.display()); + exit(1); + } + let borg_folder = borg_path.canonicalize().unwrap(); + check_remote_dir(&url, &adress); + push::copy_file(&borg_folder, &url, &adress, "pull"); + let name = url.file_name().unwrap().to_str().unwrap(); + configt::create_named_local_config(name); + let gitignore = init::get_gitignore_file(); + init::update_gitignore(&gitignore); +} diff --git a/src/main.rs b/src/main.rs index 5666b04388769a8c58256c50b4a821ceccf1debd..70652e16ec969ba920b308ab3cb06a293bdce3ee 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,6 +6,7 @@ use colored::Colorize; use configt::PartialPrune; mod checkout; mod clean; +mod clone; mod commit; mod compact; mod config_structure; @@ -123,6 +124,8 @@ enum Commands { /// The purpose of this command is to be used if a pull command fails after /// removing content inside the .borg folder Restore, + /// Clones a repository given a destination + Clone(Clone), } #[derive(Debug, Args)] @@ -329,6 +332,12 @@ struct Pull { key: String, } +#[derive(Debug, Args)] +struct Clone { + /// The path pointing to a borg folder + path: String, +} + fn main() { let args = Cli::parse(); @@ -421,5 +430,8 @@ fn main() { Commands::Restore => { restore::restore(); } + Commands::Clone(c) => { + clone::clone(&c.path); + } } }