Skip to content

Commit 1b7631e

Browse files
Add CLI autocompletion using clap_complete (#2559)
1 parent 4b254ea commit 1b7631e

File tree

5 files changed

+35
-1
lines changed

5 files changed

+35
-1
lines changed

Cargo.lock

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sqlx-cli/Cargo.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ sqlx = { workspace = true, default-features = false, features = [
3434
] }
3535
futures = "0.3.19"
3636
clap = { version = "3.1.0", features = ["derive", "env"] }
37+
clap_complete = { version = "3.1.0", optional = true }
3738
chrono = { version = "0.4.19", default-features = false, features = ["clock"] }
3839
anyhow = "1.0.52"
3940
url = { version = "2.2.2", default-features = false }
@@ -52,7 +53,7 @@ filetime = "0.2"
5253
backoff = { version = "0.4.0", features = ["futures", "tokio"] }
5354

5455
[features]
55-
default = ["postgres", "sqlite", "mysql", "native-tls"]
56+
default = ["postgres", "sqlite", "mysql", "native-tls", "completions"]
5657
rustls = ["sqlx/runtime-tokio-rustls"]
5758
native-tls = ["sqlx/runtime-tokio-native-tls"]
5859

@@ -63,3 +64,5 @@ sqlite = ["sqlx/sqlite"]
6364

6465
# workaround for musl + openssl issues
6566
openssl-vendored = ["openssl/vendored"]
67+
68+
completions = ["dep:clap_complete"]

sqlx-cli/src/completions.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
use std::io;
2+
3+
use clap::CommandFactory;
4+
use clap_complete::{generate, Shell};
5+
6+
use crate::opt::Command;
7+
8+
pub fn run(shell: Shell) {
9+
generate(shell, &mut Command::command(), "sqlx", &mut io::stdout())
10+
}

sqlx-cli/src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ mod database;
1212
mod metadata;
1313
// mod migration;
1414
// mod migrator;
15+
#[cfg(feature = "completions")]
16+
mod completions;
1517
mod migrate;
1618
mod opt;
1719
mod prepare;
@@ -68,6 +70,9 @@ pub async fn run(opt: Opt) -> Result<()> {
6870
connect_opts,
6971
args,
7072
} => prepare::run(check, workspace, connect_opts, args).await?,
73+
74+
#[cfg(feature = "completions")]
75+
Command::Completions { shell } => completions::run(shell),
7176
};
7277

7378
Ok(())

sqlx-cli/src/opt.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use std::ops::{Deref, Not};
22

33
use clap::{Args, Parser};
4+
#[cfg(feature = "completions")]
5+
use clap_complete::Shell;
46

57
#[derive(Parser, Debug)]
68
#[clap(version, about, author)]
@@ -46,6 +48,10 @@ pub enum Command {
4648

4749
#[clap(alias = "mig")]
4850
Migrate(MigrateOpt),
51+
52+
#[cfg(feature = "completions")]
53+
/// Generate shell completions for the specified shell
54+
Completions { shell: Shell },
4955
}
5056

5157
/// Group of commands for creating and dropping your database.

0 commit comments

Comments
 (0)