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

Add CLI autocompletion using clap_complete #2559

Merged
merged 1 commit into from
Jun 30, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion sqlx-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ sqlx = { workspace = true, default-features = false, features = [
] }
futures = "0.3.19"
clap = { version = "3.1.0", features = ["derive", "env"] }
clap_complete = { version = "3.1.0", optional = true }
chrono = { version = "0.4.19", default-features = false, features = ["clock"] }
anyhow = "1.0.52"
url = { version = "2.2.2", default-features = false }
Expand All @@ -52,7 +53,7 @@ filetime = "0.2"
backoff = { version = "0.4.0", features = ["futures", "tokio"] }

[features]
default = ["postgres", "sqlite", "mysql", "native-tls"]
default = ["postgres", "sqlite", "mysql", "native-tls", "completions"]
rustls = ["sqlx/runtime-tokio-rustls"]
native-tls = ["sqlx/runtime-tokio-native-tls"]

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

# workaround for musl + openssl issues
openssl-vendored = ["openssl/vendored"]

completions = ["dep:clap_complete"]
10 changes: 10 additions & 0 deletions sqlx-cli/src/completions.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use std::io;

use clap::CommandFactory;
use clap_complete::{generate, Shell};

use crate::opt::Command;

pub fn run(shell: Shell) {
generate(shell, &mut Command::command(), "sqlx", &mut io::stdout())
}
5 changes: 5 additions & 0 deletions sqlx-cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ mod database;
mod metadata;
// mod migration;
// mod migrator;
#[cfg(feature = "completions")]
mod completions;
mod migrate;
mod opt;
mod prepare;
Expand Down Expand Up @@ -68,6 +70,9 @@ pub async fn run(opt: Opt) -> Result<()> {
connect_opts,
args,
} => prepare::run(check, workspace, connect_opts, args).await?,

#[cfg(feature = "completions")]
Command::Completions { shell } => completions::run(shell),
};

Ok(())
Expand Down
6 changes: 6 additions & 0 deletions sqlx-cli/src/opt.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use std::ops::{Deref, Not};

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

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

#[clap(alias = "mig")]
Migrate(MigrateOpt),

#[cfg(feature = "completions")]
/// Generate shell completions for the specified shell
Completions { shell: Shell },
}

/// Group of commands for creating and dropping your database.
Expand Down