add command to list features

Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
Jason Volk 2024-07-24 23:53:48 +00:00
parent 8bb69eb81d
commit 4e975887cf
2 changed files with 61 additions and 1 deletions

View file

@ -1,4 +1,6 @@
use conduit::{utils::time, warn, Err, Result};
use std::fmt::Write;
use conduit::{info, utils::time, warn, Err, Result};
use ruma::events::room::message::RoomMessageEventContent;
use crate::services;
@ -19,6 +21,47 @@ pub(super) async fn show_config(_body: Vec<&str>) -> Result<RoomMessageEventCont
Ok(RoomMessageEventContent::text_plain(format!("{}", services().globals.config)))
}
pub(super) async fn list_features(
_body: Vec<&str>, available: bool, enabled: bool, comma: bool,
) -> Result<RoomMessageEventContent> {
let delim = if comma {
","
} else {
" "
};
if enabled && !available {
let features = info::rustc::features().join(delim);
let out = format!("```\n{features}\n```");
return Ok(RoomMessageEventContent::text_markdown(out));
}
if available && !enabled {
let features = info::cargo::features().join(delim);
let out = format!("```\n{features}\n```");
return Ok(RoomMessageEventContent::text_markdown(out));
}
let mut features = String::new();
let enabled = info::rustc::features();
let available = info::cargo::features();
for feature in available {
let active = enabled.contains(&feature.as_str());
let emoji = if active {
""
} else {
""
};
let remark = if active {
"[enabled]"
} else {
""
};
writeln!(features, "{emoji} {feature} {remark}")?;
}
Ok(RoomMessageEventContent::text_markdown(features))
}
pub(super) async fn memory_usage(_body: Vec<&str>) -> Result<RoomMessageEventContent> {
let services_usage = services().memory_usage().await?;
let database_usage = services().db.db.memory_usage()?;

View file

@ -14,6 +14,18 @@ pub(super) enum ServerCommand {
/// - Show configuration values
ShowConfig,
/// - List the features built into the server
ListFeatures {
#[arg(short, long)]
available: bool,
#[arg(short, long)]
enabled: bool,
#[arg(short, long)]
comma: bool,
},
/// - Print database memory usage statistics
MemoryUsage,
@ -54,6 +66,11 @@ pub(super) async fn process(command: ServerCommand, body: Vec<&str>) -> Result<R
Ok(match command {
ServerCommand::Uptime => uptime(body).await?,
ServerCommand::ShowConfig => show_config(body).await?,
ServerCommand::ListFeatures {
available,
enabled,
comma,
} => list_features(body, available, enabled, comma).await?,
ServerCommand::MemoryUsage => memory_usage(body).await?,
ServerCommand::ClearCaches => clear_caches(body).await?,
ServerCommand::ListBackups => list_backups(body).await?,