package cmd 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安装完成") } }