From 9beee746cbb5d565a3e228def328a50ac079f13b Mon Sep 17 00:00:00 2001 From: Fontrodona Nicolas <nicolas.fontrodona@ens-lyon.fr> Date: Wed, 1 Feb 2023 11:41:37 +0100 Subject: [PATCH] add clone module --- src/clone.rs | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 12 ++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 src/clone.rs diff --git a/src/clone.rs b/src/clone.rs new file mode 100644 index 0000000..281ae7b --- /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 5666b04..70652e1 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); + } } } -- GitLab