Implemented new list function
This commit is contained in:
parent
ae1a08fe57
commit
a872e4681d
3
Makefile
3
Makefile
@ -2,6 +2,9 @@
|
||||
build:
|
||||
@cargo build
|
||||
|
||||
run: build
|
||||
@./target/debug/bp
|
||||
|
||||
clean:
|
||||
@rm -rf target
|
||||
|
||||
|
49
src/git.rs
49
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<String>
|
||||
}
|
||||
}
|
||||
|
||||
@ -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();
|
||||
let ls = list_repos(&session).await.join("\n");
|
||||
|
||||
eprintln!(
|
||||
"{}",
|
||||
String::from_utf8(ls.stdout).expect("Failed to list repos.")
|
||||
);
|
||||
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<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")
|
||||
.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}.");
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user