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:
Copilot
2026-01-11 18:41:02 +08:00
committed by GitHub
parent b590700540
commit bd8f01fb26
5 changed files with 142 additions and 12 deletions
+1
View File
@@ -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"
+8 -4
View File
@@ -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;
}
+8 -3
View File
@@ -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
View File
@@ -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(),
);
}