From fcfb3cc10c1b50532fd99c69740029cf5b55f345 Mon Sep 17 00:00:00 2001 From: yoyo Date: Wed, 3 Dec 2025 18:11:54 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B7=B3=E8=BF=87=E5=B7=B2=E5=AE=89=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- install.sh | 48 +++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 41 insertions(+), 7 deletions(-) diff --git a/install.sh b/install.sh index 6b7096b..5c476a1 100755 --- a/install.sh +++ b/install.sh @@ -469,15 +469,39 @@ install_go_from_official() { # 安装 Go 环境 install_go() { - echo -e "${BLUE}安装 Go 环境...${NC}" + echo -e "${BLUE}检查 Go 环境...${NC}" - # 先检查是否已安装 + # 先检查是否已安装且可用 if command -v go > /dev/null 2>&1; then GO_VERSION=$(go version 2>/dev/null | head -1) - echo -e "${GREEN}✓ Go 已安装: ${GO_VERSION}${NC}" - return 0 + if [ -n "$GO_VERSION" ]; then + echo -e "${GREEN}✓ Go 已安装: ${GO_VERSION}${NC}" + # 检查 Go 版本是否可用(尝试运行 go version) + if go version > /dev/null 2>&1; then + echo -e "${BLUE}Go 环境正常,跳过安装流程${NC}" + # 确保 GOPROXY 已配置(如果未配置则添加) + if ! grep -q "GOPROXY" /etc/profile 2>/dev/null; then + echo -e "${BLUE}配置 Go 代理环境变量...${NC}" + echo '' | sudo tee -a /etc/profile > /dev/null + echo '# Go 代理配置(使用国内镜像加速)' | sudo tee -a /etc/profile > /dev/null + echo 'export GOPROXY=https://goproxy.cn,direct' | sudo tee -a /etc/profile > /dev/null + echo 'export GOSUMDB=off' | sudo tee -a /etc/profile > /dev/null + fi + # 设置当前会话的环境变量 + export GOPROXY=${GOPROXY:-https://goproxy.cn,direct} + export GOSUMDB=${GOSUMDB:-off} + return 0 + else + echo -e "${YELLOW}⚠ Go 已安装但无法正常运行,尝试重新安装...${NC}" + fi + else + echo -e "${YELLOW}⚠ Go 已安装但无法获取版本信息,尝试重新安装...${NC}" + fi fi + # 如果 Go 未安装或不可用,开始安装流程 + echo -e "${BLUE}开始安装 Go 环境...${NC}" + # 尝试从系统包管理器安装 local install_success=false @@ -627,10 +651,20 @@ uninstall_service() { build_from_source() { echo -e "${BLUE}从源码编译安装节点端...${NC}" - # 检查 Go 环境 + # 检查 Go 环境(如果已安装则跳过安装流程) if ! command -v go > /dev/null 2>&1; then echo -e "${BLUE}未检测到 Go 环境,开始安装...${NC}" install_go + else + # Go 已安装,验证是否可用 + GO_VERSION=$(go version 2>/dev/null | head -1 || echo "") + if [ -n "$GO_VERSION" ] && go version > /dev/null 2>&1; then + echo -e "${GREEN}✓ Go 已安装: ${GO_VERSION}${NC}" + echo -e "${BLUE}跳过 Go 安装流程,直接使用现有环境${NC}" + else + echo -e "${YELLOW}⚠ Go 已安装但无法正常运行,尝试重新安装...${NC}" + install_go + fi fi # 确保 Go 在 PATH 中(如果从官网安装的) @@ -638,7 +672,7 @@ build_from_source() { export PATH=$PATH:/usr/local/go/bin fi - # 检查 Go 版本 + # 再次检查 Go 版本(确保安装成功) GO_VERSION=$(go version 2>/dev/null | head -1 || echo "") if [ -z "$GO_VERSION" ]; then echo -e "${RED}无法获取 Go 版本信息${NC}" @@ -646,7 +680,7 @@ build_from_source() { exit 1 fi - echo -e "${BLUE}检测到 Go 版本: ${GO_VERSION}${NC}" + echo -e "${BLUE}使用 Go 版本: ${GO_VERSION}${NC}" # 确定 Go 的完整路径(用于 sudo 执行) GO_BIN=$(command -v go)