From f291e03fd7d94fae02ba5bfd9f1845f5b635ebc5 Mon Sep 17 00:00:00 2001 From: Finley Thomalla Date: Wed, 13 Nov 2024 15:58:51 +0100 Subject: [PATCH] feat: add shell completions subcommand (#106) Adds a subcommand "completions" which can generate shell completions for a few supported shells, namely bash, zsh and Nushell. You can use it like this: `koji completions > /_koji`. I consider this feature necessary for eventually publishing koji to other package registries, such as the AUR. --- Cargo.lock | 31 +++++++++++++++++++++++++++++++ Cargo.toml | 1 + README.md | 2 ++ src/bin/main.rs | 21 ++++++++++++++++++++- 4 files changed, 54 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index fac1e66..b662932 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -215,6 +215,36 @@ dependencies = [ "strsim", ] +[[package]] +name = "clap_complete" +version = "4.5.37" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "11611dca53440593f38e6b25ec629de50b14cdfa63adc0fb856115a2c6d97595" +dependencies = [ + "clap", +] + +[[package]] +name = "clap_complete_command" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da8e198c052315686d36371e8a3c5778b7852fc75cc313e4e11eeb7a644a1b62" +dependencies = [ + "clap", + "clap_complete", + "clap_complete_nushell", +] + +[[package]] +name = "clap_complete_nushell" +version = "4.5.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "315902e790cc6e5ddd20cbd313c1d0d49db77f191e149f96397230fb82a17677" +dependencies = [ + "clap", + "clap_complete", +] + [[package]] name = "clap_derive" version = "4.5.18" @@ -901,6 +931,7 @@ version = "2.2.0" dependencies = [ "anyhow", "clap", + "clap_complete_command", "cocogitto", "conventional_commit_parser", "dirs", diff --git a/Cargo.toml b/Cargo.toml index 9ffba59..7d2d29b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -31,6 +31,7 @@ indexmap = "2.1" serde = { version = "1.0", features = ["derive"] } toml = "0.8" inquire = "0.7.5" +clap_complete_command = { version = "0.6.1", features = ["nushell"]} [features] vendored-openssl = ["git2/vendored-openssl"] diff --git a/README.md b/README.md index 8a17e31..d2ddda9 100644 --- a/README.md +++ b/README.md @@ -58,6 +58,8 @@ koji See `koji --help` for more options. +Use `koji completions ` to generate completion scripts for your shell. + ## Using as a git hook An alternative way to use koji is as a [git hook](https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks), diff --git a/src/bin/main.rs b/src/bin/main.rs index f620ffa..42fd5a7 100644 --- a/src/bin/main.rs +++ b/src/bin/main.rs @@ -1,7 +1,7 @@ use std::fs::read_to_string; use anyhow::{Context, Result}; -use clap::Parser; +use clap::{CommandFactory, Parser, Subcommand}; use cocogitto::command::commit::CommitOptions; use conventional_commit_parser::parse; use git2::Repository; @@ -16,6 +16,9 @@ use koji::questions::create_prompt; version )] struct Args { + #[command(subcommand)] + command: Option, + #[arg( long, default_missing_value = "true", @@ -86,10 +89,17 @@ struct Args { all: bool, } +#[derive(Debug, Subcommand)] +enum SubCmds { + #[command(about = "Generate shell completions")] + Completions { shell: clap_complete_command::Shell }, +} + #[cfg(not(tarpaulin_include))] fn main() -> Result<()> { // Get CLI args let Args { + command, autocomplete, breaking_changes, config, @@ -100,6 +110,15 @@ fn main() -> Result<()> { all, } = Args::parse(); + if command.is_some() { + match command.unwrap() { + SubCmds::Completions { shell } => { + shell.generate(&mut Args::command(), &mut std::io::stdout()); + } + } + return Ok(()); + } + // Find repo let repo = Repository::discover(std::env::current_dir()?).context("could not find git repository")?;