Skip to content
Snippets Groups Projects
Select Git revision
  • 72965662eb6fc241a052a6b6fc4bd88bd16600cd
  • master default protected
  • doc
  • dev_doc
  • dev_push
  • dev
6 results

clone.rs

Blame
  • clone.rs 3.37 KiB
    use std::path::PathBuf;
    use std::process::exit;
    use std::process::Command;
    
    use colored::Colorize;
    
    use crate::configt;
    use crate::create_hooks;
    use crate::init;
    use crate::push;
    use crate::remote;
    
    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);
            }
        }
    }
    
    /// Function used to get borg repository folder only if
    /// the project is inside a git repository and a results folder exits
    /// # Return
    /// Absolute path to borg repository
    pub fn get_borg_absolute_folder() -> PathBuf {
        let output = Command::new("git")
            .arg("rev-parse")
            .arg("--show-cdup")
            .output()
            .unwrap();
        match output.status.code().unwrap() {
            128 => {
                eprintln!("Not a git repository !");
                exit(128);
            }
            127 => {
                eprintln!("Git not found !");
                exit(127);
            }
            0 => (),
            num => {
                eprintln!("{}", String::from_utf8(output.stderr).unwrap());
                exit(num)
            }
        }
        let mut string_path = String::from_utf8(output.stdout).unwrap();
        if string_path.ends_with('\n') {
            string_path.pop();
        }
        if string_path.is_empty() {
            string_path.push_str("./");
        }
        let mut p = PathBuf::from(&string_path).canonicalize().unwrap();
        let mut results = p.clone();
        p.push(".borg");
        results.push("results");
        if !results.is_dir() {
            eprintln!("{} not found !", results.to_str().unwrap());
            exit(1);
        }
        p
    }
    
    /// Create a push the tar achive on the selected remote path
    /// # Arguments
    /// - `remote`: The name of a remote
    /// - `hooks`: a bolean indicating whether or not to use hooks
    /// - `compression`: The compression that will be used when creating an archive
    /// - `mode`: The mode used for checkout
    pub fn clone(path: &str, hooks: bool, compression: &str, mode: &str) -> () {
        let (adress, url) = push::split_path(path);
        let borg_folder = get_borg_absolute_folder();
        if borg_folder.is_dir() {
            eprintln!(
                "{}: {} already exits.",
                "error".red(),
                &borg_folder.display()
            );
            exit(1);
        }
        check_remote_dir(&url, &adress);
        push::copy_file(&borg_folder, &url, &adress, "clone");
        let name = url.file_name().unwrap().to_str().unwrap();
        configt::create_named_local_config(name);
        let pf = url.parent().unwrap().to_path_buf();
        let full_url = if adress != "file" {
            format!("{}:{}", &adress, &pf.display())
        } else {
            pf.to_str().unwrap().to_owned()
        };
        remote::update_config("origin", &full_url, false);
        init::update_gitignores();
        if hooks {
            create_hooks::create_hooks(compression, mode);
        }
    }