mod args;
mod asked;
mod backup;
mod banner;
mod client;
mod connection;
mod files;
mod help;
mod kv;
mod lines;
mod local;
mod logs;
mod mcp;
mod node;
mod node_config;
mod remote;
mod search;
mod serve;
mod sql;
mod status;
mod stop;
mod token;
mod witness;
use std::process::ExitCode;
pub(crate) fn meant(given: &str, names: &[&str]) -> String {
let edits = match given.chars().count() {
0..3 => 0,
3..7 => 1,
_ => 2,
};
tinystore::typos::Dictionary::new(names)
.nearest(given, edits)
.map_or_else(String::new, |near| format!("; did you mean {}?", near.word))
}
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("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("version" | "--version" | "-V") => {
println!("tinystore {}", env!("CARGO_PKG_VERSION"));
ExitCode::SUCCESS
}
None => {
help::print();
ExitCode::SUCCESS
}
Some("help" | "--help" | "-h") if rest.is_empty() => {
help::print();
ExitCode::SUCCESS
}
Some("help") => {
let name = rest.join(" ");
match help::command(&name).or_else(|| help::command(&rest[0])) {
Some(command) => {
help::print_command(command);
ExitCode::SUCCESS
}
None if rest[0] == "node" => node::run(&["--help".to_owned()]),
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), "");
}
}