Add appservice show command to show config

This commit is contained in:
tezlm 2023-10-03 17:39:40 -07:00
parent b20f4d6305
commit 99fb0a2953
No known key found for this signature in database
GPG key ID: 649733FCD94AFBBA

View file

@ -87,6 +87,7 @@ enum AppserviceCommand {
/// # ```
Register,
#[command(verbatim_doc_comment)]
/// Unregister an appservice using its ID
///
/// You can find the ID using the `list-appservices` command.
@ -95,6 +96,15 @@ enum AppserviceCommand {
appservice_identifier: String,
},
#[command(verbatim_doc_comment)]
/// Show an appservice's config using its ID
///
/// You can find the ID using the `list-appservices` command.
Show {
/// The appservice to show
appservice_identifier: String,
},
/// List all the currently registered appservices
List,
}
@ -438,6 +448,29 @@ impl Service {
"Failed to unregister appservice: {e}"
)),
},
AppserviceCommand::Show { appservice_identifier } => {
match services()
.appservice
.get_registration(&appservice_identifier) {
Ok(Some(config)) => {
let config_str = serde_yaml::to_string(&config)
.expect("config should've been validated on register");
let output = format!(
"Config for {}:\n\n```yaml\n{}\n```",
appservice_identifier,
config_str,
);
let output_html = format!(
"Config for {}:\n\n<pre><code class=\"language-yaml\">{}</code></pre>",
escape_html(&appservice_identifier),
escape_html(&config_str),
);
RoomMessageEventContent::text_html(output, output_html)
},
Ok(None) => RoomMessageEventContent::text_plain("Appservice does not exist."),
Err(_) => RoomMessageEventContent::text_plain("Failed to get appservice."),
}
}
AppserviceCommand::List => {
if let Ok(appservices) = services()
.appservice
@ -903,7 +936,7 @@ impl Service {
let text = text.replace("subcommand", "command");
// Escape option names (e.g. `<element-id>`) since they look like HTML tags
let text = text.replace('<', "&lt;").replace('>', "&gt;");
let text = escape_html(&text);
// Italicize the first line (command name and version text)
let re = Regex::new("^(.*?)\n").expect("Regex compilation should not fail");
@ -1289,6 +1322,10 @@ impl Service {
}
}
fn escape_html(s: &str) -> String {
s.replace('&', "&amp;").replace('<', "&lt;").replace('>', "&gt;")
}
#[cfg(test)]
mod test {
use super::*;