Files
Dot-Zsh/Scripts/go.sh
2026-06-02 12:21:03 +01:00

173 lines
4.5 KiB
Bash

#!/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'
export GVM_ROOT="$HOME/.programming/go"
BOOTSTRAP_GO="$GVM_ROOT/bootstrap"
GO_BOOTSTRAP_VERSION="1.20.5"
OS_NAME="$(uname -s)"
ARCH="$(uname -m)"
case "$ARCH" in
x86_64)
GO_BOOTSTRAP_ARCH="amd64"
;;
aarch64|arm64)
GO_BOOTSTRAP_ARCH="arm64"
;;
*)
echo -e "${RED} ERROR:${NC} Unsupported architecture for Go bootstrap: $ARCH"
exit 1
;;
esac
case "$OS_NAME" in
Linux)
GO_BOOTSTRAP_OS="linux"
;;
Darwin)
GO_BOOTSTRAP_OS="darwin"
;;
*)
echo -e "${RED} ERROR:${NC} Unsupported operating system for Go bootstrap: $OS_NAME"
exit 1
;;
esac
GO_BOOTSTRAP_FILENAME="go${GO_BOOTSTRAP_VERSION}.${GO_BOOTSTRAP_OS}-${GO_BOOTSTRAP_ARCH}.tar.gz"
GO_BOOTSTRAP_URL="https://go.dev/dl/${GO_BOOTSTRAP_FILENAME}"
GO_RELEASE_METADATA_URL="https://go.dev/dl/?mode=json&include=all"
sha256_file() {
local file_path="$1"
if command -v sha256sum >/dev/null 2>&1; then
sha256sum "$file_path" | awk '{print $1}'
elif command -v shasum >/dev/null 2>&1; then
shasum -a 256 "$file_path" | awk '{print $1}'
else
echo -e "${RED} ERROR:${NC} No SHA256 tool found (need sha256sum or shasum)."
exit 1
fi
}
get_go_release_sha256() {
python3 - "$GO_RELEASE_METADATA_URL" "$GO_BOOTSTRAP_FILENAME" <<'PY'
import json
import sys
import urllib.request
metadata_url = sys.argv[1]
target_filename = sys.argv[2]
with urllib.request.urlopen(metadata_url, timeout=30) as response:
releases = json.load(response)
for release in releases:
for file_info in release.get("files", []):
if file_info.get("filename") == target_filename:
print(file_info.get("sha256", ""))
raise SystemExit(0)
raise SystemExit(1)
PY
}
download_and_verify_go_bootstrap() {
local destination_path="$1"
local expected_sha256=""
local actual_sha256=""
local temp_tarball=""
temp_tarball="$(mktemp "${TMPDIR:-/tmp}/go-bootstrap.XXXXXX")"
cleanup_go_bootstrap_download() {
rm -f "$temp_tarball"
}
trap cleanup_go_bootstrap_download RETURN
echo -e "${BLUE} LOG:${YELLOW} Downloading pinned Bootstrap Go ${GO_BOOTSTRAP_VERSION}...${NC}"
curl -fsSL "$GO_BOOTSTRAP_URL" -o "$temp_tarball"
expected_sha256="$(get_go_release_sha256)"
if [ -z "$expected_sha256" ]; then
echo -e "${RED} ERROR:${NC} Could not find official SHA256 for ${GO_BOOTSTRAP_FILENAME}."
exit 1
fi
actual_sha256="$(sha256_file "$temp_tarball")"
if [ "$actual_sha256" != "$expected_sha256" ]; then
echo -e "${RED} ERROR:${NC} Bootstrap Go checksum mismatch for ${GO_BOOTSTRAP_FILENAME}."
echo -e "${RED} ERROR:${NC} Expected: ${expected_sha256}"
echo -e "${RED} ERROR:${NC} Actual: ${actual_sha256}"
exit 1
fi
tar -C "$destination_path" -xzf "$temp_tarball" --strip-components=1
}
echo -e "${BLUE} LOG:${YELLOW} Setting up Go and GVM in ${GVM_ROOT}...${NC}"
if [ ! -d "$GVM_ROOT/scripts" ]; then
echo -e "${BLUE} LOG:${YELLOW} Cloning GVM...${NC}"
git clone https://github.com/moovweb/gvm.git "$GVM_ROOT"
rm -rf "$GVM_ROOT/.git"
echo -e "${BLUE} LOG:${YELLOW} Configuring GVM scripts...${NC}"
cp "$GVM_ROOT/scripts/gvm-default" "$GVM_ROOT/scripts/gvm"
python3 - "$GVM_ROOT/scripts/gvm" "$GVM_ROOT" <<'PY'
from pathlib import Path
import sys
path = Path(sys.argv[1])
root = sys.argv[2]
text = path.read_text()
path.write_text(text.replace('GVM_ROOT="$HOME/.gvm"', f'GVM_ROOT="{root}"', 1))
PY
echo -e "${GREEN} SUCCESS:${NC} GVM cloned and configured."
else
echo -e "${GREEN} SKIP:${NC} GVM already installed."
fi
if [ ! -d "$BOOTSTRAP_GO" ]; then
mkdir -p "$BOOTSTRAP_GO"
download_and_verify_go_bootstrap "$BOOTSTRAP_GO"
echo -e "${GREEN} SUCCESS:${NC} Bootstrap Go installed."
else
echo -e "${GREEN} SKIP:${NC} Bootstrap Go already exists."
fi
export PATH="$BOOTSTRAP_GO/bin:$PATH"
export GOROOT_BOOTSTRAP="$BOOTSTRAP_GO"
set +e
source "$GVM_ROOT/scripts/gvm"
set -e
if ! command -v gvm &> /dev/null; then
echo -e "${RED} ERROR:${NC} GVM failed to load."
exit 1
fi
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."