Simplify cargo-sqlx cmdline definition (#1626)

* Simplify cargo-sqlx cmdline definition

* Add note about the parser definition for cargo-sqlx

* Fix formatting
This commit is contained in:
Marcin Puc 2022-02-16 05:13:42 +01:00 committed by GitHub
parent 2849468e57
commit 313cc69988
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 21 deletions

22
Cargo.lock generated
View File

@ -425,9 +425,9 @@ dependencies = [
[[package]]
name = "clap"
version = "3.0.0-rc.9"
version = "3.0.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c7843ae7a539bef687e018bf9edf7e87728024b29d02b0f8409726be8880ae1a"
checksum = "12e8611f9ae4e068fa3e56931fded356ff745e70987ff76924a6e0ab1c8ef2e3"
dependencies = [
"atty",
"bitflags",
@ -442,11 +442,11 @@ dependencies = [
[[package]]
name = "clap_derive"
version = "3.0.0-rc.9"
version = "3.0.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cae3cc2f259ea636871f5da15b0ac033f1821d7a5506c3d1bfbdde201f14c803"
checksum = "517358c28fcef6607bf6f76108e02afad7e82297d132a6b846dcc1fc3efcd153"
dependencies = [
"heck",
"heck 0.4.0",
"proc-macro-error",
"proc-macro2",
"quote",
@ -1074,6 +1074,12 @@ dependencies = [
"unicode-segmentation",
]
[[package]]
name = "heck"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2540771e65fc8cb83cd6e8a237f70c319bd5c29f78ed1084ba5d50eeac86f7f9"
[[package]]
name = "hermit-abi"
version = "0.1.19"
@ -2386,7 +2392,7 @@ dependencies = [
"anyhow",
"async-trait",
"chrono",
"clap 3.0.0-rc.9",
"clap 3.0.7",
"console",
"dotenv",
"futures",
@ -2543,7 +2549,7 @@ version = "0.5.10"
dependencies = [
"dotenv",
"either",
"heck",
"heck 0.3.3",
"hex",
"once_cell",
"proc-macro2",
@ -2682,7 +2688,7 @@ version = "0.4.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dcb5ae327f9cc13b68763b5749770cb9e048a99bd9dfdfa58d0cf05d5f64afe0"
dependencies = [
"heck",
"heck 0.3.3",
"proc-macro-error",
"proc-macro2",
"quote",

View File

@ -33,7 +33,7 @@ sqlx = { version = "0.5.10", path = "..", default-features = false, features = [
"offline",
] }
futures = "0.3.19"
clap = { version = "3.0.0-rc.9", features = ["derive", "env", "cargo"] }
clap = { version = "3.0", features = ["derive", "env"] }
chrono = "0.4.19"
anyhow = "1.0.52"
url = { version = "2.2.2", default-features = false }

View File

@ -1,22 +1,21 @@
use clap::{crate_version, AppSettings, FromArgMatches, IntoApp};
use clap::Parser;
use console::style;
use dotenv::dotenv;
use sqlx_cli::Opt;
use std::{env, process};
use std::process;
// cargo invokes this binary as `cargo-sqlx sqlx <args>`
// so the parser below is defined with that in mind
#[derive(Parser, Debug)]
#[clap(bin_name = "cargo")]
enum Cli {
Sqlx(Opt),
}
#[tokio::main]
async fn main() {
// when invoked as `cargo sqlx [...]` the args we see are `[...]/sqlx-cli sqlx prepare`
// so we want to notch out that superfluous "sqlx"
let args = env::args_os().skip(2);
dotenv().ok();
let matches = Opt::into_app()
.bin_name("cargo sqlx")
.setting(AppSettings::NoBinaryName)
.get_matches_from(args);
let opt = Opt::from_arg_matches(&matches).unwrap_or_else(|e| e.exit());
let Cli::Sqlx(opt) = Cli::parse();
if let Err(error) = sqlx_cli::run(opt).await {
println!("{} {}", style("error:").bold().red(), error);