Fix go version

This commit is contained in:
2026-05-27 14:37:13 -03:00
parent 6f7fdef746
commit 096a8275be
3 changed files with 120 additions and 61 deletions

View File

@@ -28,17 +28,75 @@ fi
need_cmd() { command -v "$1" >/dev/null 2>&1; }
rand_hex() { openssl rand -hex "$1"; }
# The bridge source is intentionally Go 1.13-compatible so old VPS images
# such as Ubuntu 20.04 can compile it with their distro package.
# If the distro Go is older than 1.13, install a known-good official Go.
GO_MIN_MAJOR=1
GO_MIN_MINOR=13
GO_VERSION="${GO_VERSION:-1.21.13}"
GO_BIN=""
go_version_ok() {
if ! need_cmd go; then
return 1
fi
ver="$(go version 2>/dev/null | awk '{print $3}' | sed 's/^go//; s/[^0-9.].*$//')"
major="$(printf '%s' "$ver" | cut -d. -f1)"
minor="$(printf '%s' "$ver" | cut -d. -f2)"
case "$major:$minor" in
*[!0-9:]*|:*) return 1 ;;
esac
if [ "$major" -gt "$GO_MIN_MAJOR" ]; then
return 0
fi
if [ "$major" -eq "$GO_MIN_MAJOR" ] && [ "$minor" -ge "$GO_MIN_MINOR" ]; then
return 0
fi
return 1
}
install_official_go() {
arch="$(uname -m)"
case "$arch" in
x86_64|amd64) goarch="amd64" ;;
aarch64|arm64) goarch="arm64" ;;
armv6l|armv7l) goarch="armv6l" ;;
i386|i686) goarch="386" ;;
*) echo "Unsupported CPU architecture for automatic Go install: $arch"; exit 1 ;;
esac
url="https://go.dev/dl/go${GO_VERSION}.linux-${goarch}.tar.gz"
tmp="/tmp/go${GO_VERSION}.linux-${goarch}.tar.gz"
echo "Installing Go ${GO_VERSION} from ${url} ..."
curl -fsSL "$url" -o "$tmp"
rm -rf /usr/local/go
tar -C /usr/local -xzf "$tmp"
ln -sf /usr/local/go/bin/go /usr/local/bin/go
ln -sf /usr/local/go/bin/gofmt /usr/local/bin/gofmt
GO_BIN="/usr/local/go/bin/go"
}
export DEBIAN_FRONTEND=noninteractive
if need_cmd apt-get; then
apt-get update -y
apt-get install -y ca-certificates curl wget git openssl build-essential
if ! need_cmd go; then
apt-get install -y golang-go
apt-get install -y golang-go || true
fi
elif need_cmd yum; then
yum install -y ca-certificates curl wget git openssl gcc make golang
yum install -y ca-certificates curl wget git openssl gcc make golang || yum install -y ca-certificates curl wget git openssl gcc make
elif need_cmd dnf; then
dnf install -y ca-certificates curl wget git openssl gcc make golang
dnf install -y ca-certificates curl wget git openssl gcc make golang || dnf install -y ca-certificates curl wget git openssl gcc make
fi
if go_version_ok; then
GO_BIN="$(command -v go)"
else
install_official_go
fi
if ! "$GO_BIN" version >/dev/null 2>&1; then
echo "Go installation failed. Cannot build dragoncore-bridge."
exit 1
fi
mkdir -p "$INSTALL_DIR" "$CONFIG_DIR"
@@ -56,7 +114,7 @@ else
fi
cd "$INSTALL_DIR/src"
go build -trimpath -ldflags "-s -w" -o "$BIN" .
"$GO_BIN" build -trimpath -ldflags "-s -w" -o "$BIN" .
chmod 755 "$BIN"
USER_NAME="admin"