diff --git a/Makefile b/Makefile index c5a125c..6c5503c 100644 --- a/Makefile +++ b/Makefile @@ -2,6 +2,9 @@ build: @cargo build +run: build + @./target/debug/bp + clean: @rm -rf target diff --git a/src/git.rs b/src/git.rs index b9eac0f..da0cd3e 100644 --- a/src/git.rs +++ b/src/git.rs @@ -1,5 +1,6 @@ use clap::Subcommand; use openssh::{Session, KnownHosts}; +use core::str; use std::process::Command; #[derive(Subcommand)] @@ -32,7 +33,7 @@ pub enum GitCommand { Clone { #[arg()] /// Name of the repo - name: String + name: Option } } @@ -71,22 +72,17 @@ pub async fn handle_git(cmd: &GitCommand) { } GitCommand::Ls => { - let ls = session.command("ls") - .arg("store/") - .arg("|") - .arg("cat") - .output().await.unwrap(); - - eprintln!( - "{}", - String::from_utf8(ls.stdout).expect("Failed to list repos.") - ); + let ls = list_repos(&session).await.join("\n"); + + print!("{ls}"); } GitCommand::Add { name } => { add_remote_to_repo(name.to_string()); } - GitCommand::Clone { name } => { clone_repo(name.to_string()); } + GitCommand::Clone { name } => { clone_repo(name, &session).await; } } + + let _ = session.close().await; } @@ -105,11 +101,36 @@ fn add_remote_to_repo(name: String) { println!("Added remote origin git@breadpi:/home/git/store/{name}.git"); } +async fn list_repos(session: &Session) -> Vec { + let buf = session.command("ls") + .arg("store/") + // .arg("|") + // .arg("cat") + .output().await.unwrap(); + + // convert from utf8 bytes to string + let s = match std::str::from_utf8(&buf.stdout) { + Ok(v) => v, + Err(e) => panic!("Invalid UTF-8 sequence: {}", e), + }; + + // split string into array`` + let list: Vec = s.split("\n").map(str::to_string).collect(); + + return list; +} + +async fn clone_repo(name: &Option, session: &Session) { + // check if name is null and present list if so + if name.is_none() { + // let list = list_repos(session).await; + } + + let unwrapped_name = "&name.unwrap()"; -fn clone_repo(name: String) { let cmd = Command::new("git") .arg("clone") - .arg("git@breadpi:/home/git/store/".to_owned() + &name + ".git") + .arg("git@breadpi:/home/git/store/".to_owned() + &unwrapped_name + ".git") .output().expect("Could not clone repo"); eprintln!( @@ -117,5 +138,5 @@ fn clone_repo(name: String) { String::from_utf8(cmd.stdout).expect("Failed to clone repo.") ); - println!("Cloned repo git@breadpi:/home/git/store/{name}.git to {name}."); + println!("Cloned repo git@breadpi:/home/git/store/{unwrapped_name}.git to {unwrapped_name}."); } \ No newline at end of file