Skip to content
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

Merged
merged 9 commits into from
Aug 21, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.

4 changes: 4 additions & 0 deletions crates/bws/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- Support for shell autocompletion with the `bws completions` command (#103)

## [0.3.0] - 2023-07-26

### Deprecated
Expand Down
3 changes: 2 additions & 1 deletion crates/bws/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ chrono = { version = "0.4.26", features = [
"clock",
"std",
], default-features = false }
clap = { version = "4.3.0", features = ["derive", "env"] }
clap = { version = "4.3.0", features = ["derive", "env", "string"] }
clap_complete = "4.3.2"
color-eyre = "0.6"
comfy-table = "^7.0.1"
directories = "5.0.1"
Expand Down
24 changes: 24 additions & 0 deletions crates/bws/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,29 @@ Or download a pre-built binary from the [Releases](https://github.com/bitwarden/
bws --help
```

## How to enable shell autocompletions

### Zsh

If completion is not enabled already, you need to enable it first:

```zsh
echo "autoload -U compinit; compinit" >> ~/.zshrc
```

Enable autocompletions for the current user:

```zsh
echo 'source <(/path/to/bws completions zsh)' >> ~/.zshrc
```

### Bash

Enable autocompletions for the current user:

```zsh
echo 'source <(/path/to/bws completions bash)' >> ~/.bashrc
```
Comment on lines +21 to +43
Copy link
Member

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

Copy link
Member Author

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.


For more detailed documentation, please refer to the
[Secrets Manager CLI help article](https://bitwarden.com/help/secrets-manager-cli/).
25 changes: 21 additions & 4 deletions crates/bws/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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")]
Expand Down Expand Up @@ -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)]
Expand Down Expand Up @@ -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(());
}
Copy link
Member

Choose a reason for hiding this comment

The 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?

Copy link
Member Author

Choose a reason for hiding this comment

The 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,
Expand Down Expand Up @@ -577,7 +594,7 @@ async fn process_commands() -> Result<()> {
}
}

Commands::Config { .. } => {
Commands::Config { .. } | Commands::Completions { .. } => {
unreachable!()
}
}
Expand Down