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]]
|
[[package]]
|
||||||
name = "bp"
|
name = "bp"
|
||||||
version = "2.0.1"
|
version = "2.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"clap",
|
"clap",
|
||||||
"inquire",
|
"inquire",
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "bp"
|
name = "bp"
|
||||||
version = "2.0.1"
|
version = "2.1.0"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
@ -83,13 +83,16 @@ async fn list_repos(session: &Session) -> Vec<String> {
|
|||||||
// split string into array
|
// split string into array
|
||||||
let list: Vec<String> = s.split("\n").map(str::to_string).collect();
|
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();
|
let mut chars = x.chars();
|
||||||
for _ in 0..4 { chars.next_back(); }
|
for _ in 0..4 { chars.next_back(); }
|
||||||
return chars.as_str().to_string();
|
return chars.as_str().to_string();
|
||||||
} ).collect();
|
}).collect();
|
||||||
|
|
||||||
|
repos.truncate(repos.len() - 1);
|
||||||
|
|
||||||
return repos;
|
return repos;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
12
src/main.rs
12
src/main.rs
@ -1,5 +1,7 @@
|
|||||||
mod git;
|
mod git;
|
||||||
|
mod sys;
|
||||||
|
|
||||||
|
use sys::{SysCommand, handle_sys};
|
||||||
use tokio;
|
use tokio;
|
||||||
use clap::{Parser, Subcommand};
|
use clap::{Parser, Subcommand};
|
||||||
use crate::git::{handle_git, GitCommand};
|
use crate::git::{handle_git, GitCommand};
|
||||||
@ -10,6 +12,11 @@ enum Command {
|
|||||||
Git {
|
Git {
|
||||||
#[command(subcommand)]
|
#[command(subcommand)]
|
||||||
git_command: GitCommand
|
git_command: GitCommand
|
||||||
|
},
|
||||||
|
|
||||||
|
Sys {
|
||||||
|
#[command(subcommand)]
|
||||||
|
sys_command: SysCommand
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -26,8 +33,7 @@ async fn main() {
|
|||||||
let cli = Cli::parse();
|
let cli = Cli::parse();
|
||||||
|
|
||||||
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::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