From d206ac7ab002f8dc9c26925db3a201e427fd666e Mon Sep 17 00:00:00 2001 From: Alistair Date: Thu, 8 Feb 2024 14:27:18 +0000 Subject: [PATCH] feat(cli): add `completions` command to jstz --- Cargo.lock | 10 ++++++++++ crates/jstz_cli/Cargo.toml | 1 + crates/jstz_cli/src/completions.rs | 12 ++++++++++++ crates/jstz_cli/src/main.rs | 9 +++++++++ 4 files changed, 32 insertions(+) create mode 100644 crates/jstz_cli/src/completions.rs diff --git a/Cargo.lock b/Cargo.lock index c672b7df3..15b29f6d0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -757,6 +757,15 @@ dependencies = [ "strsim", ] +[[package]] +name = "clap_complete" +version = "4.4.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "abb745187d7f4d76267b37485a65e0149edd0e91a4cfcdd3f27524ad86cee9f3" +dependencies = [ + "clap", +] + [[package]] name = "clap_derive" version = "4.4.7" @@ -2045,6 +2054,7 @@ dependencies = [ "boa_gc", "bs58", "clap", + "clap_complete", "console", "crossterm", "daemonize", diff --git a/crates/jstz_cli/Cargo.toml b/crates/jstz_cli/Cargo.toml index b50343989..c775d107b 100644 --- a/crates/jstz_cli/Cargo.toml +++ b/crates/jstz_cli/Cargo.toml @@ -56,6 +56,7 @@ env_logger = "0.11.1" log = "0.4.20" dialoguer = "0.11.0" prettytable = "0.10.0" +clap_complete = "4.4.10" [[bin]] name = "jstz" diff --git a/crates/jstz_cli/src/completions.rs b/crates/jstz_cli/src/completions.rs new file mode 100644 index 000000000..fed45d04d --- /dev/null +++ b/crates/jstz_cli/src/completions.rs @@ -0,0 +1,12 @@ +use std::io; + +use clap::CommandFactory; +use clap_complete::Shell; + +use crate::{error::Result, Command}; + +pub fn exec(shell: Shell) -> Result<()> { + let cmd = &mut Command::command(); + clap_complete::generate(shell, cmd, "jstz", &mut io::stdout()); + Ok(()) +} diff --git a/crates/jstz_cli/src/main.rs b/crates/jstz_cli/src/main.rs index 926d75e76..f51d1089d 100644 --- a/crates/jstz_cli/src/main.rs +++ b/crates/jstz_cli/src/main.rs @@ -2,6 +2,7 @@ use clap::Parser; mod account; mod bridge; +mod completions; mod config; mod deploy; mod docs; @@ -15,6 +16,7 @@ mod sandbox; mod term; mod utils; +use clap_complete::Shell; use config::Config; use error::Result; use log::debug; @@ -38,6 +40,12 @@ enum Command { /// 🔑 Interact with jstz's key-value store. #[command(subcommand)] Kv(kv::Command), + /// 🐚 Generates shell completions. + Completions { + /// The shell to generate completions for + #[arg(long, short)] + shell: Shell, + }, /// 🚀 Deploys a smart function to jstz. Deploy { /// Function code. @@ -90,6 +98,7 @@ enum Command { async fn exec(command: Command) -> Result<()> { match command { Command::Docs => docs::exec(), + Command::Completions { shell } => completions::exec(shell), Command::Sandbox(sandbox_command) => sandbox::exec(sandbox_command).await, Command::Bridge(bridge_command) => bridge::exec(bridge_command), Command::Account(account_command) => account::exec(account_command).await,