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

Add subcommands to generate shell completions and manual pages #4683

Merged
merged 2 commits into from
May 14, 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
27 changes: 27 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion lib/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ bytesize = "1.0"
cfg-if = "1.0"
tempfile = "3.6.0"
serde = { version = "1.0.147", features = ["derive"] }
dirs = { version = "4.0" }
dirs = "4.0"
serde_json = { version = "1.0" }
target-lexicon = { version = "0.12", features = ["std"] }
wasmer-config = { version = "0.2.0", path = "../config" }
Expand Down Expand Up @@ -227,6 +227,9 @@ tokio-tungstenite = { version = "0.20.1", features = [
mac_address = { version = "1.1.5", optional = true }
tun-tap = { version = "0.1.3", features = ["tokio"], optional = true }

clap_complete = "4.5.2"
clap_mangen = "0.2.20"

# NOTE: Must use different features for clap because the "color" feature does not
# work on wasi due to the anstream dependency not compiling.
[target.'cfg(not(target_family = "wasm"))'.dependencies]
Expand Down
2 changes: 1 addition & 1 deletion lib/cli/src/commands/app/delete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use is_terminal::IsTerminal;
use super::util::AppIdentOpts;
use crate::{commands::AsyncCliCommand, opts::ApiOpts};

/// Show an app.
/// Delete an existing Edge app
#[derive(clap::Parser, Debug)]
pub struct CmdAppDelete {
#[clap(flatten)]
Expand Down
2 changes: 1 addition & 1 deletion lib/cli/src/commands/app/get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::{
opts::{ApiOpts, ItemFormatOpts},
};

/// Show an app.
/// Retrieve detailed informations about an app
#[derive(clap::Parser, Debug)]
pub struct CmdAppGet {
#[clap(flatten)]
Expand Down
2 changes: 1 addition & 1 deletion lib/cli/src/commands/app/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::{
opts::{ApiOpts, ListFormatOpts},
};

/// List apps.
/// List apps belonging to a namespace
#[derive(clap::Parser, Debug)]
pub struct CmdAppList {
#[clap(flatten)]
Expand Down
2 changes: 1 addition & 1 deletion lib/cli/src/commands/app/logs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub enum LogStreamArg {
Stderr,
}

/// Show an app.
/// Retrieve the logs of an app
#[derive(clap::Parser, Debug)]
pub struct CmdAppLogs {
#[clap(flatten)]
Expand Down
34 changes: 34 additions & 0 deletions lib/cli/src/commands/gen_completions.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use super::WasmerCmd;
use clap::CommandFactory;
use clap_complete::{generate, Shell};
use std::fs::OpenOptions;

#[derive(Debug, Clone, clap::Parser)]
pub struct CmdGenCompletions {
/// The shell to generate the autocompletions script for.
pub shell: Shell,

/// Where to store the generated file(s) to. Defaults to stdout.
#[clap(long)]
pub out: Option<String>,
}

impl CmdGenCompletions {
pub fn execute(&self) -> anyhow::Result<()> {
let mut cmd = WasmerCmd::command();

let name = cmd.get_name().to_string();
if let Some(out) = &self.out {
let mut f = OpenOptions::new()
.truncate(true)
.create(true)
.write(true)
.open(out)?;
generate(self.shell, &mut cmd, name, &mut f);
Ok(())
} else {
generate(self.shell, &mut cmd, name, &mut std::io::stdout());
Ok(())
}
}
}
26 changes: 26 additions & 0 deletions lib/cli/src/commands/gen_manpage.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use super::WasmerCmd;
use anyhow::Context;
use clap::CommandFactory;
use clap_mangen::generate_to;
use std::path::PathBuf;

lazy_static::lazy_static! {
static ref DEFAULT_MAN_DIR_PATH: PathBuf = dirs::data_dir().unwrap_or_default().join("man");
}

#[derive(Debug, Clone, clap::Parser)]
pub struct CmdGenManPage {
/// Where to store the generated file(s) to.
#[clap(long, default_value = DEFAULT_MAN_DIR_PATH.as_os_str())]
pub out: PathBuf,
}

impl CmdGenManPage {
pub fn execute(&self) -> anyhow::Result<()> {
let cmd = WasmerCmd::command();
generate_to(cmd, &self.out).context(format!(
"While generating the man page(s) to {}",
self.out.display()
))
}
}
14 changes: 13 additions & 1 deletion lib/cli/src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ mod create_obj;
pub(crate) mod domain;
#[cfg(feature = "static-artifact-create")]
mod gen_c_header;
mod gen_completions;
mod gen_manpage;
mod init;
mod inspect;
#[cfg(feature = "journal")]
Expand Down Expand Up @@ -115,6 +117,8 @@ impl WasmerCmd {
}

match cmd {
Some(Cmd::GenManPage(cmd)) => cmd.execute(),
xdoardo marked this conversation as resolved.
Show resolved Hide resolved
Some(Cmd::GenCompletions(cmd)) => cmd.execute(),
Some(Cmd::Run(options)) => options.execute(output),
Some(Cmd::SelfUpdate(options)) => options.execute(),
Some(Cmd::Cache(cache)) => cache.execute(),
Expand Down Expand Up @@ -220,7 +224,7 @@ enum Cmd {
#[clap(name = "publish")]
Publish(crate::commands::package::publish::PackagePublish),

/// Wasmer cache
/// Manage the local Wasmer cache
Cache(Cache),

/// Validate a WebAssembly binary
Expand Down Expand Up @@ -363,6 +367,14 @@ enum Cmd {
/// Manage DNS records
#[clap(subcommand, alias = "domains")]
Domain(crate::commands::domain::CmdDomain),

/// Generate autocompletion for different shells
#[clap(name = "gen-completions")]
GenCompletions(crate::commands::gen_completions::CmdGenCompletions),

/// Generate man pages
#[clap(name = "gen-man", hide = true)]
GenManPage(crate::commands::gen_manpage::CmdGenManPage),
}

fn is_binfmt_interpreter() -> bool {
Expand Down
1 change: 0 additions & 1 deletion lib/cli/src/commands/package/publish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ impl PackagePublish {
quiet: self.quiet,
package_namespace: self.package_namespace.clone(),
timeout: self.timeout,
bump: self.bump,
non_interactive: self.non_interactive,
wait: self.wait,
package_path: self.package_path.clone(),
Expand Down
4 changes: 0 additions & 4 deletions lib/cli/src/commands/package/push.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,6 @@ pub struct PackagePush {
#[clap(long, default_value = "5m")]
pub timeout: humantime::Duration,

/// Whether or not the patch field of the version of the package - if any - should be bumped.
#[clap(long, conflicts_with = "version")]
pub bump: bool,

/// Do not prompt for user input.
#[clap(long, default_value_t = !std::io::stdin().is_terminal())]
pub non_interactive: bool,
Expand Down
2 changes: 1 addition & 1 deletion lib/cli/src/commands/run/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ pub struct Run {
#[clap(short, long, aliases = &["command", "invoke", "command-name"])]
entrypoint: Option<String>,
/// Generate a coredump at this path if a WebAssembly trap occurs
#[clap(name = "COREDUMP PATH", long)]
#[clap(name = "COREDUMP_PATH", long)]
coredump_on_trap: Option<PathBuf>,
/// The file, URL, or package to run.
#[clap(value_parser = PackageSource::infer)]
Expand Down
Loading