Added sys subcommand

Add restart subsubcommand
This commit is contained in:
breadone 2024-07-30 11:18:54 +12:00
parent 5068420359
commit d5b9431547
No known key found for this signature in database
5 changed files with 49 additions and 8 deletions

2
Cargo.lock generated
View File

@ -100,7 +100,7 @@ checksum = "ed570934406eb16438a4e976b1b4500774099c13b8cb96eec99f620f05090ddf"
[[package]]
name = "bp"
version = "2.0.1"
version = "2.1.0"
dependencies = [
"clap",
"inquire",

View File

@ -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

View File

@ -83,12 +83,15 @@ async fn list_repos(session: &Session) -> Vec<String> {
// split string into array
let list: Vec<String> = s.split("\n").map(str::to_string).collect();
let repos: Vec<String> = list.iter().map(|x| {
// remove the trailing .git from each item
let mut repos: Vec<String> = list.iter().map(|x| {
let mut chars = x.chars();
for _ in 0..4 { chars.next_back(); }
return chars.as_str().to_string();
}).collect();
repos.truncate(repos.len() - 1);
return repos;
}

View File

@ -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; }
}
}

32
src/sys.rs Normal file
View File

@ -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));
}
}