From 96b986a0a00294ff83c9a36d738e9a7d5cd407b1 Mon Sep 17 00:00:00 2001 From: MrSnowy Date: Tue, 17 Mar 2026 18:17:51 +0000 Subject: [PATCH] Add completions sub-command for generating shell completions (#1386) Made a quick pr to add a sub-command called completions for generating shell completions, was going pretty crazy that this wasn't a thing :P. Tried my best to do everything properly, let me know if I need to change something, I tested it and it works perfectly. Co-authored-by: MrSnowy Reviewed-on: https://git.deuxfleurs.fr/Deuxfleurs/garage/pulls/1386 Reviewed-by: Alex Co-authored-by: MrSnowy Co-committed-by: MrSnowy --- src/garage/cli/local/completions.rs | 10 ++++++++++ src/garage/cli/local/mod.rs | 1 + src/garage/cli/structs.rs | 6 +++++- src/garage/main.rs | 4 ++++ 4 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 src/garage/cli/local/completions.rs diff --git a/src/garage/cli/local/completions.rs b/src/garage/cli/local/completions.rs new file mode 100644 index 00000000..58f58a40 --- /dev/null +++ b/src/garage/cli/local/completions.rs @@ -0,0 +1,10 @@ +use structopt::{clap::Shell, StructOpt}; + +use crate::Opt; + +pub(crate) fn generate_completions(shell: Shell) { + let mut command = Opt::clap(); + + let command_name = command.get_name().to_string(); + command.gen_completions_to(command_name, shell, &mut std::io::stdout()); +} diff --git a/src/garage/cli/local/mod.rs b/src/garage/cli/local/mod.rs index 476010b8..edd5e66c 100644 --- a/src/garage/cli/local/mod.rs +++ b/src/garage/cli/local/mod.rs @@ -1,3 +1,4 @@ +pub(crate) mod completions; pub(crate) mod convert_db; pub(crate) mod init; pub(crate) mod repair; diff --git a/src/garage/cli/structs.rs b/src/garage/cli/structs.rs index e7422721..f623c60d 100644 --- a/src/garage/cli/structs.rs +++ b/src/garage/cli/structs.rs @@ -1,4 +1,4 @@ -use structopt::StructOpt; +use structopt::{clap::Shell, StructOpt}; use garage_util::version::garage_version; @@ -77,6 +77,10 @@ pub enum Command { #[structopt(default_value = "null")] payload: String, }, + + /// Generate completions for a shell + #[structopt(name = "completions", version = garage_version())] + Completions { shell: Shell }, } // --------------------------- diff --git a/src/garage/main.rs b/src/garage/main.rs index 0ab64086..62d10c82 100644 --- a/src/garage/main.rs +++ b/src/garage/main.rs @@ -177,6 +177,10 @@ async fn run(opt: Opt) -> Result<(), Error> { ); Ok(()) } + Command::Completions { shell } => { + cli::local::completions::generate_completions(shell); + Ok(()) + } _ => cli_command(opt).await, } }