Skip to content

Commit

Permalink
Add a wasmtime completion subcommand
Browse files Browse the repository at this point in the history
This commit adds a new subcommand to the `wasmtime` CLI which can be
used to generate shell completions for the `wasmtime` version that is
installed. This is relatively easy to do with the `clap_complete` crate
which is also used with various other clap-based CLIs in Rust to
generate completions.
  • Loading branch information
alexcrichton committed Sep 25, 2024
1 parent 1e37274 commit 5c3446b
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 3 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.

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ wasmtime-wasi-keyvalue = { workspace = true, optional = true }
wasmtime-wasi-threads = { workspace = true, optional = true }
wasmtime-wasi-http = { workspace = true, optional = true }
clap = { workspace = true }
clap_complete = { workspace = true, optional = true }
anyhow = { workspace = true, features = ['std'] }
target-lexicon = { workspace = true }
once_cell = { workspace = true }
Expand Down Expand Up @@ -288,6 +289,7 @@ windows-sys = "0.59.0"
env_logger = "0.11.5"
log = { version = "0.4.8", default-features = false }
clap = { version = "4.5.17", default-features = false, features = ["std", "derive"] }
clap_complete = "4.4.7"
hashbrown = { version = "0.14", default-features = false }
capstone = "0.12.0"
once_cell = { version = "1.12.0", default-features = false }
Expand Down Expand Up @@ -360,6 +362,7 @@ default = [
"serve",
"wast",
"config",
"completion",

# On-by-default WASI features
"wasi-nn",
Expand Down Expand Up @@ -456,6 +459,7 @@ run = [
"dep:tokio",
"wasmtime-cli-flags/async",
]
completion = ["dep:clap_complete"]

[[test]]
name = "host_segfault"
Expand Down
42 changes: 39 additions & 3 deletions src/bin/wasmtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
//! See `wasmtime --help` for usage.

use anyhow::Result;
use clap::Parser;
use clap::{CommandFactory, Parser};

/// Wasmtime WebAssembly Runtime
#[derive(Parser, PartialEq)]
#[derive(Parser)]
#[command(
name = "wasmtime",
version = version(),
Expand Down Expand Up @@ -52,7 +52,7 @@ fn version() -> &'static str {
option_env!("WASMTIME_VERSION_INFO").unwrap_or(env!("CARGO_PKG_VERSION"))
}

#[derive(Parser, PartialEq)]
#[derive(Parser)]
enum Subcommand {
/// Runs a WebAssembly module
#[cfg(feature = "run")]
Expand Down Expand Up @@ -81,6 +81,10 @@ enum Subcommand {
/// Runs a WebAssembly test script file
#[cfg(feature = "wast")]
Wast(wasmtime_cli::commands::WastCommand),

/// Generate shell completions for the `wasmtime` CLI
#[cfg(feature = "completion")]
Completion(CompletionCommand),
}

impl Wasmtime {
Expand Down Expand Up @@ -112,10 +116,42 @@ impl Wasmtime {

#[cfg(feature = "wast")]
Subcommand::Wast(c) => c.execute(),

#[cfg(feature = "completion")]
Subcommand::Completion(c) => c.execute(),
}
}
}

/// Generate shell completion scripts for this CLI.
///
/// Shells have different paths for their completion scripts. Please refer to
/// their documentation. For example, to generate completions for the fish
/// shell, run the following command below:
///
/// wasmtime completion fish > ~/.config/fish/completions/wasmtime.fish
///
/// For a shell like zsh you can add this to your .zshrc or startup scripts:
///
/// eval "$(wasmtime completion zsh)"
#[derive(Parser)]
#[cfg(feature = "completion")]
pub struct CompletionCommand {
/// The shell to generate completions for.
shell: clap_complete::Shell,
}

#[cfg(feature = "completion")]
impl CompletionCommand {
pub fn execute(&self) -> Result<()> {
let mut cmd = Wasmtime::command();
let cli_name = cmd.get_name().to_owned();

clap_complete::generate(self.shell, &mut cmd, cli_name, &mut std::io::stdout());
Ok(())
}
}

fn main() -> Result<()> {
return Wasmtime::parse().execute();
}
Expand Down
6 changes: 6 additions & 0 deletions supply-chain/audits.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3814,6 +3814,12 @@ user-id = 6743 # Ed Page (epage)
start = "2023-03-28"
end = "2025-09-20"

[[trusted.clap_complete]]
criteria = "safe-to-deploy"
user-id = 6743 # Ed Page (epage)
start = "2021-12-31"
end = "2025-09-25"

[[trusted.clap_derive]]
criteria = "safe-to-deploy"
user-id = 6743 # Ed Page (epage)
Expand Down
7 changes: 7 additions & 0 deletions supply-chain/imports.lock
Original file line number Diff line number Diff line change
Expand Up @@ -815,6 +815,13 @@ user-id = 6743
user-login = "epage"
user-name = "Ed Page"

[[publisher.clap_complete]]
version = "4.5.28"
when = "2024-09-17"
user-id = 6743
user-login = "epage"
user-name = "Ed Page"

[[publisher.clap_derive]]
version = "4.3.12"
when = "2023-07-14"
Expand Down

0 comments on commit 5c3446b

Please sign in to comment.