use crate::cli::server::{KillArgs, ServerCommand};
use crate::conn::session::Session;
use crate::error::{Failure, Result};
use crate::output::Envelope;
use crate::serverinfo::{
capabilities, kill, locks, processes, queries, replica, status, variables, ServerSettings,
};
use tokio_util::sync::CancellationToken;
pub async fn execute(
command: &ServerCommand,
session: &mut Session,
settings: &ServerSettings,
token: &CancellationToken,
) -> Result<Envelope> {
let facts = session.facts().clone();
let connection = session.connection();
match command {
ServerCommand::Status(args) => status::run(connection, args, settings, token).await,
ServerCommand::Variables(args) => variables::run(connection, args, settings).await,
ServerCommand::Processes(args) => {
processes::run(connection, args, facts.connection_id, settings).await
}
ServerCommand::Replica => replica::run(connection, settings).await,
ServerCommand::Kill(args) => {
named_connection(args)?;
kill::run(connection, args, facts.connection_id, settings).await
}
ServerCommand::Capabilities => Ok(capabilities::run(&facts)),
ServerCommand::Queries(args) => {
offered("server queries", settings)?;
queries::run(connection, args, facts.database.as_deref(), settings).await
}
ServerCommand::Locks => {
offered("server locks", settings)?;
locks::run(connection, settings).await
}
}
}
pub fn offered(name: &'static str, settings: &ServerSettings) -> Result<()> {
if settings.observability_visible {
return Ok(());
}
Err(Failure::usage(
"usage.command_withdrawn",
format!("`{name}` is not offered by this host"),
)
.with_detail("detail", name)
.with_hint("set server.observability_visible to true to offer it again"))
}
fn named_connection(args: &KillArgs) -> Result<u64> {
if args.id == 0 {
return Err(Failure::usage(
"server.connection_missing",
"ending work needs the connection whose work is to end",
)
.with_hint(
"name one with --id, as in `server kill --id 42`; `server processes` lists them",
));
}
Ok(args.id)
}
#[cfg(test)]
mod tests {
use crate::cli::server::ServerCommand;
use crate::cli::Cli;
use clap::Parser;
fn parse(args: &[&str]) -> ServerCommand {
let command_line: Vec<String> = std::iter::once("mariadb-cli")
.chain(args.iter().copied())
.map(ToOwned::to_owned)
.collect();
match Cli::try_parse_from(command_line)
.expect("accepted form")
.command
{
crate::cli::Command::Server(command) => command,
other => panic!("expected a server command, got {other:?}"),
}
}
#[test]
fn the_interval_comes_from_the_caller_or_from_the_stored_setting() {
let ServerCommand::Status(args) = parse(&["server", "status", "--interval", "5"]) else {
panic!("expected status");
};
assert_eq!(args.interval, Some(5));
let ServerCommand::Status(args) = parse(&["server", "status"]) else {
panic!("expected status");
};
assert_eq!(args.interval, None);
}
#[test]
fn the_mark_for_another_users_work_is_off_unless_written() {
let ServerCommand::Kill(args) = parse(&["server", "kill", "--id", "4711"]) else {
panic!("expected kill");
};
assert!(!args.kill_other);
assert!(!args.query_only);
let ServerCommand::Kill(args) = parse(&[
"server",
"kill",
"--id",
"4711",
"--query-only",
"--kill-other",
]) else {
panic!("expected kill");
};
assert!(args.kill_other);
assert!(args.query_only);
}
#[test]
fn ending_work_without_a_connection_is_refused_with_a_code() {
let ServerCommand::Kill(args) = parse(&["server", "kill"]) else {
panic!("expected kill");
};
let failure = super::named_connection(&args).expect_err("must refuse");
assert_eq!(failure.code(), "server.connection_missing");
assert_eq!(failure.exit_number().code(), 2);
assert!(
failure.hint().is_some_and(|hint| hint.contains("--id")),
"the refusal names the flag that carries the connection"
);
let ServerCommand::Kill(named) = parse(&["server", "kill", "--id", "4711"]) else {
panic!("expected kill");
};
assert_eq!(super::named_connection(&named).expect("named"), 4711);
}
#[test]
fn the_observation_commands_are_always_on_the_surface() {
assert!(matches!(parse(&["server", "locks"]), ServerCommand::Locks));
assert!(matches!(
parse(&["server", "queries"]),
ServerCommand::Queries(_)
));
}
}