mirror of
https://github.com/EasyTier/EasyTier.git
synced 2026-05-06 17:59:11 +00:00
Add Nushell completion script generation support (#1756)
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
This commit is contained in:
@@ -135,6 +135,7 @@ clap = { version = "4.5.30", features = [
|
||||
"env",
|
||||
] }
|
||||
clap_complete = { version = "4.5.55" }
|
||||
clap_complete_nushell = { version = "4.5.10" }
|
||||
|
||||
async-recursion = "1.0.5"
|
||||
|
||||
|
||||
@@ -23,12 +23,11 @@ use crate::{
|
||||
rpc_service::ApiRpcServer,
|
||||
tunnel::PROTO_PORT_OFFSET,
|
||||
utils::{init_logger, setup_panic_handler},
|
||||
web_client,
|
||||
web_client, ShellType,
|
||||
};
|
||||
use anyhow::Context;
|
||||
use cidr::IpCidr;
|
||||
use clap::{CommandFactory, Parser};
|
||||
use clap_complete::Shell;
|
||||
use rust_i18n::t;
|
||||
use tokio::io::AsyncReadExt;
|
||||
|
||||
@@ -126,7 +125,7 @@ struct Cli {
|
||||
rpc_portal_options: RpcPortalOptions,
|
||||
|
||||
#[clap(long, help = t!("core_clap.generate_completions").to_string())]
|
||||
gen_autocomplete: Option<Shell>,
|
||||
gen_autocomplete: Option<ShellType>,
|
||||
|
||||
#[clap(long, help = t!("core_clap.check_config").to_string())]
|
||||
check_config: bool,
|
||||
@@ -1380,7 +1379,12 @@ pub async fn main() -> ExitCode {
|
||||
|
||||
if let Some(shell) = cli.gen_autocomplete {
|
||||
let mut cmd = Cli::command();
|
||||
crate::print_completions(shell, &mut cmd, "easytier-core");
|
||||
if let Some(shell) = shell.to_shell() {
|
||||
crate::print_completions(shell, &mut cmd, "easytier-core");
|
||||
} else {
|
||||
// Handle Nushell
|
||||
crate::print_nushell_completions(&mut cmd, "easytier-core");
|
||||
}
|
||||
return ExitCode::SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
@@ -10,8 +10,8 @@ use std::{
|
||||
use anyhow::Context;
|
||||
use cidr::Ipv4Inet;
|
||||
use clap::{Args, CommandFactory, Parser, Subcommand};
|
||||
use clap_complete::Shell;
|
||||
use dashmap::DashMap;
|
||||
use easytier::ShellType;
|
||||
use humansize::format_size;
|
||||
use rust_i18n::t;
|
||||
use service_manager::*;
|
||||
@@ -126,7 +126,7 @@ enum SubCommand {
|
||||
#[command(about = "manage logger configuration")]
|
||||
Logger(LoggerArgs),
|
||||
#[command(about = t!("core_clap.generate_completions").to_string())]
|
||||
GenAutocomplete { shell: Shell },
|
||||
GenAutocomplete { shell: ShellType },
|
||||
}
|
||||
|
||||
#[derive(clap::ValueEnum, Debug, Clone, PartialEq)]
|
||||
@@ -1950,7 +1950,12 @@ async fn main() -> Result<(), Error> {
|
||||
},
|
||||
SubCommand::GenAutocomplete { shell } => {
|
||||
let mut cmd = Cli::command();
|
||||
easytier::print_completions(shell, &mut cmd, "easytier-cli");
|
||||
if let Some(shell) = shell.to_shell() {
|
||||
easytier::print_completions(shell, &mut cmd, "easytier-cli");
|
||||
} else {
|
||||
// Handle Nushell
|
||||
easytier::print_nushell_completions(&mut cmd, "easytier-cli");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+33
-1
@@ -3,7 +3,7 @@
|
||||
use std::io;
|
||||
|
||||
use clap::Command;
|
||||
use clap_complete::Generator;
|
||||
use clap_complete::{Generator, Shell};
|
||||
|
||||
mod arch;
|
||||
mod gateway;
|
||||
@@ -30,6 +30,38 @@ mod tests;
|
||||
pub const VERSION: &str = common::constants::EASYTIER_VERSION;
|
||||
rust_i18n::i18n!("locales", fallback = "en");
|
||||
|
||||
#[derive(clap::ValueEnum, Debug, Clone, PartialEq)]
|
||||
pub enum ShellType {
|
||||
Bash,
|
||||
Elvish,
|
||||
Fish,
|
||||
Powershell,
|
||||
Zsh,
|
||||
Nu,
|
||||
}
|
||||
|
||||
impl ShellType {
|
||||
pub fn to_shell(&self) -> Option<Shell> {
|
||||
match self {
|
||||
ShellType::Bash => Some(Shell::Bash),
|
||||
ShellType::Elvish => Some(Shell::Elvish),
|
||||
ShellType::Fish => Some(Shell::Fish),
|
||||
ShellType::Powershell => Some(Shell::PowerShell),
|
||||
ShellType::Zsh => Some(Shell::Zsh),
|
||||
ShellType::Nu => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn print_completions<G: Generator>(generator: G, cmd: &mut Command, bin_name: &str) {
|
||||
clap_complete::generate(generator, cmd, bin_name, &mut io::stdout());
|
||||
}
|
||||
|
||||
pub fn print_nushell_completions(cmd: &mut Command, bin_name: &str) {
|
||||
clap_complete::generate(
|
||||
clap_complete_nushell::Nushell,
|
||||
cmd,
|
||||
bin_name,
|
||||
&mut io::stdout(),
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user