跳过已安装

This commit is contained in:
2025-12-03 18:11:54 +08:00
parent ad2c5e9a0d
commit fcfb3cc10c

View File

@@ -469,14 +469,38 @@ install_go_from_official() {
# 安装 Go 环境 # 安装 Go 环境
install_go() { install_go() {
echo -e "${BLUE}安装 Go 环境...${NC}" echo -e "${BLUE}检查 Go 环境...${NC}"
# 先检查是否已安装 # 先检查是否已安装且可用
if command -v go > /dev/null 2>&1; then if command -v go > /dev/null 2>&1; then
GO_VERSION=$(go version 2>/dev/null | head -1) GO_VERSION=$(go version 2>/dev/null | head -1)
if [ -n "$GO_VERSION" ]; then
echo -e "${GREEN}✓ Go 已安装: ${GO_VERSION}${NC}" echo -e "${GREEN}✓ Go 已安装: ${GO_VERSION}${NC}"
return 0 # 检查 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 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 local install_success=false
@@ -627,10 +651,20 @@ uninstall_service() {
build_from_source() { build_from_source() {
echo -e "${BLUE}从源码编译安装节点端...${NC}" echo -e "${BLUE}从源码编译安装节点端...${NC}"
# 检查 Go 环境 # 检查 Go 环境(如果已安装则跳过安装流程)
if ! command -v go > /dev/null 2>&1; then if ! command -v go > /dev/null 2>&1; then
echo -e "${BLUE}未检测到 Go 环境,开始安装...${NC}" echo -e "${BLUE}未检测到 Go 环境,开始安装...${NC}"
install_go 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 fi
# 确保 Go 在 PATH 中(如果从官网安装的) # 确保 Go 在 PATH 中(如果从官网安装的)
@@ -638,7 +672,7 @@ build_from_source() {
export PATH=$PATH:/usr/local/go/bin export PATH=$PATH:/usr/local/go/bin
fi fi
# 检查 Go 版本 # 再次检查 Go 版本(确保安装成功)
GO_VERSION=$(go version 2>/dev/null | head -1 || echo "") GO_VERSION=$(go version 2>/dev/null | head -1 || echo "")
if [ -z "$GO_VERSION" ]; then if [ -z "$GO_VERSION" ]; then
echo -e "${RED}无法获取 Go 版本信息${NC}" echo -e "${RED}无法获取 Go 版本信息${NC}"
@@ -646,7 +680,7 @@ build_from_source() {
exit 1 exit 1
fi fi
echo -e "${BLUE}检测到 Go 版本: ${GO_VERSION}${NC}" echo -e "${BLUE}使用 Go 版本: ${GO_VERSION}${NC}"
# 确定 Go 的完整路径(用于 sudo 执行) # 确定 Go 的完整路径(用于 sudo 执行)
GO_BIN=$(command -v go) GO_BIN=$(command -v go)