2026-07-15 16:46:11 +01:00
|
|
|
//! `rebuild gateway` command parsing.
|
2026-03-22 19:07:07 +00:00
|
|
|
//!
|
2026-07-15 16:46:11 +01:00
|
|
|
//! The old `rebuild` command (in-container cargo build + restart of a sled)
|
|
|
|
|
//! was removed: fleet binary updates go through `release` + `upgrade all`
|
|
|
|
|
//! exclusively. Only the gateway self-rebuild command remains here.
|
2026-03-22 19:07:07 +00:00
|
|
|
|
2026-03-28 18:33:22 +00:00
|
|
|
use crate::chat::util::strip_bot_mention;
|
2026-03-22 19:07:07 +00:00
|
|
|
|
|
|
|
|
/// A parsed rebuild command.
|
|
|
|
|
#[derive(Debug, PartialEq)]
|
|
|
|
|
pub struct RebuildCommand;
|
|
|
|
|
|
2026-05-19 18:07:59 +00:00
|
|
|
/// Parse a "rebuild gateway" command from a raw message body.
|
|
|
|
|
///
|
|
|
|
|
/// Returns `Some(RebuildCommand)` only when the stripped message begins with
|
2026-07-15 16:46:11 +01:00
|
|
|
/// "rebuild gateway" (case-insensitive).
|
2026-05-19 18:07:59 +00:00
|
|
|
pub fn extract_rebuild_gateway_command(
|
|
|
|
|
message: &str,
|
|
|
|
|
bot_name: &str,
|
|
|
|
|
bot_user_id: &str,
|
|
|
|
|
) -> Option<RebuildCommand> {
|
|
|
|
|
let stripped = strip_bot_mention(message, bot_name, bot_user_id);
|
|
|
|
|
let trimmed = stripped
|
|
|
|
|
.trim()
|
|
|
|
|
.trim_start_matches(|c: char| !c.is_alphanumeric());
|
|
|
|
|
|
|
|
|
|
let (cmd, rest) = trimmed.split_once(char::is_whitespace)?;
|
|
|
|
|
|
|
|
|
|
if !cmd.eq_ignore_ascii_case("rebuild") {
|
|
|
|
|
return None;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let qualifier = rest
|
|
|
|
|
.trim()
|
|
|
|
|
.trim_start_matches(|c: char| !c.is_alphanumeric());
|
|
|
|
|
let first_word = match qualifier.split_once(char::is_whitespace) {
|
|
|
|
|
Some((w, _)) => w,
|
|
|
|
|
None => qualifier,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if first_word.eq_ignore_ascii_case("gateway") {
|
|
|
|
|
Some(RebuildCommand)
|
|
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-22 19:07:07 +00:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
// Tests
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests {
|
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
|
|
#[test]
|
2026-07-15 16:46:11 +01:00
|
|
|
fn extract_gateway_rebuild() {
|
2026-04-13 14:07:08 +00:00
|
|
|
let cmd =
|
2026-07-15 16:46:11 +01:00
|
|
|
extract_rebuild_gateway_command("Timmy rebuild gateway", "Timmy", "@timmy:home.local");
|
2026-03-22 19:07:07 +00:00
|
|
|
assert_eq!(cmd, Some(RebuildCommand));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
2026-07-15 16:46:11 +01:00
|
|
|
fn plain_rebuild_is_not_gateway_rebuild() {
|
|
|
|
|
let cmd = extract_rebuild_gateway_command("Timmy rebuild", "Timmy", "@timmy:home.local");
|
2026-03-22 19:07:07 +00:00
|
|
|
assert_eq!(cmd, None);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
2026-07-15 16:46:11 +01:00
|
|
|
fn non_rebuild_returns_none() {
|
|
|
|
|
let cmd = extract_rebuild_gateway_command("Timmy status", "Timmy", "@timmy:home.local");
|
2026-03-22 19:07:07 +00:00
|
|
|
assert_eq!(cmd, None);
|
|
|
|
|
}
|
|
|
|
|
}
|