-
-
Notifications
You must be signed in to change notification settings - Fork 521
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
sea-orm-migration: bump clap version to 3.1.17 sea-orm-cli: use clap derive API instead of builder API sea-orm-migration: use clap derive
- Loading branch information
Thanh Van
authored and
Thanh Van
committed
May 15, 2022
1 parent
1984a70
commit 01b910a
Showing
9 changed files
with
412 additions
and
418 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,24 @@ | ||
use clap::StructOpt; | ||
use dotenv::dotenv; | ||
use sea_orm_cli::*; | ||
use sea_orm_cli::{handle_error, run_generate_command, run_migrate_command, Cli, Commands}; | ||
|
||
#[async_std::main] | ||
async fn main() { | ||
dotenv().ok(); | ||
|
||
let matches = cli::build_cli().get_matches(); | ||
let cli = Cli::parse(); | ||
let verbose = cli.verbose; | ||
|
||
match matches.subcommand() { | ||
("generate", Some(matches)) => run_generate_command(matches) | ||
.await | ||
match cli.command { | ||
Commands::Generate { command } => { | ||
run_generate_command(command, verbose) | ||
.await | ||
.unwrap_or_else(handle_error); | ||
} | ||
Commands::Migrate { | ||
migration_dir, | ||
command, | ||
} => run_migrate_command(command, migration_dir.as_str(), verbose) | ||
.unwrap_or_else(handle_error), | ||
("migrate", Some(matches)) => run_migrate_command(matches).unwrap_or_else(handle_error), | ||
_ => unreachable!("You should never see this message"), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,26 @@ | ||
//! COPY FROM bin/main.rs | ||
use clap::StructOpt; | ||
use dotenv::dotenv; | ||
use sea_orm_cli::*; | ||
use sea_orm_cli::{handle_error, run_generate_command, run_migrate_command, Cli, Commands}; | ||
|
||
#[async_std::main] | ||
async fn main() { | ||
dotenv().ok(); | ||
|
||
let matches = cli::build_cli().get_matches(); | ||
let cli = Cli::parse(); | ||
let verbose = cli.verbose; | ||
|
||
match matches.subcommand() { | ||
("generate", Some(matches)) => run_generate_command(matches) | ||
.await | ||
match cli.command { | ||
Commands::Generate { command } => { | ||
run_generate_command(command, verbose) | ||
.await | ||
.unwrap_or_else(handle_error); | ||
} | ||
Commands::Migrate { | ||
migration_dir, | ||
command, | ||
} => run_migrate_command(command, migration_dir.as_str(), verbose) | ||
.unwrap_or_else(handle_error), | ||
("migrate", Some(matches)) => run_migrate_command(matches).unwrap_or_else(handle_error), | ||
_ => unreachable!("You should never see this message"), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,129 +1,140 @@ | ||
use crate::migration::get_subcommands; | ||
use clap::{App, AppSettings, Arg, SubCommand}; | ||
use clap::{ArgGroup, Parser, Subcommand}; | ||
|
||
pub fn build_cli() -> App<'static, 'static> { | ||
let entity_subcommand = SubCommand::with_name("generate") | ||
.about("Codegen related commands") | ||
.setting(AppSettings::VersionlessSubcommands) | ||
.subcommand( | ||
SubCommand::with_name("entity") | ||
.about("Generate entity") | ||
.arg( | ||
Arg::with_name("DATABASE_URL") | ||
.long("database-url") | ||
.short("u") | ||
.help("Database URL") | ||
.takes_value(true) | ||
.required(true) | ||
.env("DATABASE_URL"), | ||
) | ||
.arg( | ||
Arg::with_name("DATABASE_SCHEMA") | ||
.long("database-schema") | ||
.short("s") | ||
.help("Database schema") | ||
.long_help("Database schema\n \ | ||
#[derive(Parser, Debug)] | ||
#[clap(version)] | ||
pub struct Cli { | ||
#[clap(global = true, short, long, help = "Show debug messages")] | ||
pub verbose: bool, | ||
|
||
#[clap(subcommand)] | ||
pub command: Commands, | ||
} | ||
|
||
#[derive(Subcommand, PartialEq, Debug)] | ||
pub enum Commands { | ||
#[clap(about = "Codegen related commands")] | ||
#[clap(arg_required_else_help = true)] | ||
Generate { | ||
#[clap(subcommand)] | ||
command: GenerateSubcommands, | ||
}, | ||
#[clap(about = "Migration related commands")] | ||
Migrate { | ||
#[clap( | ||
global = true, | ||
short = 'd', | ||
long, | ||
help = "Migration script directory", | ||
default_value = "./migration" | ||
)] | ||
migration_dir: String, | ||
|
||
#[clap(subcommand)] | ||
command: Option<MigrateSubcommands>, | ||
}, | ||
} | ||
|
||
#[derive(Subcommand, PartialEq, Debug)] | ||
pub enum MigrateSubcommands { | ||
#[clap(about = "Initialize migration directory")] | ||
Init, | ||
#[clap(about = "Generate a new, empty migration")] | ||
Generate { | ||
#[clap(long, required = true, help = "Name of the new migration")] | ||
migration_name: String, | ||
}, | ||
#[clap(about = "Drop all tables from the database, then reapply all migrations")] | ||
Fresh, | ||
#[clap(about = "Rollback all applied migrations, then reapply all migrations")] | ||
Refresh, | ||
#[clap(about = "Rollback all applied migrations")] | ||
Reset, | ||
#[clap(about = "Check the status of all migrations")] | ||
Status, | ||
#[clap(about = "Apply pending migrations")] | ||
Up { | ||
#[clap( | ||
short, | ||
long, | ||
default_value = "1", | ||
help = "Number of pending migrations to be rolled back" | ||
)] | ||
num: u32, | ||
}, | ||
#[clap(about = "Rollback applied migrations")] | ||
Down { | ||
#[clap( | ||
short, | ||
long, | ||
default_value = "1", | ||
help = "Number of pending migrations to be rolled back" | ||
)] | ||
num: u32, | ||
}, | ||
} | ||
|
||
#[derive(Subcommand, PartialEq, Debug)] | ||
pub enum GenerateSubcommands { | ||
#[clap(about = "Generate entity")] | ||
#[clap(arg_required_else_help = true)] | ||
#[clap(group(ArgGroup::new("formats").args(&["compact-format", "expanded-format"])))] | ||
#[clap(group(ArgGroup::new("group-tables").args(&["tables", "include-hidden-tables"])))] | ||
Entity { | ||
#[clap(long, help = "Generate entity file of compact format")] | ||
compact_format: bool, | ||
|
||
#[clap(long, help = "Generate entity file of expanded format")] | ||
expanded_format: bool, | ||
|
||
#[clap( | ||
long, | ||
help = "Generate entity file for hidden tables (i.e. table name starts with an underscore)" | ||
)] | ||
include_hidden_tables: bool, | ||
|
||
#[clap( | ||
short = 't', | ||
long, | ||
use_value_delimiter = true, | ||
takes_value = true, | ||
help = "Generate entity file for specified tables only (comma separated)" | ||
)] | ||
tables: Option<String>, | ||
|
||
#[clap( | ||
long, | ||
default_value = "1", | ||
help = "The maximum amount of connections to use when connecting to the database." | ||
)] | ||
max_connections: u32, | ||
|
||
#[clap( | ||
short = 'o', | ||
long, | ||
default_value = "./", | ||
help = "Entity file output directory" | ||
)] | ||
output_dir: String, | ||
|
||
#[clap( | ||
short = 's', | ||
long, | ||
env = "DATABASE_SCHEMA", | ||
default_value = "public", | ||
long_help = "Database schema\n \ | ||
- For MySQL, this argument is ignored.\n \ | ||
- For PostgreSQL, this argument is optional with default value 'public'.") | ||
.takes_value(true) | ||
.env("DATABASE_SCHEMA"), | ||
) | ||
.arg( | ||
Arg::with_name("OUTPUT_DIR") | ||
.long("output-dir") | ||
.short("o") | ||
.help("Entity file output directory") | ||
.takes_value(true) | ||
.default_value("./"), | ||
) | ||
.arg( | ||
Arg::with_name("INCLUDE_HIDDEN_TABLES") | ||
.long("include-hidden-tables") | ||
.help("Generate entity file for hidden tables (i.e. table name starts with an underscore)") | ||
.takes_value(false), | ||
) | ||
.arg( | ||
Arg::with_name("TABLES") | ||
.long("tables") | ||
.short("t") | ||
.use_delimiter(true) | ||
.help("Generate entity file for specified tables only (comma seperated)") | ||
.takes_value(true) | ||
.conflicts_with("INCLUDE_HIDDEN_TABLES"), | ||
) | ||
.arg( | ||
Arg::with_name("EXPANDED_FORMAT") | ||
.long("expanded-format") | ||
.help("Generate entity file of expanded format") | ||
.takes_value(false) | ||
.conflicts_with("COMPACT_FORMAT"), | ||
) | ||
.arg( | ||
Arg::with_name("COMPACT_FORMAT") | ||
.long("compact-format") | ||
.help("Generate entity file of compact format") | ||
.takes_value(false) | ||
.conflicts_with("EXPANDED_FORMAT"), | ||
) | ||
.arg( | ||
Arg::with_name("WITH_SERDE") | ||
.long("with-serde") | ||
.help("Automatically derive serde Serialize / Deserialize traits for the entity (none, serialize, deserialize, both)") | ||
.takes_value(true) | ||
.default_value("none") | ||
) | ||
.arg( | ||
Arg::with_name("MAX_CONNECTIONS") | ||
.long("max-connections") | ||
.help("The maximum amount of connections to use when connecting to the database.") | ||
.takes_value(true) | ||
.default_value("1") | ||
), | ||
) | ||
.setting(AppSettings::SubcommandRequiredElseHelp); | ||
- For PostgreSQL, this argument is optional with default value 'public'." | ||
)] | ||
database_schema: String, | ||
|
||
let arg_migration_dir = Arg::with_name("MIGRATION_DIR") | ||
.long("migration-dir") | ||
.short("d") | ||
.help("Migration script directory") | ||
.takes_value(true) | ||
.default_value("./migration"); | ||
let mut migrate_subcommands = SubCommand::with_name("migrate") | ||
.about("Migration related commands") | ||
.subcommand( | ||
SubCommand::with_name("init") | ||
.about("Initialize migration directory") | ||
.arg(arg_migration_dir.clone()), | ||
) | ||
.subcommand( | ||
SubCommand::with_name("generate") | ||
.about("Generate a new, empty migration") | ||
.arg( | ||
Arg::with_name("MIGRATION_NAME") | ||
.help("Name of the new migation") | ||
.required(true) | ||
.takes_value(true), | ||
) | ||
.arg(arg_migration_dir.clone()), | ||
) | ||
.arg(arg_migration_dir.clone()); | ||
for subcommand in get_subcommands() { | ||
migrate_subcommands = | ||
migrate_subcommands.subcommand(subcommand.arg(arg_migration_dir.clone())); | ||
} | ||
#[clap(short = 'u', long, env = "DATABASE_URL", help = "Database URL")] | ||
database_url: String, | ||
|
||
App::new("sea-orm-cli") | ||
.version(env!("CARGO_PKG_VERSION")) | ||
.setting(AppSettings::VersionlessSubcommands) | ||
.subcommand(entity_subcommand) | ||
.subcommand(migrate_subcommands) | ||
.arg( | ||
Arg::with_name("VERBOSE") | ||
.long("verbose") | ||
.short("v") | ||
.help("Show debug messages") | ||
.takes_value(false) | ||
.global(true), | ||
) | ||
.setting(AppSettings::SubcommandRequiredElseHelp) | ||
#[clap( | ||
long, | ||
default_value = "none", | ||
help = "Automatically derive serde Serialize / Deserialize traits for the entity (none, serialize, deserialize, both)" | ||
)] | ||
with_serde: String, | ||
}, | ||
} |
Oops, something went wrong.