From d5b943154766cdd93b18ffe8e6fd25306e6182c9 Mon Sep 17 00:00:00 2001 From: breadone Date: Tue, 30 Jul 2024 11:18:54 +1200 Subject: [PATCH] Added sys subcommand Add restart subsubcommand --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/git.rs | 9 ++++++--- src/main.rs | 12 +++++++++--- src/sys.rs | 32 ++++++++++++++++++++++++++++++++ 5 files changed, 49 insertions(+), 8 deletions(-) create mode 100644 src/sys.rs diff --git a/Cargo.lock b/Cargo.lock index 03bf78c..39aea0d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -100,7 +100,7 @@ checksum = "ed570934406eb16438a4e976b1b4500774099c13b8cb96eec99f620f05090ddf" [[package]] name = "bp" -version = "2.0.1" +version = "2.1.0" dependencies = [ "clap", "inquire", diff --git a/Cargo.toml b/Cargo.toml index c14cdb7..3627630 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "bp" -version = "2.0.1" +version = "2.1.0" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/src/git.rs b/src/git.rs index 0403685..43a3b97 100644 --- a/src/git.rs +++ b/src/git.rs @@ -83,13 +83,16 @@ async fn list_repos(session: &Session) -> Vec { // split string into array let list: Vec = s.split("\n").map(str::to_string).collect(); - let repos: Vec = list.iter().map(|x| { + // remove the trailing .git from each item + let mut repos: Vec = list.iter().map(|x| { let mut chars = x.chars(); for _ in 0..4 { chars.next_back(); } return chars.as_str().to_string(); - } ).collect(); + }).collect(); + + repos.truncate(repos.len() - 1); - return repos; + return repos; } diff --git a/src/main.rs b/src/main.rs index de8e29a..db65de8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,7 @@ mod git; +mod sys; +use sys::{SysCommand, handle_sys}; use tokio; use clap::{Parser, Subcommand}; use crate::git::{handle_git, GitCommand}; @@ -10,6 +12,11 @@ enum Command { Git { #[command(subcommand)] git_command: GitCommand + }, + + Sys { + #[command(subcommand)] + sys_command: SysCommand } } @@ -26,8 +33,7 @@ async fn main() { let cli = Cli::parse(); match &cli.command { - Command::Git { git_command } => { - handle_git(&git_command).await; - }, + Command::Git { git_command } => { handle_git(&git_command).await; }, + Command::Sys { sys_command } => { handle_sys(&sys_command).await; } } } diff --git a/src/sys.rs b/src/sys.rs new file mode 100644 index 0000000..0747d25 --- /dev/null +++ b/src/sys.rs @@ -0,0 +1,32 @@ +use clap::Subcommand; +use openssh::{Session, KnownHosts}; +use core::str; + +#[derive(Subcommand)] +pub enum SysCommand { + /// Reboot breadpi. + Reboot, +} + +pub async fn handle_sys(cmd: &SysCommand) { + let session = Session::connect("bread@breadpi", KnownHosts::Strict) + .await + .expect("Could not connect to breadpi"); + + match cmd { + SysCommand::Reboot => { sys_reboot(&session).await; } + } + + let _ = session.close(); +} + +async fn sys_reboot(session: &Session) { + let cmd = session.command("sudo") + .arg("systemctl") + .arg("reboot") + .output().await.unwrap(); + if !cmd.status.success() { + println!("Error rebooting! Try again, possibly directly through ssh..."); + println!("Error: {:?}", String::from_utf8(cmd.stderr)); + } +} \ No newline at end of file