diff --git a/src/init.rs b/src/init.rs new file mode 100644 index 0000000000000000000000000000000000000000..d80fdcd3057c30e2a9672c98d4fc04e119efd766 --- /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 0000000000000000000000000000000000000000..094212b06c16940754683d806ea3f2357bae2b14 --- /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(); + } + } +}