初始化项目

This commit is contained in:
zeaslity
2025-03-27 16:09:20 +08:00
parent e09a32d1e8
commit fc2d585489
709 changed files with 516391 additions and 0 deletions

173
agent-wdd/cmd/root.go Normal file
View File

@@ -0,0 +1,173 @@
package cmd
import (
"agent-wdd/config"
"fmt"
"io"
"os"
"strings"
"github.com/spf13/cobra"
)
var rootCmd = &cobra.Command{
Use: "wdd",
Short: "wdd应用程序是wdd封装的NB工具",
Long: `使用golang的强大特性加上 WDD 的高超技术打造一个方便、高效、适用于各种LINUX系统的瑞士军刀工具!
尽情享用! 尽情享用! 尽情享用!`,
DisableAutoGenTag: true,
CompletionOptions: cobra.CompletionOptions{
DisableDefaultCmd: true,
DisableNoDescFlag: true,
DisableDescriptions: true,
HiddenDefaultCmd: true,
},
}
func init() {
// 初始化配置
cobra.OnInitialize(config.InitConfig)
}
func Execute() {
// 1. base命令
baseCmd := &cobra.Command{
Use: "base",
Short: "服务器基础操作",
}
addBaseSubcommands(baseCmd)
// 2. zsh命令
zshCmd := &cobra.Command{
Use: "zsh",
Short: "zsh相关的内容",
}
addZshSubcommands(zshCmd)
// 3. proxy命令
proxyCmd := &cobra.Command{
Use: "proxy",
Short: "主机代理相关的内容",
}
addProxySubcommands(proxyCmd)
// 4. acme命令
acmeCmd := &cobra.Command{
Use: "acme",
Short: "acme相关的内容",
}
addAcmeSubcommands(acmeCmd)
// 5. wdd命令
wddCmd := &cobra.Command{
Use: "wdd",
Short: "WDD相关操作",
}
addWddSubcommands(wddCmd)
// 6. security命令
securityCmd := &cobra.Command{
Use: "security",
Short: "安全相关操作",
}
// 7. info命令
infoCmd := &cobra.Command{
Use: "info",
Short: "主机信息",
}
addInfoSubcommands(infoCmd)
// 8. version命令
versionCmd := &cobra.Command{
Use: "version",
Short: "打印版本信息",
Run: func(cmd *cobra.Command, args []string) {
// 实现version逻辑
fmt.Println("版本信息: 1.0.0")
},
}
// 9. config命令
configCmd := &cobra.Command{
Use: "config",
Short: "配置文件管理",
}
addConfigSubcommands(configCmd)
// 10. download命令
downloadCmd := &cobra.Command{
Use: "download",
Short: "文件下载,直接下载 [url] [dest_path]",
}
addDownloadSubcommands(downloadCmd)
helpCmd := &cobra.Command{
Use: "help",
Short: "帮助信息",
Run: func(cmd *cobra.Command, args []string) {
// 在你的命令执行函数中
// cmd.Root().SetHelpFunc(func(c *cobra.Command, s []string) {
// // 直接使用默认帮助模板
// c.Usage()
// })
// 自定义实现 打印全部的命令
printAllCommands(cmd.Root(), os.Stdout)
},
}
// 添加所有根命令
rootCmd.AddCommand(
baseCmd,
zshCmd,
proxyCmd,
acmeCmd,
wddCmd,
securityCmd,
infoCmd,
versionCmd,
configCmd,
downloadCmd,
helpCmd,
)
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
}
}
func printAllCommands(cmd *cobra.Command, writer io.Writer) {
// 打印命令标题
fmt.Fprintf(writer, "All available commands:\n\n")
// 递归打印命令树
var traverse func(*cobra.Command, int)
traverse = func(c *cobra.Command, depth int) {
if c.Hidden || c.Deprecated != "" {
return
}
// 生成命令前缀缩进
indent := strings.Repeat(" ", depth)
// 打印当前命令信息
fmt.Fprintf(writer, "%s%-20s%s\n",
indent,
c.Name(),
strings.TrimSpace(c.Short))
// 递归子命令
for _, subCmd := range c.Commands() {
traverse(subCmd, depth+1)
}
}
// 从根命令开始遍历
for _, c := range cmd.Commands() {
traverse(c, 0)
}
}