Implemented new list function

This commit is contained in:
breadone 2024-07-28 17:06:02 +12:00
parent ae1a08fe57
commit a872e4681d
No known key found for this signature in database
2 changed files with 39 additions and 15 deletions

View File

@ -2,6 +2,9 @@
build: build:
@cargo build @cargo build
run: build
@./target/debug/bp
clean: clean:
@rm -rf target @rm -rf target

View File

@ -1,5 +1,6 @@
use clap::Subcommand; use clap::Subcommand;
use openssh::{Session, KnownHosts}; use openssh::{Session, KnownHosts};
use core::str;
use std::process::Command; use std::process::Command;
#[derive(Subcommand)] #[derive(Subcommand)]
@ -32,7 +33,7 @@ pub enum GitCommand {
Clone { Clone {
#[arg()] #[arg()]
/// Name of the repo /// Name of the repo
name: String name: Option<String>
} }
} }
@ -71,22 +72,17 @@ pub async fn handle_git(cmd: &GitCommand) {
} }
GitCommand::Ls => { GitCommand::Ls => {
let ls = session.command("ls") let ls = list_repos(&session).await.join("\n");
.arg("store/")
.arg("|") print!("{ls}");
.arg("cat")
.output().await.unwrap();
eprintln!(
"{}",
String::from_utf8(ls.stdout).expect("Failed to list repos.")
);
} }
GitCommand::Add { name } => { add_remote_to_repo(name.to_string()); } 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"); println!("Added remote origin git@breadpi:/home/git/store/{name}.git");
} }
async fn list_repos(session: &Session) -> Vec<String> {
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<String> = s.split("\n").map(str::to_string).collect();
return list;
}
async fn clone_repo(name: &Option<String>, 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") let cmd = Command::new("git")
.arg("clone") .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"); .output().expect("Could not clone repo");
eprintln!( eprintln!(
@ -117,5 +138,5 @@ fn clone_repo(name: String) {
String::from_utf8(cmd.stdout).expect("Failed to clone repo.") 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}.");
} }