From bccdc77780288608d0b61f17d807ef968cef7015 Mon Sep 17 00:00:00 2001
From: Fontrodona Nicolas <nicolas.fontrodona@ens-lyon.fr>
Date: Tue, 10 May 2022 14:17:09 +0200
Subject: [PATCH] initial code commit

---
 src/init.rs | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 src/main.rs | 29 +++++++++++++++++++++++
 2 files changed, 95 insertions(+)
 create mode 100644 src/init.rs
 create mode 100644 src/main.rs

diff --git a/src/init.rs b/src/init.rs
new file mode 100644
index 0000000..d80fdcd
--- /dev/null
+++ b/src/init.rs
@@ -0,0 +1,66 @@
+use std::path::PathBuf;
+use std::process::{exit, Command};
+
+/// Function used to get git folder
+pub fn get_borg_folder() -> (PathBuf, 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();
+    }
+    let mut p = PathBuf::from(&string_path);
+    let mut results = p.clone();
+    p.push(".borg");
+    results.push("results");
+    (p, results)
+}
+
+/// Creation of a borg repository in the same directory as the .git repository
+pub fn init_repository() {
+    let (borg_path, results) = get_borg_folder();
+    if !results.is_dir() {
+        eprintln!("{} not found !", results.to_str().unwrap());
+        exit(1);
+    }
+    let output = Command::new("borg")
+        .arg("init")
+        .arg("--encryption=none")
+        .arg(&borg_path)
+        .output()
+        .unwrap();
+    match output.status.code().unwrap() {
+        128 => {
+            eprintln!("borg not found !");
+            exit(128)
+        }
+        0 => {
+            println!(
+                "borg repository initialised at {}",
+                borg_path.to_str().unwrap()
+            );
+        }
+        num => {
+            eprintln!("{}", String::from_utf8(output.stderr).unwrap());
+            exit(num);
+        }
+    }
+}
diff --git a/src/main.rs b/src/main.rs
new file mode 100644
index 0000000..094212b
--- /dev/null
+++ b/src/main.rs
@@ -0,0 +1,29 @@
+use clap::{Parser, Subcommand};
+
+mod init;
+
+#[derive(Debug, Parser)]
+#[clap(name = "gbl")]
+/// A tool used to link borg and git together
+///
+/// This tool was created to link borg and git together and ease the management of developpment artifact versionning using git
+struct Cli {
+    #[clap(subcommand)]
+    commands: Commands,
+}
+
+#[derive(Debug, Subcommand)]
+enum Commands {
+    /// Initialize a borg repository inside a git project
+    Init,
+}
+
+fn main() {
+    let args = Cli::parse();
+
+    match args.commands {
+        Commands::Init => {
+            init::init_repository();
+        }
+    }
+}
-- 
GitLab