pub mod config;
pub mod conn;
pub mod data;
pub mod globals;
pub mod history;
pub mod meta;
pub mod query;
pub mod schema;
pub mod server;
use clap::{Parser, Subcommand};
#[derive(Debug, Parser)]
#[command(
name = "mariadb-cli",
version,
about = "One-shot agent-native command line client for MariaDB servers",
long_about = None,
propagate_version = true,
disable_help_subcommand = true,
)]
pub struct Cli {
#[command(flatten)]
pub globals: globals::GlobalFlags,
#[command(subcommand)]
pub command: Command,
}
#[derive(Debug, Clone, Subcommand)]
pub enum Command {
#[command(subcommand)]
Conn(conn::ConnCommand),
#[command(subcommand)]
Schema(schema::SchemaCommand),
#[command(subcommand)]
Query(query::QueryCommand),
#[command(subcommand)]
Data(data::DataCommand),
#[command(subcommand)]
Server(server::ServerCommand),
#[command(subcommand)]
History(history::HistoryCommand),
#[command(subcommand)]
Config(config::ConfigCommand),
#[command(subcommand)]
Meta(meta::MetaCommand),
}
impl Command {
#[must_use]
pub const fn published_name(&self) -> &'static str {
match self {
Self::Conn(inner) => inner.published_name(),
Self::Schema(inner) => inner.published_name(),
Self::Query(inner) => inner.published_name(),
Self::Data(inner) => inner.published_name(),
Self::Server(inner) => inner.published_name(),
Self::History(inner) => inner.published_name(),
Self::Config(inner) => inner.published_name(),
Self::Meta(inner) => inner.published_name(),
}
}
}
impl conn::ConnCommand {
#[must_use]
pub const fn published_name(&self) -> &'static str {
match self {
Self::Add(_) => "conn add",
Self::List => "conn list",
Self::Show(_) => "conn show",
Self::Edit(_) => "conn edit",
Self::Remove(_) => "conn remove",
Self::Test(_) => "conn test",
Self::Default(_) => "conn default",
}
}
}
impl schema::SchemaCommand {
#[must_use]
pub const fn published_name(&self) -> &'static str {
match self {
Self::Map(_) => "schema map",
Self::Databases => "schema databases",
Self::Tables(_) => "schema tables",
Self::Columns(_) => "schema columns",
Self::Keys(_) => "schema keys",
Self::Indexes(_) => "schema indexes",
Self::Checks(_) => "schema checks",
Self::Views(_) => "schema views",
Self::Routines(_) => "schema routines",
Self::Triggers(_) => "schema triggers",
Self::Events(_) => "schema events",
Self::Sequences(_) => "schema sequences",
Self::Partitions(_) => "schema partitions",
Self::Search(_) => "schema search",
Self::Stats(_) => "schema stats",
Self::Ddl(_) => "schema ddl",
Self::Count(_) => "schema count",
Self::Size(_) => "schema size",
Self::Grants => "schema grants",
}
}
}
impl query::QueryCommand {
#[must_use]
pub const fn published_name(&self) -> &'static str {
match self {
Self::Run(_) => "query run",
Self::Sample(_) => "query sample",
Self::Explain(_) => "query explain",
Self::Analyze(_) => "query analyze",
Self::Plan(_) => "query plan",
Self::Lineage(_) => "query lineage",
Self::File(_) => "query file",
Self::Tx(_) => "query tx",
Self::Call(_) => "query call",
Self::Named(args) => args.action.published_name(),
}
}
}
impl data::DataCommand {
#[must_use]
pub const fn published_name(&self) -> &'static str {
match self {
Self::Export(_) => "data export",
Self::Import(_) => "data import",
}
}
}
impl server::ServerCommand {
#[must_use]
pub const fn published_name(&self) -> &'static str {
match self {
Self::Status(_) => "server status",
Self::Variables(_) => "server variables",
Self::Processes(_) => "server processes",
Self::Replica => "server replica",
Self::Kill(_) => "server kill",
Self::Capabilities => "server capabilities",
Self::Queries(_) => "server queries",
Self::Locks => "server locks",
}
}
}
impl history::HistoryCommand {
#[must_use]
pub const fn published_name(&self) -> &'static str {
match self {
Self::AsOf(_) => "history as-of",
Self::Between(_) => "history between",
Self::Tables(_) => "history tables",
}
}
}
impl config::ConfigCommand {
#[must_use]
pub const fn published_name(&self) -> &'static str {
match self {
Self::Set(_) => "config set",
Self::Get(_) => "config get",
Self::List(_) => "config list",
Self::Path => "config path",
Self::Unset(_) => "config unset",
Self::Doctor => "config doctor",
}
}
}
impl meta::MetaCommand {
#[must_use]
pub const fn published_name(&self) -> &'static str {
match self {
Self::Commands(_) => "meta commands",
Self::Schema(_) => "meta schema",
Self::Version => "meta version",
Self::Manual(_) => "meta manual",
Self::Completions(_) => "meta completions",
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use clap::CommandFactory;
#[test]
fn the_command_tree_is_well_formed() {
Cli::command().debug_assert();
}
#[test]
fn the_surface_has_the_published_command_count() {
fn leaves(command: &clap::Command) -> usize {
let mut total = 0;
for sub in command.get_subcommands() {
if sub.get_subcommands().next().is_some() {
total += leaves(sub);
} else {
total += 1;
}
}
total
}
const NAMED_EXTRA_LEAVES: usize = 3;
assert_eq!(leaves(&Cli::command()) - NAMED_EXTRA_LEAVES, 60);
}
}