Updated initrepo

This commit is contained in:
breadone 2024-07-28 18:12:42 +12:00
parent 65e33a7c9f
commit a514ff72d2
No known key found for this signature in database
2 changed files with 102 additions and 52 deletions

View File

@ -1,6 +1,6 @@
use clap::Subcommand; use clap::Subcommand;
use openssh::{Session, KnownHosts}; use openssh::{Session, KnownHosts};
use inquire::{Select, InquireError}; use inquire::{Text, Select, Confirm, InquireError};
use core::str; use core::str;
use std::process::Command; use std::process::Command;
@ -10,7 +10,7 @@ pub enum GitCommand {
Init { Init {
#[arg()] #[arg()]
/// Name of the repo /// Name of the repo
name: String name: Option<String>
}, },
/// List all repositories in breadpi. /// List all repositories in breadpi.
@ -20,14 +20,14 @@ pub enum GitCommand {
Delete { Delete {
#[arg()] #[arg()]
/// Name of the repo /// Name of the repo
name: String name: Option<String>
}, },
/// Add remote to the git repo in the current directory /// Add remote to the git repo in the current directory
Add { Add {
#[arg()] #[arg()]
/// Name of the repo /// Name of the repo
name: String name: Option<String>
}, },
/// Clone a repo from the remote /// Clone a repo from the remote
@ -45,31 +45,11 @@ pub async fn handle_git(cmd: &GitCommand) {
match cmd { match cmd {
GitCommand::Init { name } => { GitCommand::Init { name } => {
let init = session.command("git") init_repo(name, &session).await;
.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 } => { GitCommand::Delete { name } => {
let rm = session.command("rm") rm_repo(name, &session).await;
.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::Ls => { GitCommand::Ls => {
@ -87,21 +67,6 @@ pub async fn handle_git(cmd: &GitCommand) {
} }
fn add_remote_to_repo(name: String) {
let cmd = Command::new("git")
.arg("remote")
.arg("add")
.arg("origin")
.arg("git@breadpi:/home/git/store/".to_owned() + &name + ".git")
.output().expect("Could not add remote");
eprintln!(
"{}",
String::from_utf8(cmd.stdout).expect("Failed to add git remote.")
);
println!("Added remote origin git@breadpi:/home/git/store/{name}.git");
}
async fn list_repos(session: &Session) -> Vec<String> { async fn list_repos(session: &Session) -> Vec<String> {
let buf = session.command("ls") let buf = session.command("ls")
.arg("store/") .arg("store/")
@ -127,20 +92,77 @@ async fn list_repos(session: &Session) -> Vec<String> {
return repos; return repos;
} }
async fn clone_repo(name: &Option<String>, session: &Session) {
let mut unwrapped_name: String = "".to_string(); async fn choose_repo_from_list(session: &Session) -> String {
let list = list_repos(session).await;
let repos = list.iter().map(|s| s as &str).collect();
let repo: Result<&str, InquireError> = Select::new("Choose a repo: ", repos).prompt();
match repo {
Ok(s) => return s.to_string(),
Err(e) => panic!("{e}")
}
}
fn add_remote_to_repo(name: String) {
let cmd = Command::new("git")
.arg("remote")
.arg("add")
.arg("origin")
.arg("git@breadpi:/home/git/store/".to_owned() + &name + ".git")
.output().expect("Could not add remote");
eprintln!(
"{}",
String::from_utf8(cmd.stdout).expect("Failed to add git remote.")
);
println!("Added remote origin git@breadpi:/home/git/store/{name}.git");
}
async fn init_repo(name: &Option<String>, session: &Session) {
let unwrapped_name: String;
// check if name is null and present list if so // check if name is null and present list if so
if name.is_none() { if name.is_none() {
let list = list_repos(session).await; let text = Text::new("Enter the repo name: ").prompt();
let repos = list.iter().map(|s| s as &str).collect();
let repo: Result<&str, InquireError> = Select::new("Choose a repo: ", repos).prompt(); match text {
Ok(s) => unwrapped_name = s,
match repo { Err(e) => panic!("{e}")
Ok(s) => unwrapped_name = s.to_string(),
Err(_) => println!("error!")
} }
} else {
unwrapped_name = name.clone().unwrap();
}
let init = session.command("git")
.arg("init")
.arg("--bare")
.arg("store/".to_owned() + &unwrapped_name + ".git")
.output().await.unwrap();
eprintln!(
"{}",
String::from_utf8(init.stdout).expect("Failed to init git repo.")
);
let add_repo = Confirm::new("Would you like to add the remote to the repo in this directory?").prompt();
match add_repo {
Ok(true) => add_remote_to_repo(unwrapped_name),
Ok(false) => {},
Err(e) => panic!("{e}")
}
}
async fn clone_repo(name: &Option<String>, session: &Session) {
let unwrapped_name: String;
// check if name is null and present list if so
if name.is_none() {
unwrapped_name = choose_repo_from_list(session).await;
} else { } else {
unwrapped_name = name.clone().unwrap(); unwrapped_name = name.clone().unwrap();
} }
@ -156,4 +178,26 @@ async fn clone_repo(name: &Option<String>, session: &Session) {
); );
println!("Cloned repo git@breadpi:/home/git/store/{unwrapped_name} to {unwrapped_name}."); println!("Cloned repo git@breadpi:/home/git/store/{unwrapped_name} to {unwrapped_name}.");
}
async fn rm_repo(name: &Option<String>, session: &Session) {
let unwrapped_name: String;
// check if name is null and present list if so
if name.is_none() {
unwrapped_name = choose_repo_from_list(session).await;
} else {
unwrapped_name = name.clone().unwrap();
}
let rm = session.command("rm")
.arg("-r")
.arg("-f")
.arg("store/".to_owned() + &unwrapped_name + ".git")
.output().await.unwrap();
eprintln!(
"{}",
String::from_utf8(rm.stdout).expect("Failed to rm repo.")
);
} }

View File

@ -4,7 +4,7 @@ use tokio;
use clap::{Parser, Subcommand}; use clap::{Parser, Subcommand};
use crate::git::{handle_git, GitCommand}; use crate::git::{handle_git, GitCommand};
const VERSION: &str = "2.0b1"; const VERSION: &str = "2.0b5";
#[derive(Subcommand)] #[derive(Subcommand)]
enum Command { enum Command {
@ -12,7 +12,9 @@ enum Command {
Git { Git {
#[command(subcommand)] #[command(subcommand)]
git_command: GitCommand git_command: GitCommand
} },
V
} }
@ -29,6 +31,10 @@ async fn main() {
match &cli.command { match &cli.command {
Command::Git { git_command } => { Command::Git { git_command } => {
handle_git(&git_command).await; handle_git(&git_command).await;
},
Command::V => {
println!("breadpi-client {VERSION}")
} }
} }
} }