macro_rules! out {
($($arg:tt)*) => {
$crate::asked::out(format_args!($($arg)*))
};
}
macro_rules! outln {
() => {
$crate::asked::out(format_args!("\n"))
};
($($arg:tt)*) => {
$crate::asked::out(format_args!("{}\n", format_args!($($arg)*)))
};
}
mod args;
mod asked;
mod backup;
mod banner;
mod channels;
mod client;
mod complete;
mod config;
mod connection;
mod events;
mod files;
mod following;
mod help;
mod jobs;
mod kv;
mod lines;
mod local;
mod lock;
mod logs;
mod mcp;
mod mcp_writes;
mod metrics;
mod names;
mod node;
mod node_config;
mod on_disk;
#[cfg(windows)]
mod own_handles;
#[cfg(windows)]
mod owner_only;
mod reach;
mod remote;
mod search;
mod serve;
mod sql;
mod status;
mod stop;
mod token;
mod window;
mod witness;
use std::process::ExitCode;
pub(crate) use tinystore::mcp::given::meant;
fn main() -> ExitCode {
let args: Vec<String> = std::env::args().skip(1).collect();
let rest = args.get(1..).unwrap_or_default();
match args.first().map(String::as_str) {
Some("status") => status::run(rest),
Some("logs") => logs::run(rest),
Some("kv") => kv::run(rest),
Some("jobs") => jobs::run(rest),
Some("files") => files::run(rest),
Some("channels") => channels::run(rest),
Some("lock") => lock::run(rest),
Some("config") => config::run(rest),
Some("metrics") => metrics::run(rest),
Some("events") => events::run(rest),
Some("sql") => sql::run(rest),
Some("search") => search::run(rest),
Some("serve") => serve::run(rest),
Some("stop") => stop::run(rest),
Some("backup") => backup::backup(rest),
Some("restore") => backup::restore(rest),
Some("mcp") => mcp::run(rest),
Some("token") => token::run(rest),
Some("node") => node::run(rest),
Some("witness") => witness::run(rest),
Some("migrate") => sql::migrate(rest),
Some("schema") => sql::schema(rest),
Some("completion") => complete::completion(rest),
Some("__complete") => complete::run(rest),
Some("version" | "--version" | "-V") => asked::exit("", outln!("tinystore {}", env!("CARGO_PKG_VERSION"))),
None => asked::exit("", help::print()),
Some("help" | "--help" | "-h") if rest.is_empty() => asked::exit("help", help::print()),
Some("help" | "--help" | "-h") if rest == ["--all"] => asked::exit("help", help::print_all()),
Some("help") if rest.contains(&"--all".to_owned()) && rest.contains(&"--json".to_owned()) => {
asked::exit("help", out!("{}", help::json_text()))
}
Some("help") => {
let name = rest.join(" ");
match help::command(&name).or_else(|| help::command(&rest[0])) {
Some(command) => asked::exit("help", help::print_command(command)),
None if rest[0] == "node" => node::run(&["--help".to_owned()]),
None if help::is_group(&rest[0]) => asked::exit("help", help::print_group(&rest[0])),
None => asked::end(
"help",
asked::wrong(format!("no command {name}{}", meant(&rest[0], &help::first_words()))),
),
}
}
Some(other) => {
let refused = asked::wrong(format!("no command {other}{}", meant(other, &help::first_words())));
asked::end("", refused)
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_misspelt_command_is_told_its_nearest_and_a_word_far_from_any_none() {
let commands = help::first_words();
assert_eq!(meant("bakup", &commands), "; did you mean backup?");
assert_eq!(meant("serv", &commands), "; did you mean serve?");
assert_eq!(meant("deploy", &commands), "");
}
}