Skip to content

Commit

Permalink
feat: Add shell completion generation (#1775)
Browse files Browse the repository at this point in the history
  • Loading branch information
NickyMeuleman authored Oct 11, 2023
1 parent 2ac07d7 commit e16f636
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 4 deletions.
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.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ clap = { version = "4.1.6", default-features = false, features = [
"usage",
"error-context",
] }
clap_complete = "4.4.3"
console = "0.15.5"
curl = { version = "0.4.44", features = ["static-curl", "static-ssl"] }
dirs = "4.0.0"
Expand Down
34 changes: 30 additions & 4 deletions src/commands/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
//! This module implements the root command of the CLI tool.
use std::env;
use std::io;
use std::process;

use anyhow::{bail, Result};
use clap::{Arg, ArgAction, ArgMatches, Command};
use clap::{value_parser, Arg, ArgAction, ArgMatches, Command};
use clap_complete::{generate, Generator, Shell};
use log::{debug, info, set_logger, set_max_level, LevelFilter};

use crate::api::Api;
Expand Down Expand Up @@ -75,6 +77,10 @@ const UPDATE_NAGGER_CMDS: &[&str] = &[
"sourcemaps",
];

fn print_completions<G: Generator>(gen: G, cmd: &mut Command) {
generate(gen, cmd, cmd.get_name().to_string(), &mut io::stdout());
}

fn preexecute_hooks() -> Result<bool> {
return sentry_react_native_xcode_wrap();

Expand Down Expand Up @@ -187,6 +193,16 @@ fn app() -> Command {
.hide(true)
.help("Always return 0 exit code."),
)
.subcommand(
Command::new("completions")
.about("Generate completions for the specified shell.")
.arg_required_else_help(true)
.arg(
Arg::new("shell")
.help("The shell to print completions for.")
.value_parser(value_parser!(Shell)),
)
)
}

fn add_commands(mut app: Command) -> Command {
Expand Down Expand Up @@ -227,9 +243,9 @@ pub fn execute() -> Result<()> {
}

let mut config = Config::from_cli_config()?;
let mut app = app();
app = add_commands(app);
let matches = app.get_matches();
let mut cmd = app();
cmd = add_commands(cmd);
let matches = cmd.get_matches();
configure_args(&mut config, &matches)?;
set_quiet_mode(matches.get_flag("quiet"));

Expand All @@ -255,6 +271,16 @@ pub fn execute() -> Result<()> {
.join(" ")
);

if let Some(argmatches) = matches.subcommand_matches("completions") {
let mut cmd = app();
cmd = add_commands(cmd);
if let Some(generator) = argmatches.get_one::<Shell>("shell") {
eprintln!("Generating completion file for {generator}...");
print_completions(*generator, &mut cmd);
return Ok(());
}
}

match run_command(&matches) {
Ok(()) => Ok(()),
Err(e) => {
Expand Down
1 change: 1 addition & 0 deletions tests/integration/_cases/help/help-windows.trycmd
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ to learn more about them.
Usage: sentry-cli[EXE] [OPTIONS] <COMMAND>

Commands:
completions Generate completions for the specified shell.
debug-files Locate, analyze or upload debug information files. [aliases: dif]
deploys Manage deployments for Sentry releases.
events Manage events on Sentry.
Expand Down
1 change: 1 addition & 0 deletions tests/integration/_cases/help/help.trycmd
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ to learn more about them.
Usage: sentry-cli[EXE] [OPTIONS] <COMMAND>

Commands:
completions Generate completions for the specified shell.
debug-files Locate, analyze or upload debug information files. [aliases: dif]
deploys Manage deployments for Sentry releases.
events Manage events on Sentry.
Expand Down

0 comments on commit e16f636

Please sign in to comment.