Added sys subcommand
Add restart subsubcommand
This commit is contained in:
parent
5068420359
commit
d5b9431547
2
Cargo.lock
generated
2
Cargo.lock
generated
@ -100,7 +100,7 @@ checksum = "ed570934406eb16438a4e976b1b4500774099c13b8cb96eec99f620f05090ddf"
|
||||
|
||||
[[package]]
|
||||
name = "bp"
|
||||
version = "2.0.1"
|
||||
version = "2.1.0"
|
||||
dependencies = [
|
||||
"clap",
|
||||
"inquire",
|
||||
|
@ -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
|
||||
|
@ -83,11 +83,14 @@ 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();
|
||||
}).collect();
|
||||
|
||||
repos.truncate(repos.len() - 1);
|
||||
|
||||
return repos;
|
||||
}
|
||||
|
12
src/main.rs
12
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; }
|
||||
}
|
||||
}
|
||||
|
32
src/sys.rs
Normal file
32
src/sys.rs
Normal 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));
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user