Added mono service commands for all service actions

+ Made all commands ephemeral
main
Joey Hines 2024-03-10 14:29:47 -06:00
parent c529e6b2ac
commit c380e71dda
Signed by: joeyahines
GPG Key ID: 995E531F7A569DDB
1 changed files with 39 additions and 39 deletions

View File

@ -3,8 +3,8 @@ use crate::models::service::{Service, ServiceGroup};
use j_db::database::Database; use j_db::database::Database;
use j_db::model::JdbModel; use j_db::model::JdbModel;
use log::info; use log::info;
use poise::serenity_prelude as serenity;
use poise::serenity_prelude::MessageBuilder; use poise::serenity_prelude::MessageBuilder;
use poise::{serenity_prelude as serenity, ChoiceParameter};
#[allow(dead_code)] #[allow(dead_code)]
struct Data { struct Data {
@ -14,7 +14,7 @@ struct Data {
type Error = Box<dyn std::error::Error + Send + Sync>; type Error = Box<dyn std::error::Error + Send + Sync>;
type Context<'a> = poise::Context<'a, Data, Error>; type Context<'a> = poise::Context<'a, Data, Error>;
#[poise::command(slash_command)] #[poise::command(slash_command, ephemeral)]
async fn add_service( async fn add_service(
ctx: Context<'_>, ctx: Context<'_>,
#[description = "Service unit name to add"] service_name: String, #[description = "Service unit name to add"] service_name: String,
@ -41,7 +41,7 @@ async fn add_service(
Ok(()) Ok(())
} }
#[poise::command(slash_command)] #[poise::command(slash_command, ephemeral)]
async fn add_service_group( async fn add_service_group(
ctx: Context<'_>, ctx: Context<'_>,
#[description = "New group name"] group_name: String, #[description = "New group name"] group_name: String,
@ -54,7 +54,7 @@ async fn add_service_group(
Ok(()) Ok(())
} }
#[poise::command(slash_command)] #[poise::command(slash_command, ephemeral)]
async fn add_service_to_group( async fn add_service_to_group(
ctx: Context<'_>, ctx: Context<'_>,
#[description = "Service name"] service_name: String, #[description = "Service name"] service_name: String,
@ -83,7 +83,7 @@ async fn add_service_to_group(
Ok(()) Ok(())
} }
#[poise::command(slash_command)] #[poise::command(slash_command, ephemeral)]
async fn list_services( async fn list_services(
ctx: Context<'_>, ctx: Context<'_>,
#[description = "Service group name"] group: Option<String>, #[description = "Service group name"] group: Option<String>,
@ -131,47 +131,48 @@ async fn list_services(
Ok(()) Ok(())
} }
#[poise::command(slash_command)] #[derive(Debug, Clone, ChoiceParameter)]
async fn restart_service( pub enum ServiceAction {
ctx: Context<'_>, Start,
#[description = "Service unit name"] service_name: String, Stop,
) -> Result<(), Error> { Restart,
let service: Option<Service> = ctx Status,
.data()
.db
.filter(|_, s: &Service| s.name.contains(&service_name))?
.next();
if let Some(service) = service {
systemctl::restart(&service.name)?;
ctx.reply(format!("`{}` has been restarted", service_name))
.await?;
} else {
ctx.reply(format!("Unknown service `{}`", service_name))
.await?;
}
Ok(())
} }
#[poise::command(slash_command)] #[poise::command(slash_command, ephemeral)]
async fn service_status( async fn service(
ctx: Context<'_>, ctx: Context<'_>,
#[description = "Action to preform on service"] action: ServiceAction,
#[description = "Service unit name"] service_name: String, #[description = "Service unit name"] service_name: String,
) -> Result<(), Error> { ) -> Result<(), Error> {
let service: Option<Service> = ctx let service: Option<Service> = Service::find_service_by_name(&ctx.data().db, &service_name)?;
.data()
.db
.filter(|_, s: &Service| s.name.contains(&service_name))?
.next();
if let Some(service) = service { if let Some(service) = service {
let status = systemctl::status(&service.name)?; match action {
let mut msg = MessageBuilder::new(); ServiceAction::Start => {
systemctl::start(&service_name)?;
ctx.reply(format!("`{}` has been started", service_name))
.await?;
}
ServiceAction::Stop => {
systemctl::stop(&service_name)?;
ctx.reply(format!("`{}` has been stopped", service_name))
.await?;
}
ServiceAction::Restart => {
systemctl::stop(&service_name)?;
ctx.reply(format!("`{}` has been restarted", service_name))
.await?;
}
ServiceAction::Status => {
let status = systemctl::status(&service.name)?;
let mut msg = MessageBuilder::new();
msg.push_codeblock_safe(status, None); msg.push_codeblock_safe(status, None);
ctx.reply(msg.build()).await?; ctx.reply(msg.build()).await?;
}
}
} else { } else {
ctx.reply(format!("Unknown service `{}`", service_name)) ctx.reply(format!("Unknown service `{}`", service_name))
.await?; .await?;
@ -194,9 +195,8 @@ pub async fn run_bot(db: Database, config: DaemonConfig) {
list_services(), list_services(),
add_service(), add_service(),
add_service_group(), add_service_group(),
restart_service(),
service_status(),
add_service_to_group(), add_service_to_group(),
service(),
], ],
..Default::default() ..Default::default()
}) })