Enhance Zsh and Config Commands, Update Network Configuration

- Implemented comprehensive Zsh installation command with multiple network scenarios
- Added 'config show' subcommand to display agent configuration
- Updated version command to print version information
- Modified Network configuration to clarify internet connectivity status
- Improved download utility with additional file existence checks
- Updated agent-wdd rules and documentation
This commit is contained in:
zeaslity
2025-02-27 14:20:05 +08:00
parent 16c041e3eb
commit 72bc56b5e5
8 changed files with 196 additions and 39 deletions

View File

@@ -1,13 +1,10 @@
---
description:
globs:
---
---
description: 构建agent-wdd的特定上下文的规则
globs: *.go
---
# 你是一个精通golang的编程大师熟练掌握github.com/spf13/cobra框架能够构建出非常现代的cli工具
@.cursorignore 请忽略这些目录下的文件
# 整个项目的架构结构如下
1. base 服务器基础操作 相关的功能存放于 [Base.go](mdc:agent-wdd/cmd/Base.go)
@@ -28,8 +25,7 @@ globs: *.go
6. selinux 关闭本机的selinux相关内容
7. firewall 关闭本机的防火墙相关的设置
8. sysconfig 修改主机sysconfig相关的内容
2. zsh zsh相关的内容
1. cn 参数如果有此参数则从跟国内安装zsh
2. zsh zsh相关的内容 自动安装配置zsh [Zsh.go](mdc:agent-wdd/cmd/Zsh.go)
3. proxy 主机代理相关的内容
1. xray xray相关的内容
1. install 安装最新版本的xray

View File

@@ -1,7 +1,30 @@
package cmd
import "github.com/spf13/cobra"
import (
"agent-wdd/config"
"agent-wdd/log"
"agent-wdd/utils"
"github.com/spf13/cobra"
)
func addConfigSubcommands(cmd *cobra.Command) {
showConfigCmd := &cobra.Command{
Use: "show",
Short: "显示agent运行配置",
Run: func(cmd *cobra.Command, args []string) {
log.Info("显示agent运行配置")
content := utils.ReadAllContentFromFile(config.WddConfigFilePath)
utils.BeautifulPrintListWithTitle(content, "agent运行配置")
log.Info("显示agent运行配置完成")
},
}
cmd.AddCommand(showConfigCmd)
}

View File

@@ -1,7 +1,10 @@
package cmd
import (
"agent-wdd/log"
"agent-wdd/utils"
"fmt"
"github.com/spf13/cobra"
)
@@ -16,16 +19,26 @@ func addDownloadSubcommands(cmd *cobra.Command) {
},
}
unproxyCmd := &cobra.Command{
Use: "unproxy [url] [dest]",
Short: "不使用代理下载",
Args: cobra.ExactArgs(2),
Run: func(cmd *cobra.Command, args []string) {
fmt.Printf("Downloading without proxy: %s -> %s\n", args[0], args[1])
},
cmd.Run = func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
log.Error("请输入下载地址")
return
} else if len(args) == 1 {
args = append(args, ".")
}
cmd.AddCommand(proxyCmd, unproxyCmd)
log.Info("Downloading without proxy: %s -> %s\n", args[0], args[1])
downloadOk, resultLog := utils.DownloadFile(args[0], args[1])
if !downloadOk {
log.Error("下载失败: %s", resultLog)
} else {
log.Info("下载成功: %s", args[1])
}
}
cmd.AddCommand(proxyCmd)
}
// 根据需求补充其他子命令的添加函数...

View File

@@ -1,7 +1,146 @@
package cmd
import "github.com/spf13/cobra"
import (
"agent-wdd/config"
"agent-wdd/log"
"agent-wdd/op"
"agent-wdd/utils"
"github.com/spf13/cobra"
)
func addZshSubcommands(cmd *cobra.Command) {
cmd.Run = func(cmd *cobra.Command, args []string) {
log.Info("安装zsh")
// 初始化安装命令
utils.CreateFolder("/root/wdd")
if config.CanConnectInternet() <= 1 {
log.Error("无法连接互联网无法安装zsh")
return
}
// 判定config
if config.ConfigCache.Agent.Network.Public.IPv4 == "" {
network := config.ConfigCache.Agent.Network
network.Gather()
}
// 下载并安装zsh和git
ok := op.AgentPackOperator.Install([]string{"git", "zsh"})
if !ok {
log.Error("安装git和zsh失败, 无法继续后面的的安装!")
return
}
// 清理残留
utils.RemoveFolderComplete("/root/.oh-my-zsh")
utils.RemoveFolderComplete("/root/wdd/zsh-install.sh")
utils.RemoveFolderComplete("/root/wdd/oh-my-zsh-plugins-list.txt")
ohMyZshInstallUrl := ""
if config.ConfigCache.Agent.Network.Internet == 7 {
ohMyZshInstallUrl = "https://gitea.107421.xyz/zeaslity/ohmyzsh/raw/branch/master/tools/install.sh"
} else {
ohMyZshInstallUrl = "https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh"
}
downloadOk, resultLog := utils.DownloadFile(ohMyZshInstallUrl, "/root/wdd/zsh-install.sh")
if !downloadOk {
log.Error("下载zsh安装脚本失败: %v", resultLog)
return
}
op.SingleLineCommandExecutor([]string{
"chmod",
"+x",
"/root/wdd/zsh-install.sh",
})
installZshCommand := []string{
"/bin/bash",
"/root/wdd/zsh-install.sh",
}
if config.ConfigCache.Agent.Network.Internet == 7 {
installZshCommand = append(installZshCommand,
"REMOTE=https://gitea.107421.xyz/zeaslity/ohmyzsh.git",
)
}
op.SingleLineCommandExecutor(installZshCommand)
log.Info("安装zsh完成, 开始安装插件!")
pluginsHardCodeUrl := []string{
"git clone https://gitee.com/wangl-cc/zsh-autosuggestions.git ~/.oh-my-zsh/plugins/zsh-autosuggestions",
"git clone https://gitee.com/xiaoqqya/zsh-syntax-highlighting.git ~/.oh-my-zsh/plugins/zsh-syntax-highlighting",
}
pluginsHardCodeOutsideUrl := []string{
"git clone https://github.com/zsh-users/zsh-autosuggestions ~/.oh-my-zsh/plugins/zsh-autosuggestions",
"git clone https://github.com/zsh-users/zsh-syntax-highlighting.git ~/.oh-my-zsh/plugins/zsh-syntax-highlighting",
}
pluginsListUrl := []string{
"https://gitea.107421.xyz/zeaslity/ohmyzsh/src/branch/master/plugins/command-not-found/command-not-found.plugin.zsh",
"https://gitea.107421.xyz/zeaslity/ohmyzsh/src/branch/master/plugins/autojump/autojump.plugin.zsh",
"https://gitea.107421.xyz/zeaslity/ohmyzsh/src/branch/master/plugins/themes/themes.plugin.zsh",
}
if config.ConfigCache.Agent.Network.Internet == 7 {
log.Info("使用gitea镜像安装插件!")
pluginsHardCodeUrl = append(pluginsHardCodeUrl,
"wget -c -i https://b2.107421.xyz/oh-my-zsh-plugins-list.txt -P /root/.oh-my-zsh/plugins/",
)
} else {
log.Info("使用github安装插件!")
}
// 下载插件
if config.ConfigCache.Agent.Network.Internet == 7 {
// 执行硬编码命令, 安装插件
for _, command := range pluginsHardCodeUrl {
op.HardCodeCommandExecutor(command)
}
} else {
// 执行硬编码命令, 安装插件 国外
for _, command := range pluginsHardCodeOutsideUrl {
op.HardCodeCommandExecutor(command)
}
}
// 下载插件
for _, url := range pluginsListUrl {
downloadOk, resultLog := utils.DownloadFile(url, "/root/.oh-my-zsh/plugins/")
if !downloadOk {
log.Error("下载插件失败: %v", resultLog)
}
}
// 修改ZSH配置
utils.FindAndReplaceContentInFile("/root/.zshrc", "robbyrussell", "agnoster")
utils.FindAndReplaceContentInFile("/root/.zshrc", "# DISABLE_AUTO_UPDATE=\"true\"", "DISABLE_AUTO_UPDATE=\"true\"")
utils.FindAndReplaceContentInFile("/root/.zshrc", "plugins=(git)", "plugins=(git zsh-autosuggestions zsh-syntax-highlighting command-not-found z themes)")
//
op.SingleLineCommandExecutor([]string{
"source",
"/root/.zshrc",
})
op.SingleLineCommandExecutor([]string{
"chsh",
"-s",
"/bin/zsh",
})
op.SingleLineCommandExecutor([]string{
"zsh",
})
log.Info("Zsh安装完成")
}
}

View File

@@ -2,8 +2,8 @@ package cmd
import (
"agent-wdd/config"
"agent-wdd/log"
"fmt"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
@@ -93,10 +93,7 @@ func Execute() {
Short: "打印版本信息",
Run: func(cmd *cobra.Command, args []string) {
// 实现version逻辑
log.Debug("来自王达达的礼物 !")
log.Info("来自王达达的礼物 !")
log.Warning("来自王达达的礼物 !")
log.Error("来自王达达的礼物 !")
fmt.Println("版本信息: 1.0.0")
},
}

View File

@@ -62,7 +62,7 @@ type OS struct {
}
type Network struct {
Internet int `yaml:"internet"`
Internet int `yaml:"internet",comment:"是否能够上网 外网为9 国内为7 不能上网为1"`
Public PublicInfo `yaml:"public"`
Interfaces []Interface `yaml:"interfaces"`
}

View File

@@ -39,25 +39,9 @@ func (network *Network) Gather() {
//获取本机网卡相关的内容
}
// CanConnectInternet 判定主机能够上网
// CanConnectInternet 判定主机能够上网 外网为9 国内为7 不能上网为1
func CanConnectInternet() int {
// 初始化 ConfigCache
if ConfigCache == nil {
ConfigCache = &Config{
TimeStamp: "",
ModifiedTimes: 0,
Agent: Agent{
OS: OS{},
Network: Network{},
CPU: CPU{},
Mem: Memory{},
Swap: Swap{},
Disks: []Disk{},
},
}
}
// 读取 config 文件,判定能否上网
internetCode := ConfigCache.Agent.Network.Internet

View File

@@ -39,5 +39,10 @@ func DownloadFile(url string, path string) (bool, string) {
return false, fmt.Sprintf("写入文件失败: %v", err)
}
// 检查文件是否存在
if !FileExistAndNotNull(path) {
return false, fmt.Sprintf("文件下载失败: 文件为空 => %s", path)
}
return true, fmt.Sprintf("文件下载成功: %s", path)
}