Add appservice show command to show config

This commit is contained in:
tezlm 2023-10-03 17:39:40 -07:00 committed by strawberry
parent 562eaa1dea
commit 6fdeec1108

View file

@ -87,6 +87,7 @@ enum AppserviceCommand {
/// # ``` /// # ```
Register, Register,
#[command(verbatim_doc_comment)]
/// Unregister an appservice using its ID /// Unregister an appservice using its ID
/// ///
/// You can find the ID using the `list-appservices` command. /// You can find the ID using the `list-appservices` command.
@ -95,6 +96,15 @@ enum AppserviceCommand {
appservice_identifier: String, 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 all the currently registered appservices
List, List,
} }
@ -434,6 +444,29 @@ impl Service {
"Failed to unregister appservice: {e}" "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 => { AppserviceCommand::List => {
if let Ok(appservices) = services() if let Ok(appservices) = services()
.appservice .appservice
@ -917,7 +950,7 @@ impl Service {
let text = text.replace("subcommand", "command"); let text = text.replace("subcommand", "command");
// Escape option names (e.g. `<element-id>`) since they look like HTML tags // 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) // Italicize the first line (command name and version text)
let re = Regex::new("^(.*?)\n").expect("Regex compilation should not fail"); let re = Regex::new("^(.*?)\n").expect("Regex compilation should not fail");
@ -1353,6 +1386,10 @@ impl Service {
} }
} }
fn escape_html(s: &str) -> String {
s.replace('&', "&amp;").replace('<', "&lt;").replace('>', "&gt;")
}
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use super::*; use super::*;