92 lines
2.3 KiB
Bash
92 lines
2.3 KiB
Bash
#!/bin/bash
|
||
export PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:$PATH
|
||
|
||
source <(curl -f -s --connect-timeout 10 --retry 3 https://dl.acepanel.net/public.sh)
|
||
if [ $? -ne 0 ]; then
|
||
echo "Download public.sh failed, please check the network or try again later."
|
||
exit 1
|
||
fi
|
||
|
||
slug=${1}
|
||
version=${2}
|
||
go_path="${setup_path}/server/go/${slug}"
|
||
in_china=$(curl --retry 2 -m 10 -L -k https://www.qualcomm.cn/cdn-cgi/trace 2>/dev/null | grep -qx 'loc=CN' && echo "true" || echo "false")
|
||
|
||
# 架构映射
|
||
case ${ARCH} in
|
||
x86_64)
|
||
GO_ARCH="amd64"
|
||
;;
|
||
aarch64)
|
||
GO_ARCH="arm64"
|
||
;;
|
||
esac
|
||
|
||
# 准备安装目录
|
||
rm -rf ${go_path}
|
||
mkdir -p ${go_path}
|
||
cd ${go_path}
|
||
|
||
# 下载Go安装包
|
||
GO_TARBALL="go${version}.linux-${GO_ARCH}.tar.gz"
|
||
|
||
if ${in_china}; then
|
||
GO_URL="https://mirrors.aliyun.com/golang/${GO_TARBALL}"
|
||
echo "Downloading ${GO_TARBALL} from Aliyun mirror..."
|
||
else
|
||
GO_URL="https://dl.google.com/go/${GO_TARBALL}"
|
||
echo "Downloading ${GO_TARBALL} from Google..."
|
||
fi
|
||
|
||
aria2c -x8 \
|
||
-U "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/144.0.0.0 Safari/537.36" \
|
||
-o ${GO_TARBALL} \
|
||
${GO_URL}
|
||
if [ $? -ne 0 ]; then
|
||
rm -rf ${go_path}
|
||
error "Failed to download Go ${version}"
|
||
fi
|
||
|
||
# 解压
|
||
tar -zxf ${GO_TARBALL}
|
||
if [ $? -ne 0 ]; then
|
||
rm -rf ${go_path}
|
||
error "Failed to extract Go ${version}"
|
||
fi
|
||
|
||
# Go解压后会在go目录下,移动到当前目录
|
||
mv go/* .
|
||
rmdir go
|
||
rm -f ${GO_TARBALL}
|
||
|
||
# 验证安装
|
||
if [ ! -f "${go_path}/bin/go" ]; then
|
||
rm -rf ${go_path}
|
||
error "Go ${version} installation failed"
|
||
fi
|
||
|
||
# 设置软链接
|
||
ln -sf ${go_path}/bin/go /usr/local/bin/go${slug}
|
||
ln -sf ${go_path}/bin/gofmt /usr/local/bin/gofmt${slug}
|
||
[ ! -f /usr/local/bin/go ] && ln -sf ${go_path}/bin/go /usr/local/bin/go
|
||
[ ! -f /usr/local/bin/gofmt ] && ln -sf ${go_path}/bin/gofmt /usr/local/bin/gofmt
|
||
|
||
# 国内设置GOPROXY
|
||
if ${in_china}; then
|
||
go env -w GO111MODULE=on
|
||
go env -w GOPROXY=https://goproxy.cn,direct
|
||
fi
|
||
|
||
# 创建环境变量配置
|
||
cat >${go_path}/env.sh <<EOF
|
||
export GOROOT=${go_path}
|
||
export PATH=\$GOROOT/bin:\$PATH
|
||
EOF
|
||
chmod 644 ${go_path}/env.sh
|
||
|
||
echo -e $HR
|
||
echo "Go ${version} installation successful"
|
||
echo "Go path: ${go_path}"
|
||
echo "To use this version, run: source ${go_path}/env.sh"
|
||
echo -e $HR
|