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

fix: improve completions dx #451

Merged
merged 3 commits into from
Jan 3, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 3 additions & 3 deletions docs/cli_usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
## Shell Completions

We support shell completions through clap-complete.
You can generate them for your shell using the `--generate` command.
You can generate them for your shell using the `completion` command.

Eg,
```sh
rattler-build --generate=zsh > ${ZSH_COMPLETIONS_PATH:~/.zsh/completions}/_rattler-build
rattler-build completion --shell=zsh > ${ZSH_COMPLETIONS_PATH:~/.zsh/completions}/_rattler-build
compinit
```

Expand All @@ -24,5 +24,5 @@ test -- Test a package

Example for Fish Shell just generate the `completions.fish` and add to `~/.config/fish/completions`.
```sh
rattler-build --generate=fish > ${ZSH_COMPLETIONS_PATH:~/.config/fish/completions}/rattler-build.fish
rattler-build completion --shell=fish > ${ZSH_COMPLETIONS_PATH:~/.config/fish/completions}/rattler-build.fish
```
52 changes: 30 additions & 22 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,20 @@ enum SubCommands {

/// Upload a package
Upload(UploadOpts),

/// Generate shell completion script
Completion(ShellCompletion),
}

#[derive(Parser)]
struct ShellCompletion {
#[arg(short, long)]
shell: clap_complete::Shell,
}

#[derive(Parser)]
#[clap(version = crate_version!())]
struct App {
// If provided, outputs the completion file for given shell
#[arg(long = "generate", value_enum)]
generator: Option<clap_complete::Shell>,

#[clap(subcommand)]
subcommand: Option<SubCommands>,

Expand Down Expand Up @@ -223,7 +228,7 @@ struct ArtifactoryOpts {
channel: String,

/// Your Artifactory username
#[arg(short, long, env = "ARTIFACTORY_USERNAME")]
#[arg(short = 'r', long, env = "ARTIFACTORY_USERNAME")]
username: Option<String>,

/// Your Artifactory password
Expand Down Expand Up @@ -269,24 +274,27 @@ async fn main() -> miette::Result<()> {
)
.init();

if let Some(generator) = args.generator {
let mut cmd = App::command();
tracing::info!("Generating completion file for {generator:?}...");
fn print_completions<G: clap_complete::Generator>(gen: G, cmd: &mut clap::Command) {
clap_complete::generate(gen, cmd, cmd.get_name().to_string(), &mut std::io::stdout());
}
print_completions(generator, &mut cmd);
Ok(())
} else {
match args.subcommand {
Some(SubCommands::Build(args)) => run_build_from_args(args, multi_progress).await,
Some(SubCommands::Test(args)) => run_test_from_args(args).await,
Some(SubCommands::Rebuild(args)) => rebuild_from_args(args).await,
Some(SubCommands::Upload(args)) => upload_from_args(args).await,
None => {
_ = App::command().print_long_help();
Ok(())
match args.subcommand {
Some(SubCommands::Completion(ShellCompletion { shell })) => {
let mut cmd = App::command();
fn print_completions<G: clap_complete::Generator>(gen: G, cmd: &mut clap::Command) {
clap_complete::generate(
gen,
cmd,
cmd.get_name().to_string(),
&mut std::io::stdout(),
);
}
print_completions(shell, &mut cmd);
Ok(())
}
Some(SubCommands::Build(args)) => run_build_from_args(args, multi_progress).await,
Some(SubCommands::Test(args)) => run_test_from_args(args).await,
Some(SubCommands::Rebuild(args)) => rebuild_from_args(args).await,
Some(SubCommands::Upload(args)) => upload_from_args(args).await,
None => {
_ = App::command().print_long_help();
Ok(())
}
}
}
Expand Down