From fe1877a49d994292cff068f2d4854fa8c5f730ab Mon Sep 17 00:00:00 2001 From: Alex Date: Sat, 26 Mar 2022 18:06:14 +0800 Subject: [PATCH] `sea` as an alternative bin name to `sea-orm-cli` (#558) * Simplify bin name from sea-orm-cli to sea * -S * fixed test --- sea-orm-cli/Cargo.toml | 12 ++++++++++- sea-orm-cli/README.md | 14 +++++++++++++ sea-orm-cli/src/bin/main.rs | 18 ++++++++++++++++ sea-orm-cli/src/bin/sea.rs | 19 +++++++++++++++++ sea-orm-cli/src/cli.rs | 1 + sea-orm-cli/src/{main.rs => commands.rs} | 26 +++++------------------- sea-orm-cli/src/lib.rs | 5 +++++ 7 files changed, 73 insertions(+), 22 deletions(-) create mode 100644 sea-orm-cli/src/bin/main.rs create mode 100644 sea-orm-cli/src/bin/sea.rs rename sea-orm-cli/src/{main.rs => commands.rs} (95%) create mode 100644 sea-orm-cli/src/lib.rs diff --git a/sea-orm-cli/Cargo.toml b/sea-orm-cli/Cargo.toml index c683dc3a..dfcc3fe0 100644 --- a/sea-orm-cli/Cargo.toml +++ b/sea-orm-cli/Cargo.toml @@ -12,10 +12,20 @@ documentation = "https://docs.rs/sea-orm" repository = "https://github.com/SeaQL/sea-orm" categories = [ "database" ] keywords = ["async", "orm", "mysql", "postgres", "sqlite"] +default-run = "sea-orm-cli" + + +[lib] +name = "sea_orm_cli" +path = "src/lib.rs" [[bin]] name = "sea-orm-cli" -path = "src/main.rs" +path = "src/bin/main.rs" + +[[bin]] +name = "sea" +path = "src/bin/sea.rs" [dependencies] clap = { version = "^2.33.3" } diff --git a/sea-orm-cli/README.md b/sea-orm-cli/README.md index af7f1dd3..87a41232 100644 --- a/sea-orm-cli/README.md +++ b/sea-orm-cli/README.md @@ -1,5 +1,19 @@ # SeaORM CLI +Install and Usage: + +```sh +> cargo install sea-orm-cli +> sea-orm-cli help +``` + +Or: + +```sh +> cargo install --bin sea +> sea help +``` + Getting Help: ```sh diff --git a/sea-orm-cli/src/bin/main.rs b/sea-orm-cli/src/bin/main.rs new file mode 100644 index 00000000..f09b5b8e --- /dev/null +++ b/sea-orm-cli/src/bin/main.rs @@ -0,0 +1,18 @@ + +use dotenv::dotenv; +use sea_orm_cli::*; + +#[async_std::main] +async fn main() { + dotenv().ok(); + + let matches = cli::build_cli().get_matches(); + + match matches.subcommand() { + ("generate", Some(matches)) => run_generate_command(matches) + .await + .unwrap_or_else(handle_error), + ("migrate", Some(matches)) => run_migrate_command(matches).unwrap_or_else(handle_error), + _ => unreachable!("You should never see this message"), + } +} diff --git a/sea-orm-cli/src/bin/sea.rs b/sea-orm-cli/src/bin/sea.rs new file mode 100644 index 00000000..1eb4dad5 --- /dev/null +++ b/sea-orm-cli/src/bin/sea.rs @@ -0,0 +1,19 @@ +//! COPY FROM bin/main.rs + +use dotenv::dotenv; +use sea_orm_cli::*; + +#[async_std::main] +async fn main() { + dotenv().ok(); + + let matches = cli::build_cli().get_matches(); + + match matches.subcommand() { + ("generate", Some(matches)) => run_generate_command(matches) + .await + .unwrap_or_else(handle_error), + ("migrate", Some(matches)) => run_migrate_command(matches).unwrap_or_else(handle_error), + _ => unreachable!("You should never see this message"), + } +} \ No newline at end of file diff --git a/sea-orm-cli/src/cli.rs b/sea-orm-cli/src/cli.rs index ee81f7b5..7b6a46df 100644 --- a/sea-orm-cli/src/cli.rs +++ b/sea-orm-cli/src/cli.rs @@ -108,3 +108,4 @@ pub fn build_cli() -> App<'static, 'static> { ) .setting(AppSettings::SubcommandRequiredElseHelp) } + diff --git a/sea-orm-cli/src/main.rs b/sea-orm-cli/src/commands.rs similarity index 95% rename from sea-orm-cli/src/main.rs rename to sea-orm-cli/src/commands.rs index 23d505ec..f5d38b33 100644 --- a/sea-orm-cli/src/main.rs +++ b/sea-orm-cli/src/commands.rs @@ -1,27 +1,10 @@ + use clap::ArgMatches; -use dotenv::dotenv; use sea_orm_codegen::{EntityTransformer, OutputFile, WithSerde}; use std::{error::Error, fmt::Display, fs, io::Write, path::Path, process::Command, str::FromStr}; use url::Url; -mod cli; - -#[async_std::main] -async fn main() { - dotenv().ok(); - - let matches = cli::build_cli().get_matches(); - - match matches.subcommand() { - ("generate", Some(matches)) => run_generate_command(matches) - .await - .unwrap_or_else(handle_error), - ("migrate", Some(matches)) => run_migrate_command(matches).unwrap_or_else(handle_error), - _ => unreachable!("You should never see this message"), - } -} - -async fn run_generate_command(matches: &ArgMatches<'_>) -> Result<(), Box> { +pub async fn run_generate_command(matches: &ArgMatches<'_>) -> Result<(), Box> { match matches.subcommand() { ("entity", Some(args)) => { let output_dir = args.value_of("OUTPUT_DIR").unwrap(); @@ -188,7 +171,7 @@ async fn run_generate_command(matches: &ArgMatches<'_>) -> Result<(), Box) -> Result<(), Box> { +pub fn run_migrate_command(matches: &ArgMatches<'_>) -> Result<(), Box> { let migrate_subcommand = matches.subcommand(); // If it's `migrate init` if let ("init", Some(args)) = migrate_subcommand { @@ -263,7 +246,7 @@ fn run_migrate_command(matches: &ArgMatches<'_>) -> Result<(), Box> { Ok(()) } -fn handle_error(error: E) +pub fn handle_error(error: E) where E: Display, { @@ -275,6 +258,7 @@ where mod tests { use super::*; use clap::AppSettings; + use crate::cli; #[test] #[should_panic( diff --git a/sea-orm-cli/src/lib.rs b/sea-orm-cli/src/lib.rs new file mode 100644 index 00000000..719a77cc --- /dev/null +++ b/sea-orm-cli/src/lib.rs @@ -0,0 +1,5 @@ +pub mod cli; +pub mod commands; + +pub use cli::*; +pub use commands::*; \ No newline at end of file