Updated initrepo
This commit is contained in:
parent
65e33a7c9f
commit
a514ff72d2
144
src/git.rs
144
src/git.rs
@ -1,6 +1,6 @@
|
||||
use clap::Subcommand;
|
||||
use openssh::{Session, KnownHosts};
|
||||
use inquire::{Select, InquireError};
|
||||
use inquire::{Text, Select, Confirm, InquireError};
|
||||
use core::str;
|
||||
use std::process::Command;
|
||||
|
||||
@ -10,7 +10,7 @@ pub enum GitCommand {
|
||||
Init {
|
||||
#[arg()]
|
||||
/// Name of the repo
|
||||
name: String
|
||||
name: Option<String>
|
||||
},
|
||||
|
||||
/// List all repositories in breadpi.
|
||||
@ -20,14 +20,14 @@ pub enum GitCommand {
|
||||
Delete {
|
||||
#[arg()]
|
||||
/// Name of the repo
|
||||
name: String
|
||||
name: Option<String>
|
||||
},
|
||||
|
||||
/// Add remote to the git repo in the current directory
|
||||
Add {
|
||||
#[arg()]
|
||||
/// Name of the repo
|
||||
name: String
|
||||
name: Option<String>
|
||||
},
|
||||
|
||||
/// Clone a repo from the remote
|
||||
@ -45,31 +45,11 @@ pub async fn handle_git(cmd: &GitCommand) {
|
||||
|
||||
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): ");
|
||||
init_repo(name, &session).await;
|
||||
}
|
||||
|
||||
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.")
|
||||
);
|
||||
rm_repo(name, &session).await;
|
||||
}
|
||||
|
||||
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> {
|
||||
let buf = session.command("ls")
|
||||
.arg("store/")
|
||||
@ -127,20 +92,77 @@ async fn list_repos(session: &Session) -> Vec<String> {
|
||||
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
|
||||
if name.is_none() {
|
||||
let list = list_repos(session).await;
|
||||
let repos = list.iter().map(|s| s as &str).collect();
|
||||
let text = Text::new("Enter the repo name: ").prompt();
|
||||
|
||||
let repo: Result<&str, InquireError> = Select::new("Choose a repo: ", repos).prompt();
|
||||
|
||||
match repo {
|
||||
Ok(s) => unwrapped_name = s.to_string(),
|
||||
Err(_) => println!("error!")
|
||||
match text {
|
||||
Ok(s) => unwrapped_name = s,
|
||||
Err(e) => panic!("{e}")
|
||||
}
|
||||
|
||||
} 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 {
|
||||
unwrapped_name = name.clone().unwrap();
|
||||
}
|
||||
@ -157,3 +179,25 @@ async fn clone_repo(name: &Option<String>, session: &Session) {
|
||||
|
||||
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.")
|
||||
);
|
||||
}
|
10
src/main.rs
10
src/main.rs
@ -4,7 +4,7 @@ use tokio;
|
||||
use clap::{Parser, Subcommand};
|
||||
use crate::git::{handle_git, GitCommand};
|
||||
|
||||
const VERSION: &str = "2.0b1";
|
||||
const VERSION: &str = "2.0b5";
|
||||
|
||||
#[derive(Subcommand)]
|
||||
enum Command {
|
||||
@ -12,7 +12,9 @@ enum Command {
|
||||
Git {
|
||||
#[command(subcommand)]
|
||||
git_command: GitCommand
|
||||
}
|
||||
},
|
||||
|
||||
V
|
||||
}
|
||||
|
||||
|
||||
@ -29,6 +31,10 @@ async fn main() {
|
||||
match &cli.command {
|
||||
Command::Git { git_command } => {
|
||||
handle_git(&git_command).await;
|
||||
},
|
||||
|
||||
Command::V => {
|
||||
println!("breadpi-client {VERSION}")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user