Skip to main content

mariadb_cli/cli/
mod.rs

1//! The published command surface.
2//!
3//! Every command a caller may run appears here, and nothing appears here that
4//! the caller may not run. `meta commands` walks this tree, so the published
5//! surface and the real surface cannot drift apart.
6
7pub mod config;
8pub mod conn;
9pub mod data;
10pub mod globals;
11pub mod history;
12pub mod meta;
13pub mod query;
14pub mod schema;
15pub mod server;
16
17use clap::{Parser, Subcommand};
18
19/// One-shot client for MariaDB servers.
20///
21/// The process is born, does its work and dies within a single invocation.
22#[derive(Debug, Parser)]
23#[command(
24    name = "mariadb-cli",
25    version,
26    about = "One-shot agent-native command line client for MariaDB servers",
27    long_about = None,
28    propagate_version = true,
29    disable_help_subcommand = true,
30)]
31pub struct Cli {
32    /// The marks that mean the same thing on every command.
33    #[command(flatten)]
34    pub globals: globals::GlobalFlags,
35
36    /// The one command this invocation runs.
37    #[command(subcommand)]
38    pub command: Command,
39}
40
41/// The eight command families.
42#[derive(Debug, Clone, Subcommand)]
43pub enum Command {
44    /// Connection profiles.
45    #[command(subcommand)]
46    Conn(conn::ConnCommand),
47
48    /// Structure of the database.
49    #[command(subcommand)]
50    Schema(schema::SchemaCommand),
51
52    /// Reading and changing.
53    #[command(subcommand)]
54    Query(query::QueryCommand),
55
56    /// Moving data in and out.
57    #[command(subcommand)]
58    Data(data::DataCommand),
59
60    /// What the server is doing.
61    #[command(subcommand)]
62    Server(server::ServerCommand),
63
64    /// Version history of tables that keep one.
65    #[command(subcommand)]
66    History(history::HistoryCommand),
67
68    /// Stored configuration.
69    #[command(subcommand)]
70    Config(config::ConfigCommand),
71
72    /// Self-description.
73    #[command(subcommand)]
74    Meta(meta::MetaCommand),
75}
76
77impl Command {
78    /// The published name of this command, as it appears in the surface map.
79    ///
80    /// This is the identifier the output envelope carries and the one
81    /// `meta schema --command` accepts, so both always agree with the tree.
82    #[must_use]
83    pub const fn published_name(&self) -> &'static str {
84        match self {
85            Self::Conn(inner) => inner.published_name(),
86            Self::Schema(inner) => inner.published_name(),
87            Self::Query(inner) => inner.published_name(),
88            Self::Data(inner) => inner.published_name(),
89            Self::Server(inner) => inner.published_name(),
90            Self::History(inner) => inner.published_name(),
91            Self::Config(inner) => inner.published_name(),
92            Self::Meta(inner) => inner.published_name(),
93        }
94    }
95}
96
97impl conn::ConnCommand {
98    /// The published name of this profile command.
99    #[must_use]
100    pub const fn published_name(&self) -> &'static str {
101        match self {
102            Self::Add(_) => "conn add",
103            Self::List => "conn list",
104            Self::Show(_) => "conn show",
105            Self::Edit(_) => "conn edit",
106            Self::Remove(_) => "conn remove",
107            Self::Test(_) => "conn test",
108            Self::Default(_) => "conn default",
109        }
110    }
111}
112
113impl schema::SchemaCommand {
114    /// The published name of this structure command.
115    #[must_use]
116    pub const fn published_name(&self) -> &'static str {
117        match self {
118            Self::Map(_) => "schema map",
119            Self::Databases => "schema databases",
120            Self::Tables(_) => "schema tables",
121            Self::Columns(_) => "schema columns",
122            Self::Keys(_) => "schema keys",
123            Self::Indexes(_) => "schema indexes",
124            Self::Checks(_) => "schema checks",
125            Self::Views(_) => "schema views",
126            Self::Routines(_) => "schema routines",
127            Self::Triggers(_) => "schema triggers",
128            Self::Events(_) => "schema events",
129            Self::Sequences(_) => "schema sequences",
130            Self::Partitions(_) => "schema partitions",
131            Self::Search(_) => "schema search",
132            Self::Stats(_) => "schema stats",
133            Self::Ddl(_) => "schema ddl",
134            Self::Count(_) => "schema count",
135            Self::Size(_) => "schema size",
136            Self::Grants => "schema grants",
137        }
138    }
139}
140
141impl query::QueryCommand {
142    /// The published name of this query command.
143    #[must_use]
144    pub const fn published_name(&self) -> &'static str {
145        match self {
146            Self::Run(_) => "query run",
147            Self::Sample(_) => "query sample",
148            Self::Explain(_) => "query explain",
149            Self::Analyze(_) => "query analyze",
150            Self::Plan(_) => "query plan",
151            Self::Lineage(_) => "query lineage",
152            Self::File(_) => "query file",
153            Self::Tx(_) => "query tx",
154            Self::Call(_) => "query call",
155            // Named by its action, not by its family. The four actions are
156            // four published commands, so answering all four with the contract
157            // of `query named` told a caller about a command it had not asked
158            // about, and anything branching on the contract's `command` field
159            // branched on the wrong name.
160            Self::Named(args) => args.action.published_name(),
161        }
162    }
163}
164
165impl data::DataCommand {
166    /// The published name of this data command.
167    #[must_use]
168    pub const fn published_name(&self) -> &'static str {
169        match self {
170            Self::Export(_) => "data export",
171            Self::Import(_) => "data import",
172        }
173    }
174}
175
176impl server::ServerCommand {
177    /// The published name of this server command.
178    #[must_use]
179    pub const fn published_name(&self) -> &'static str {
180        match self {
181            Self::Status(_) => "server status",
182            Self::Variables(_) => "server variables",
183            Self::Processes(_) => "server processes",
184            Self::Replica => "server replica",
185            Self::Kill(_) => "server kill",
186            Self::Capabilities => "server capabilities",
187            Self::Queries(_) => "server queries",
188            Self::Locks => "server locks",
189        }
190    }
191}
192
193impl history::HistoryCommand {
194    /// The published name of this history command.
195    #[must_use]
196    pub const fn published_name(&self) -> &'static str {
197        match self {
198            Self::AsOf(_) => "history as-of",
199            Self::Between(_) => "history between",
200            Self::Tables(_) => "history tables",
201        }
202    }
203}
204
205impl config::ConfigCommand {
206    /// The published name of this configuration command.
207    #[must_use]
208    pub const fn published_name(&self) -> &'static str {
209        match self {
210            Self::Set(_) => "config set",
211            Self::Get(_) => "config get",
212            Self::List(_) => "config list",
213            Self::Path => "config path",
214            Self::Unset(_) => "config unset",
215            Self::Doctor => "config doctor",
216        }
217    }
218}
219
220impl meta::MetaCommand {
221    /// The published name of this discovery command.
222    #[must_use]
223    pub const fn published_name(&self) -> &'static str {
224        match self {
225            Self::Commands(_) => "meta commands",
226            Self::Schema(_) => "meta schema",
227            Self::Version => "meta version",
228            Self::Manual(_) => "meta manual",
229            Self::Completions(_) => "meta completions",
230        }
231    }
232}
233
234#[cfg(test)]
235mod tests {
236    use super::*;
237    use clap::CommandFactory;
238
239    /// The tree parses and every flag has a long name, as the contract requires.
240    #[test]
241    fn the_command_tree_is_well_formed() {
242        Cli::command().debug_assert();
243    }
244
245    /// The surface carries exactly the fifty-nine commands the map publishes.
246    #[test]
247    fn the_surface_has_the_published_command_count() {
248        fn leaves(command: &clap::Command) -> usize {
249            let mut total = 0;
250            for sub in command.get_subcommands() {
251                if sub.get_subcommands().next().is_some() {
252                    total += leaves(sub);
253                } else {
254                    total += 1;
255                }
256            }
257            total
258        }
259        // `query named` carries four actions of its own; the map counts it as
260        // one command, so its three extra leaves are discounted here.
261        const NAMED_EXTRA_LEAVES: usize = 3;
262        assert_eq!(leaves(&Cli::command()) - NAMED_EXTRA_LEAVES, 60);
263    }
264}