Skip to content

Commit

Permalink
cmd: add completion command
Browse files Browse the repository at this point in the history
Add `bender completion` command that generates completion scripts
for common shells (bash, zsh, fish, ...) using clap's clap_complete
crate.
  • Loading branch information
schilkp committed Mar 7, 2024
1 parent 818a779 commit 3c1749f
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 2 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 @@ -18,6 +18,7 @@ tokio = { version = "1.27", features = ["full"] }
async-recursion = "1.0"

clap = { version = "4.0", features = ["derive"] }
clap_complete = "4.0"
semver = { version = "1.0", features = ["serde"] }
blake2 = "0.10"
typed-arena = "2"
Expand Down
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ Calling update with the `--fetch/-f` flag will force all git dependencies to be

### `clone` --- Clone dependency to make modifications

The `bender clone <PKG>` command checks out the package `PKG` into a directory (default `working_dir`, can be overridden with `-p / --path <DIR>`).
The `bender clone <PKG>` command checks out the package `PKG` into a directory (default `working_dir`, can be overridden with `-p / --path <DIR>`).
To ensure the package is correctly linked in bender, the `Bender.local` file is modified to include a `path` dependency override, linking to the corresponding package.

This can be used for development of dependent packages within the parent repository, allowing to test uncommitted and committed changes, without the worry that bender would update the dependency.
Expand Down Expand Up @@ -535,6 +535,17 @@ This branch can then be rebased and a pull request can be opened from it as usua
Note: when using mappings in your `vendor_package`, the patches will be relative to the mapped directory.
Hence, for upstreaming, you might need to use `git am --directory=<mapping.from>` instead of plain `git am`.

### `completion` --- Generate shell completion script

The `bender completion <SHELL>` command prints a completion script for the given shell.

Supported shells:
- `bash`
- `elvish`
- `fish`
- `powershell`
- `zsh`

[aur-bender]: https://aur.archlinux.org/packages/bender
[releases]: https://github.com/pulp-platform/bender/releases
[rust-installation]: https://doc.rust-lang.org/book/ch01-01-installation.html
8 changes: 7 additions & 1 deletion src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ pub fn main() -> Result<()> {
.subcommand(cmd::clone::new())
.subcommand(cmd::packages::new())
.subcommand(cmd::sources::new())
.subcommand(cmd::completion::new())
.subcommand(cmd::config::new())
.subcommand(cmd::script::new())
.subcommand(cmd::checkout::new())
Expand All @@ -99,7 +100,7 @@ pub fn main() -> Result<()> {
};

// Parse the arguments.
let matches = app.get_matches();
let matches = app.clone().get_matches();

// Enable debug outputs if needed.
if matches.contains_id("debug") && matches.get_flag("debug") {
Expand All @@ -110,6 +111,11 @@ pub fn main() -> Result<()> {
return cmd::init::run(matches);
}

if let Some(("completion", matches)) = matches.subcommand() {
let mut app = app;
return cmd::completion::run(matches, &mut app);
}

let mut force_fetch = false;
if let Some(("update", intern_matches)) = matches.subcommand() {
force_fetch = intern_matches.get_flag("fetch");
Expand Down
44 changes: 44 additions & 0 deletions src/cmd/completion.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright (c) 2017-2024 ETH Zurich
// Philipp Schilk <[email protected]>

//! The `completion` subcommand.

use std::io;

use crate::error::*;
use clap::{builder::PossibleValue, Arg, ArgMatches, Command};

/// Assemble the `completion` subcommand.
pub fn new() -> Command {
Command::new("completion")
.about("Emit shell completion script")
.arg(
Arg::new("completion_shell")
.help("Shell completion script style")
.required(true)
.num_args(1)
.value_name("SHELL")
.value_parser([
PossibleValue::new("bash"),
PossibleValue::new("elvish"),
PossibleValue::new("fish"),
PossibleValue::new("powershell"),
PossibleValue::new("zsh"),
]),
)
}

/// Execute the `completion` subcommand.
pub fn run(matches: &ArgMatches, app: &mut Command) -> Result<()> {
let shell = matches.get_one::<String>("completion_shell").unwrap();
let shell = match shell.as_str() {
"bash" => clap_complete::Shell::Bash,
"elvish" => clap_complete::Shell::Elvish,
"fish" => clap_complete::Shell::Fish,
"powershell" => clap_complete::Shell::PowerShell,
"zsh" => clap_complete::Shell::Zsh,
_ => unreachable!(),
};
clap_complete::generate(shell, app, "bender", &mut io::stdout());
Ok(())
}
1 change: 1 addition & 0 deletions src/cmd/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

pub mod checkout;
pub mod clone;
pub mod completion;
pub mod config;
pub mod fusesoc;
pub mod init;
Expand Down

0 comments on commit 3c1749f

Please sign in to comment.