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