Skip to content

Commit

Permalink
feat: add shell completions subcommand (#106)
Browse files Browse the repository at this point in the history
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 <SHELL> >
<CUSTOM_COMPLETIONS_DIR>/_koji`.
I consider this feature necessary for eventually publishing koji to
other package registries, such as the AUR.
  • Loading branch information
cococonscious authored Nov 13, 2024
1 parent 26dd8ba commit f291e03
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 1 deletion.
31 changes: 31 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ koji

See `koji --help` for more options.

Use `koji completions <SHELL>` 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),
Expand Down
21 changes: 20 additions & 1 deletion src/bin/main.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -16,6 +16,9 @@ use koji::questions::create_prompt;
version
)]
struct Args {
#[command(subcommand)]
command: Option<SubCmds>,

#[arg(
long,
default_missing_value = "true",
Expand Down Expand Up @@ -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,
Expand All @@ -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")?;
Expand Down

0 comments on commit f291e03

Please sign in to comment.