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

View File

@ -33,7 +33,7 @@ sqlx = { version = "0.5.10", path = "..", default-features = false, features = [
"offline", "offline",
] } ] }
futures = "0.3.19" 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" chrono = "0.4.19"
anyhow = "1.0.52" anyhow = "1.0.52"
url = { version = "2.2.2", default-features = false } 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 console::style;
use dotenv::dotenv; use dotenv::dotenv;
use sqlx_cli::Opt; 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] #[tokio::main]
async fn 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(); dotenv().ok();
let matches = Opt::into_app() let Cli::Sqlx(opt) = Cli::parse();
.bin_name("cargo sqlx")
.setting(AppSettings::NoBinaryName)
.get_matches_from(args);
let opt = Opt::from_arg_matches(&matches).unwrap_or_else(|e| e.exit());
if let Err(error) = sqlx_cli::run(opt).await { if let Err(error) = sqlx_cli::run(opt).await {
println!("{} {}", style("error:").bold().red(), error); println!("{} {}", style("error:").bold().red(), error);