diff --git a/Cargo.lock b/Cargo.lock index f9e423f0cd..a63a119f8a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -531,6 +531,15 @@ dependencies = [ "textwrap 0.15.1", ] +[[package]] +name = "clap_complete" +version = "3.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f7a2e0a962c45ce25afce14220bc24f9dade0a1787f185cecf96bfba7847cd8" +dependencies = [ + "clap 3.2.22", +] + [[package]] name = "clap_derive" version = "3.2.18" @@ -4044,6 +4053,7 @@ dependencies = [ "cargo-target-dep", "chrono", "clap 3.2.22", + "clap_complete", "cloud", "cloud-openapi", "comfy-table", diff --git a/Cargo.toml b/Cargo.toml index 75621726ef..cb95733291 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,6 +18,7 @@ bindle = { workspace = true } bytes = "1.1" chrono = "0.4" clap = { version = "3.1.15", features = ["derive", "env"] } +clap_complete = "3.1.4" cloud = { path = "crates/cloud" } cloud-openapi = { git = "https://github.com/fermyon/cloud-openapi" } comfy-table = "5.0" diff --git a/src/bin/spin.rs b/src/bin/spin.rs index b25af89e63..065675c9ec 100644 --- a/src/bin/spin.rs +++ b/src/bin/spin.rs @@ -57,6 +57,8 @@ enum SpinApp { Plugin(PluginCommands), #[clap(subcommand, hide = true)] Trigger(TriggerCommands), + #[clap(hide = true, name = "generate-bash-completions")] + GenerateBashCompletions, #[clap(external_subcommand)] External(Vec), } @@ -82,6 +84,11 @@ impl SpinApp { Self::Trigger(TriggerCommands::Redis(cmd)) => cmd.run().await, Self::Login(cmd) => cmd.run().await, Self::Plugin(cmd) => cmd.run().await, + Self::GenerateBashCompletions => { + let mut cmd: clap::Command = SpinApp::into_app(); + print_completions(clap_complete::Shell::Bash, &mut cmd); + Ok(()) + } Self::External(cmd) => execute_external_subcommand(cmd, SpinApp::command()).await, } } @@ -96,3 +103,7 @@ fn build_info() -> String { env!("VERGEN_GIT_COMMIT_DATE") ) } + +fn print_completions(gen: G, cmd: &mut clap::Command) { + clap_complete::generate(gen, cmd, cmd.get_name().to_string(), &mut std::io::stdout()) +}