init.rs 1.82 KiB
use std::path::PathBuf;
use std::process::{exit, Command};
/// Function used to get borg repository folder and results repository only if
/// the project is inside a git repository
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");
if !results.is_dir() {
eprintln!("{} not found !", results.to_str().unwrap());
exit(1);
}
(p, results)
}
/// Creation of a borg repository in the same directory as the .git repository
pub fn init_repository() {
let (borg_path, _) = get_borg_folder();
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);
}
}
}