-
Notifications
You must be signed in to change notification settings - Fork 49
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[PM-3029] Add support for shell completion #103
Changes from 6 commits
37428e2
31ef89f
f38a84e
0882d06
d978372
9450edf
3217d84
eb4d14d
466644a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,7 @@ use bitwarden::{ | |
}, | ||
}; | ||
use clap::{ArgGroup, CommandFactory, Parser, Subcommand}; | ||
use clap_complete::Shell; | ||
use color_eyre::eyre::{bail, Result}; | ||
use log::error; | ||
|
||
|
@@ -26,16 +27,16 @@ use render::{serialize_response, Color, Output}; | |
use uuid::Uuid; | ||
|
||
#[derive(Parser, Debug)] | ||
#[command(name = "Bitwarden Secrets CLI", version, about = "Bitwarden Secrets CLI", long_about = None)] | ||
#[command(name = "bws", version, about = "Bitwarden Secrets CLI", long_about = None)] | ||
struct Cli { | ||
// Optional as a workaround for https://github.com/clap-rs/clap/issues/3572 | ||
#[command(subcommand)] | ||
command: Option<Commands>, | ||
|
||
#[arg(short = 'o', long, global = true, value_enum, default_value_t = Output::JSON)] | ||
#[arg(short = 'o', long, global = true, value_enum, default_value_t = Output::JSON, help="Select the output format for the commands", hide = true)] | ||
dani-garcia marked this conversation as resolved.
Show resolved
Hide resolved
|
||
output: Output, | ||
|
||
#[arg(short = 'c', long, global = true, value_enum, default_value_t = Color::Auto)] | ||
#[arg(short = 'c', long, global = true, value_enum, default_value_t = Color::Auto, help="Enable or disable the use of colors in the output")] | ||
dani-garcia marked this conversation as resolved.
Show resolved
Hide resolved
|
||
color: Color, | ||
|
||
#[arg(short = 't', long, global = true, env = ACCESS_TOKEN_KEY_VAR_NAME, hide_env_values = true, help="Specify access token for the service account")] | ||
|
@@ -66,6 +67,10 @@ enum Commands { | |
#[arg(short = 'd', long)] | ||
delete: bool, | ||
}, | ||
|
||
#[command(long_about = "Generate shell completion files")] | ||
Completions { shell: Option<Shell> }, | ||
|
||
#[command(long_about = "Commands available on Projects")] | ||
Project { | ||
#[command(subcommand)] | ||
|
@@ -245,6 +250,18 @@ async fn process_commands() -> Result<()> { | |
return Ok(()); | ||
}; | ||
|
||
if let Commands::Completions { shell } = command { | ||
let Some(shell) = shell.or_else(Shell::from_env) else { | ||
eprintln!("Couldn't autodetect a valid shell. Run `bws completions --help` for more info."); | ||
std::process::exit(1); | ||
}; | ||
|
||
let mut cmd = Cli::command(); | ||
let name = cmd.get_name().to_string(); | ||
clap_complete::generate(shell, &mut cmd, name, &mut std::io::stdout()); | ||
return Ok(()); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We're starting to have a few if let here, should we try and use a switch? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's a good point, we should probably consider extracting them to a separate function in the future because the process_commands is already getting pretty big, but that's a bigger refactoring. |
||
|
||
// Modify profile commands | ||
if let Commands::Config { | ||
name, | ||
|
@@ -577,7 +594,7 @@ async fn process_commands() -> Result<()> { | |
} | ||
} | ||
|
||
Commands::Config { .. } => { | ||
Commands::Config { .. } | Commands::Completions { .. } => { | ||
unreachable!() | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be in the readme or the help docs? The readme is only really visible on crates.io
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The help docs should probably only be updated after we make a release with these changes merged in I think, otherwise it might be confusing for users of the existing release.