This commit is contained in:
MangoPig
2025-12-05 19:01:00 +00:00
parent afd6481ec6
commit cf670488ac
9 changed files with 211 additions and 89 deletions

View File

@@ -1,46 +1,78 @@
#!/bin/bash
# Path: scripts/go.sh
set -e
BLUE='\033[1;34m'
YELLOW='\033[1;33m'
GREEN='\033[1;32m'
RED='\033[1;31m'
NC='\033[0m'
echo -e "${BLUE} LOG:${YELLOW} Setting up Go (GVM)...${NC}"
# 1. Define Custom GVM Root
export GVM_ROOT="$HOME/.programming/go"
BOOTSTRAP_GO="$GVM_ROOT/bootstrap"
# 1. Install GVM dependencies
unalias cd 2>/dev/null || true
echo -e "${BLUE} LOG:${YELLOW} Setting up Go and GVM in ${GVM_ROOT}...${NC}"
# 2. Install GVM if missing
if [ ! -d "$HOME/.gvm" ]; then
bash < <(curl -s -S -L https://raw.githubusercontent.com/moovweb/gvm/master/binscripts/gvm-installer)
# 2. Install GVM (Manual Method)
# Check for 'scripts' dir instead of '.git', because we are going to delete .git
if [ ! -d "$GVM_ROOT/scripts" ]; then
echo -e "${BLUE} LOG:${YELLOW} Cloning GVM...${NC}"
git clone https://github.com/moovweb/gvm.git "$GVM_ROOT"
# CRITICAL FIX: Remove git metadata so GVM treats this as a valid installation
rm -rf "$GVM_ROOT/.git"
# Generate the GVM executable script from default
echo -e "${BLUE} LOG:${YELLOW} Configuring GVM scripts...${NC}"
cp "$GVM_ROOT/scripts/gvm-default" "$GVM_ROOT/scripts/gvm"
sed -i "s|^GVM_ROOT=.*|GVM_ROOT=\"$GVM_ROOT\"|" "$GVM_ROOT/scripts/gvm"
echo -e "${GREEN} SUCCESS:${NC} GVM cloned and configured."
else
echo -e "${GREEN} SKIP:${NC} GVM already installed."
fi
# 3. Load GVM
[[ -s "$HOME/.gvm/scripts/gvm" ]] && source "$HOME/.gvm/scripts/gvm"
# 4. Install Go
# Use a known stable version that exists. 1.24.11 is speculative. 1.24.0 is real.
TARGET_GO="go1.24.0"
if command -v gvm &> /dev/null; then
if [[ $(gvm list) != *"$TARGET_GO"* ]]; then
echo -e "${BLUE} LOG:${YELLOW} Installing $TARGET_GO...${NC}"
# Try Binary install first (Fastest/Safest)
gvm install "$TARGET_GO" -B || {
echo -e "${RED} LOG: Binary failed. Bootstrapping via go1.22.9...${NC}"
gvm install go1.22.9 -B
gvm use go1.22.9
gvm install "$TARGET_GO"
}
gvm use "$TARGET_GO" --default
echo -e "${GREEN} LOG: Go setup complete.$(go version)${NC}"
else
echo -e "${GREEN} LOG: $TARGET_GO already installed.${NC}"
fi
# 3. Install Universal Bootstrap Go
if [ ! -d "$BOOTSTRAP_GO" ]; then
echo -e "${BLUE} LOG:${YELLOW} Downloading Bootstrap Go...${NC}"
mkdir -p "$BOOTSTRAP_GO"
wget -q https://go.dev/dl/go1.20.5.linux-amd64.tar.gz -O /tmp/go-bootstrap.tar.gz
tar -C "$BOOTSTRAP_GO" -xzf /tmp/go-bootstrap.tar.gz --strip-components=1
rm /tmp/go-bootstrap.tar.gz
echo -e "${GREEN} SUCCESS:${NC} Bootstrap Go installed."
else
echo -e "${RED} ERROR: GVM failed to load.${NC}"
echo -e "${GREEN} SKIP:${NC} Bootstrap Go already exists."
fi
# 4. Configure Environment for Installation
export PATH="$BOOTSTRAP_GO/bin:$PATH"
export GOROOT_BOOTSTRAP="$BOOTSTRAP_GO"
# Source GVM
set +e
source "$GVM_ROOT/scripts/gvm"
set -e
# Verify GVM loaded correctly
if ! command -v gvm &> /dev/null; then
echo -e "${RED} ERROR:${NC} GVM failed to load."
exit 1
fi
fi
# 5. Install Target Version
TARGET_VER="go1.24.11"
if ! gvm list | grep -q "$TARGET_VER"; then
echo -e "${BLUE} LOG:${YELLOW} Installing ${TARGET_VER}...${NC}"
gvm install "$TARGET_VER" --prefer-binary
fi
gvm use "$TARGET_VER" --default
echo -e "${GREEN} SUCCESS:${NC} Go setup completed."