Script-Collected/env/init_go.sh

204 lines
4.7 KiB
Bash
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
# ==============================================
# Go 语言安装脚本 for CentOS
# 版本1.3
# 作者:您的名字
# ==============================================
# 启用严格模式
set -euo pipefail
# --------------------------
# 配置区(可根据需要修改)
# --------------------------
GO_VERSION="1.22.0" # 默认安装版本
INSTALL_DIR="/usr/local" # 安装目录
GOPATH="$HOME/go" # GOPATH 设置
PROFILE_FILE="/etc/profile.d/go.sh" # 环境变量文件
# --------------------------
# 颜色定义
# --------------------------
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
NC='\033[0m' # No Color
# --------------------------
# 日志函数
# --------------------------
log_info() {
echo -e "${GREEN}[INFO]${NC} $1"
}
log_warn() {
echo -e "${YELLOW}[WARN]${NC} $1"
}
log_error() {
echo -e "${RED}[ERROR]${NC} $1" >&2
}
# --------------------------
# 检查root权限
# --------------------------
check_root() {
if [ "$(id -u)" -ne 0 ]; then
log_error "请使用root用户或通过sudo运行此脚本"
exit 1
fi
}
# --------------------------
# 安装依赖
# --------------------------
install_deps() {
log_info "正在安装必要依赖..."
yum install -y curl tar gzip wget git make gcc
}
# --------------------------
# 获取最新Go版本
# --------------------------
get_latest_version() {
log_info "正在获取最新Go版本..."
local latest
latest=$(curl -s https://go.dev/VERSION?m=text | head -1)
if [[ "$latest" =~ ^go[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
GO_VERSION="${latest#go}"
log_info "检测到最新版本: ${GO_VERSION}"
else
log_warn "无法获取最新版本,使用默认版本: ${GO_VERSION}"
fi
}
# --------------------------
# 下载Go安装包
# --------------------------
download_go() {
local url="https://dl.google.com/go/go${GO_VERSION}.linux-amd64.tar.gz"
local tmp_file="/tmp/go${GO_VERSION}.tar.gz"
if [ -f "$tmp_file" ]; then
log_info "发现本地缓存安装包,跳过下载"
return
fi
log_info "正在下载Go ${GO_VERSION}..."
if ! curl -fSL --progress-bar -o "$tmp_file" "$url"; then
log_error "下载失败,请检查网络连接或版本是否存在"
exit 1
fi
}
# --------------------------
# 安装Go
# --------------------------
install_go() {
local tmp_file="/tmp/go${GO_VERSION}.tar.gz"
log_info "正在安装Go到 ${INSTALL_DIR}..."
tar -C "$INSTALL_DIR" -xzf "$tmp_file"
rm -f "$tmp_file"
}
# --------------------------
# 配置环境变量
# --------------------------
setup_env() {
log_info "正在配置环境变量..."
# 创建环境变量文件
cat > "$PROFILE_FILE" <<EOF
export GOROOT=${INSTALL_DIR}/go
export GOPATH=${GOPATH}
export PATH=\$GOROOT/bin:\$GOPATH/bin:\$PATH
EOF
# 立即生效
source "$PROFILE_FILE"
# 创建GOPATH目录
mkdir -p "${GOPATH}/"{src,bin,pkg}
}
# --------------------------
# 验证安装
# --------------------------
verify_install() {
log_info "验证安装..."
if ! command -v go &> /dev/null; then
log_error "Go安装失败请检查错误"
exit 1
fi
echo -e "\n${GREEN}Go安装成功${NC}"
echo "============================="
go version
echo "GOROOT: $(go env GOROOT)"
echo "GOPATH: $(go env GOPATH)"
echo "============================="
}
# --------------------------
# 卸载Go
# --------------------------
uninstall_go() {
log_info "正在卸载Go..."
rm -rf "${INSTALL_DIR}/go"
rm -f "$PROFILE_FILE"
log_info "Go已卸载"
}
# --------------------------
# 清理缓存
# --------------------------
clean_cache() {
log_info "正在清理缓存..."
rm -f "/tmp/go${GO_VERSION}.tar.gz"
log_info "缓存已清理"
}
# --------------------------
# 主函数
# --------------------------
main() {
case "$1" in
install)
check_root
install_deps
# 如果想安装最新版本,取消下面注释
# get_latest_version
download_go
install_go
setup_env
verify_install
log_info "安装完成!请重新登录或运行 'source ${PROFILE_FILE}' 使环境变量生效"
;;
uninstall)
check_root
uninstall_go
;;
clean)
clean_cache
;;
*)
echo "用法: $0 {install|uninstall|clean}"
echo " install 安装Go"
echo " uninstall 卸载Go"
echo " clean 清理缓存"
exit 1
;;
esac
}
# --------------------------
# 执行主函数
# --------------------------
main "$@"
exit 0