use clap::Subcommand; use openssh::{Session, KnownHosts}; use std::{process::Command, io}; #[derive(Subcommand)] pub enum GitCommand { /// Create a new repository in breadpi. Init { #[arg()] /// Name of the repo name: String }, /// List all repositories in breadpi. List, /// Delete the specified repository in breadpi. Delete { #[arg()] /// Name of the repo name: String }, /// Add remote to the git repo in the current directory Add { #[arg()] /// Name of the repo name: String } } pub async fn handle_git(cmd: &GitCommand) { let session = Session::connect("git@breadpi", KnownHosts::Strict) .await .expect("Could not connect to git@breadpi"); match cmd { GitCommand::Init { name } => { let init = session.command("git") .arg("init") .arg("--bare") .arg("store/".to_owned() + name + ".git") .output().await.unwrap(); eprintln!( "{}", String::from_utf8(init.stdout).expect("Failed to init git repo.") ); print!("Would you like to add the remote to the repo in this directory? (Y/n): "); } GitCommand::Delete { name } => { let rm = session.command("rm") .arg("-r") .arg("-f") .arg("store/".to_owned() + name + ".git") .output().await.unwrap(); eprintln!( "{}", String::from_utf8(rm.stdout).expect("Failed to rm repo.") ); } GitCommand::Add { name } => { add_remote_to_repo(name.to_string()); } _ => { println!("oopsies") } } } fn add_remote_to_repo(name: String) { let cmd = Command::new("git") .arg("remote") .arg("add") .arg("origin") .arg("git@breadpi.local:/home/git/store/".to_owned() + &name + ".git") .output().expect("Could not add remote"); eprintln!( "{}", String::from_utf8(cmd.stdout).expect("Failed to rm git repo.") ); println!("Added remote origin git@breadpi.local:/home/git/store/{name}.git"); }