Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bump clap version to 3.2 #706

Merged
merged 4 commits into from
Jun 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion sea-orm-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ path = "src/bin/sea.rs"
required-features = ["codegen"]

[dependencies]
clap = { version = "^2.33.3" }
clap = { version = "^3.2", features = ["env", "derive"] }
dotenv = { version = "^0.15" }
async-std = { version = "^1.9", features = [ "attributes", "tokio1" ] }
sea-orm-codegen = { version = "^0.8.0", path = "../sea-orm-codegen", optional = true }
Expand Down
21 changes: 14 additions & 7 deletions sea-orm-cli/src/bin/main.rs
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"),
}
}
21 changes: 14 additions & 7 deletions sea-orm-cli/src/bin/sea.rs
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"),
}
}
279 changes: 155 additions & 124 deletions sea-orm-cli/src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,129 +1,160 @@
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(action, 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(
value_parser,
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(
value_parser,
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(
value_parser,
short,
long,
default_value = "1",
help = "Number of pending migrations to be rolled back"
)]
num: u32,
},
#[clap(value_parser, about = "Rollback applied migrations")]
Down {
#[clap(
value_parser,
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(action, long, help = "Generate entity file of compact format")]
compact_format: bool,

#[clap(action, long, help = "Generate entity file of expanded format")]
expanded_format: bool,

#[clap(
action,
long,
help = "Generate entity file for hidden tables (i.e. table name starts with an underscore)"
)]
include_hidden_tables: bool,

#[clap(
value_parser,
short = 't',
long,
use_value_delimiter = true,
takes_value = true,
help = "Generate entity file for specified tables only (comma separated)"
)]
tables: Option<String>,

#[clap(
value_parser,
long,
default_value = "1",
help = "The maximum amount of connections to use when connecting to the database."
)]
max_connections: u32,

#[clap(
value_parser,
short = 'o',
long,
default_value = "./",
help = "Entity file output directory"
)]
output_dir: String,

#[clap(
value_parser,
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(
value_parser,
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(
value_parser,
long,
default_value = "none",
help = "Automatically derive serde Serialize / Deserialize traits for the entity (none, serialize, deserialize, both)"
)]
with_serde: String,
},
}