Files
go/rootfs/root/docker/setup/05-custom.sh
T

221 lines
7.9 KiB
Bash
Raw Normal View History

2026-05-01 06:43:23 -04:00
#!/usr/bin/env bash
# shellcheck shell=bash
# - - - - - - - - - - - - - - - - - - - - - - - - -
2026-06-01 00:12:38 -04:00
##@Version : 202606010000-git
2026-05-01 06:43:23 -04:00
# @@Author : CasjaysDev
# @@Contact : CasjaysDev <docker-admin@casjaysdev.pro>
# @@License : MIT
# @@Copyright : Copyright 2026 CasjaysDev
2026-05-30 02:19:11 -04:00
# @@Created : Fri May 29 10:20:10 PM EDT 2026
2026-05-01 06:43:23 -04:00
# @@File : 05-custom.sh
# @@Description : Install Go latest and Go tooling
2026-06-01 00:12:38 -04:00
# @@Changelog : Use pre-built binaries where available; go install for the rest
# @@TODO : N/A
2026-05-01 06:43:23 -04:00
# @@Other : N/A
# @@Resource : https://go.dev/dl/
2026-05-01 06:43:23 -04:00
# @@Terminal App : yes
# @@sudo/root : yes
# @@Template : templates/dockerfiles/init_scripts/05-custom.sh
# - - - - - - - - - - - - - - - - - - - - - - - - -
# shellcheck disable=SC1001,SC1003,SC2001,SC2003,SC2016,SC2031,SC2090,SC2115,SC2120,SC2155,SC2199,SC2229,SC2317,SC2329
# - - - - - - - - - - - - - - - - - - - - - - - - -
# Set bash options
set -eo pipefail
2026-05-01 06:43:23 -04:00
[ "$DEBUGGER" = "on" ] && echo "Enabling debugging" && set -x$DEBUGGER_OPTIONS
# Force IPv4 for all curl calls in this script — the base image IPv6 routing
# intercepts *.github.com and presents a cert for casjay.in, causing SAN mismatch
printf -- '-4\n' > /root/.curlrc
2026-05-01 06:43:23 -04:00
# - - - - - - - - - - - - - - - - - - - - - - - - -
# Set env variables
exitCode=0
# Installation root for the Go distribution (not GOPATH)
GOINSTALL_DIR="/usr/local/go"
# GOPATH: module cache, pkg index, user-installed binaries (declared VOLUME)
GOPATH_DIR="/usr/local/share/go"
# Baked-in tool binaries land here so they are on the default PATH
GOBIN_DIR="/usr/local/bin"
# Throwaway build cache used only during this image build layer
GOCACHE_BUILD="/tmp/go-build-cache"
2026-05-01 06:43:23 -04:00
# - - - - - - - - - - - - - - - - - - - - - - - - -
2026-06-01 00:12:38 -04:00
# Helpers
# Return the latest release tag from GitHub; retries up to 3 times on transient errors
# (rate-limit 403s are common in parallel multi-platform builds without a token).
# Set GITHUB_TOKEN to raise the authenticated rate limit (5000 req/hr vs 60 req/hr).
2026-06-01 00:12:38 -04:00
_gh_latest() {
local repo="$1"
local filter="${2:-.tag_name}"
2026-06-01 00:21:20 -04:00
local auth_header=""
[ -n "${GITHUB_TOKEN:-}" ] && auth_header="-H Authorization: token ${GITHUB_TOKEN}"
local ver attempt
for attempt in 1 2 3; do
# shellcheck disable=SC2206
ver="$(curl -fsSL ${auth_header:+$auth_header} "https://api.github.com/repos/${repo}/releases/latest" | jq -r "${filter}")"
if [ -n "$ver" ] && [ "$ver" != "null" ]; then
echo "$ver"
return 0
fi
if [ "$attempt" -lt 3 ]; then
echo " rate-limited on ${repo} (attempt ${attempt}/3) — retrying in 60s..." >&2
sleep 60
fi
done
echo "ERROR: could not resolve latest version for ${repo} after 3 attempts" >&2
exit 1
2026-06-01 00:12:38 -04:00
}
2026-06-01 00:21:20 -04:00
# Download a tar.gz asset, find a named binary anywhere inside, install to GOBIN_DIR
2026-06-01 00:12:38 -04:00
_install_tar() {
local url="$1"
local bin="$2"
local tmp
tmp="$(mktemp -d)"
2026-06-01 00:21:20 -04:00
echo "${bin} from ${url##*/}"
2026-06-01 00:12:38 -04:00
curl -fsSL "$url" | tar -C "$tmp" -xz
2026-06-01 00:21:20 -04:00
local found
found="$(find "$tmp" -name "$bin" -type f | head -1)"
if [ -z "$found" ]; then
echo "ERROR: binary '${bin}' not found in archive ${url##*/}" >&2
rm -rf "$tmp"
exit 1
2026-06-01 00:12:38 -04:00
fi
2026-06-01 00:21:20 -04:00
install -m 0755 "$found" "${GOBIN_DIR}/${bin}"
2026-06-01 00:12:38 -04:00
rm -rf "$tmp"
}
2026-06-01 00:21:20 -04:00
# Download a single binary asset directly to GOBIN_DIR
2026-06-01 00:12:38 -04:00
_install_bin() {
local url="$1"
local name="$2"
2026-06-01 00:21:20 -04:00
echo "${name} from ${url##*/}"
2026-06-01 00:12:38 -04:00
curl -fsSL "$url" -o "${GOBIN_DIR}/${name}"
chmod 0755 "${GOBIN_DIR}/${name}"
}
2026-05-01 06:43:23 -04:00
# - - - - - - - - - - - - - - - - - - - - - - - - -
2026-06-01 00:12:38 -04:00
# Architecture detection
2026-05-01 06:43:23 -04:00
2026-06-01 00:21:20 -04:00
# Go convention: amd64 / arm64 / armv6l / 386
case "$(uname -m)" in
x86_64) _GOARCH="amd64" ;;
aarch64) _GOARCH="arm64" ;;
armv7l) _GOARCH="armv6l" ;;
i386|i686) _GOARCH="386" ;;
*)
echo "Unsupported architecture: $(uname -m)" >&2
exit 1
;;
esac
2026-06-01 00:21:20 -04:00
# uname -m verbatim: buf uses x86_64 / aarch64
2026-06-01 00:12:38 -04:00
_UNAME_M="$(uname -m)"
2026-06-01 00:21:20 -04:00
# goreleaser / ko / goose use x86_64 / arm64 (arm64 not aarch64)
2026-06-01 00:12:38 -04:00
if [ "$_UNAME_M" = "aarch64" ]; then
_ARCH_GLIBC="arm64"
else
_ARCH_GLIBC="$_UNAME_M"
fi
# - - - - - - - - - - - - - - - - - - - - - - - - -
# Install Go distribution
_GO_VERSION="$(curl -fsSL 'https://go.dev/dl/?mode=json' | jq -r '.[0].version')"
echo "Installing ${_GO_VERSION} (linux/${_GOARCH})"
rm -rf "${GOINSTALL_DIR}"
curl -fsSL "https://dl.google.com/go/${_GO_VERSION}.linux-${_GOARCH}.tar.gz" | tar -C /usr/local -xz
ln -sf "${GOINSTALL_DIR}/bin/go" "${GOBIN_DIR}/go"
ln -sf "${GOINSTALL_DIR}/bin/gofmt" "${GOBIN_DIR}/gofmt"
export GOPATH="${GOPATH_DIR}"
export GOBIN="${GOBIN_DIR}"
export PATH="${GOINSTALL_DIR}/bin:${PATH}"
export GOCACHE="${GOCACHE_BUILD}"
export CGO_ENABLED="0"
export GOTOOLCHAIN="auto"
mkdir -p "${GOPATH_DIR}/pkg/mod" "${GOPATH_DIR}/cache" "${GOPATH_DIR}/bin"
2026-06-01 00:12:38 -04:00
# - - - - - - - - - - - - - - - - - - - - - - - - -
# Pre-built binary installs (fast — no compilation)
echo "Installing pre-built tools"
2026-06-01 00:21:20 -04:00
# goreleaser — release automation (Linux/x86_64 or Linux/arm64)
2026-06-01 00:12:38 -04:00
_GR_VER="$(_gh_latest goreleaser/goreleaser)"
_install_tar \
"https://github.com/goreleaser/goreleaser/releases/download/${_GR_VER}/goreleaser_Linux_${_ARCH_GLIBC}.tar.gz" \
"goreleaser"
2026-06-01 00:21:20 -04:00
# golangci-lint — meta-linter (official installer handles its own version resolution)
2026-06-01 00:12:38 -04:00
curl -fsSL https://raw.githubusercontent.com/golangci/golangci-lint/HEAD/install.sh \
| sh -s -- -b "${GOBIN_DIR}" latest
2026-06-01 00:21:20 -04:00
# staticcheck — standalone advanced static analyser (linux_amd64 / linux_arm64)
_SC_VER="$(_gh_latest dominikh/go-tools)"
2026-06-01 00:12:38 -04:00
_install_tar \
"https://github.com/dominikh/go-tools/releases/download/${_SC_VER}/staticcheck_linux_${_GOARCH}.tar.gz" \
"staticcheck"
2026-06-01 00:21:20 -04:00
# gofumpt — stricter formatter; asset name includes version: gofumpt_v0.x.y_linux_amd64
2026-06-01 00:12:38 -04:00
_GF_VER="$(_gh_latest mvdan/gofumpt)"
_install_bin \
2026-06-01 00:21:20 -04:00
"https://github.com/mvdan/gofumpt/releases/download/${_GF_VER}/gofumpt_${_GF_VER}_linux_${_GOARCH}" \
2026-06-01 00:12:38 -04:00
"gofumpt"
2026-06-01 00:21:20 -04:00
# gotestsum — structured test runner; asset uses amd64/arm64 (not x86_64)
2026-06-01 00:12:38 -04:00
_GTS_VER="$(_gh_latest gotestyourself/gotestsum)"
_GTS_TAG="${_GTS_VER#v}"
_install_tar \
2026-06-01 00:21:20 -04:00
"https://github.com/gotestyourself/gotestsum/releases/download/${_GTS_VER}/gotestsum_${_GTS_TAG}_linux_${_GOARCH}.tar.gz" \
2026-06-01 00:12:38 -04:00
"gotestsum"
2026-06-01 00:21:20 -04:00
# ko — build Go container images without a Dockerfile (Linux/x86_64 or Linux/arm64)
2026-06-01 00:12:38 -04:00
_KO_VER="$(_gh_latest google/ko)"
_KO_TAG="${_KO_VER#v}"
_install_tar \
"https://github.com/google/ko/releases/download/${_KO_VER}/ko_${_KO_TAG}_Linux_${_ARCH_GLIBC}.tar.gz" \
"ko"
2026-06-01 00:21:20 -04:00
# air — live-reload dev server; asset: air_1.x.y_linux_amd64 (version without v prefix)
2026-06-01 00:12:38 -04:00
_AIR_VER="$(_gh_latest air-verse/air)"
2026-06-01 00:21:20 -04:00
_AIR_TAG="${_AIR_VER#v}"
2026-06-01 00:12:38 -04:00
_install_bin \
2026-06-01 00:21:20 -04:00
"https://github.com/air-verse/air/releases/download/${_AIR_VER}/air_${_AIR_TAG}_linux_${_GOARCH}" \
2026-06-01 00:12:38 -04:00
"air"
2026-06-01 00:21:20 -04:00
# buf — modern protobuf toolchain; uses x86_64/aarch64 (uname -m convention)
2026-06-01 00:12:38 -04:00
_BUF_VER="$(_gh_latest bufbuild/buf)"
_install_bin \
"https://github.com/bufbuild/buf/releases/download/${_BUF_VER}/buf-Linux-${_UNAME_M}" \
"buf"
2026-06-01 00:21:20 -04:00
# goose — DB migration runner (linux_x86_64 or linux_arm64)
2026-06-01 00:12:38 -04:00
_GOOSE_VER="$(_gh_latest pressly/goose)"
_install_bin \
"https://github.com/pressly/goose/releases/download/${_GOOSE_VER}/goose_linux_${_ARCH_GLIBC}" \
"goose"
# go install tools (goimports, stringer, gopls, govulncheck, dlv, gops, benchstat,
# wire, mockgen, protoc-gen-go, protoc-gen-go-grpc) are cross-compiled natively on
# the build platform in the Dockerfile go-tools stage and copied to /usr/local/bin
# before this script runs — no QEMU-emulated compilation needed here.
2026-06-01 00:12:38 -04:00
# Strip the module download cache and ephemeral build cache from this layer
go clean -modcache
go clean -cache
rm -rf "${GOCACHE_BUILD}"
2026-05-24 21:46:18 -04:00
# - - - - - - - - - - - - - - - - - - - - - - - - -
# Set the exit code
exitCode=$?
# - - - - - - - - - - - - - - - - - - - - - - - - -
exit $exitCode
2026-05-01 06:43:23 -04:00
# - - - - - - - - - - - - - - - - - - - - - - - - -
# ex: ts=2 sw=2 et filetype=sh
# - - - - - - - - - - - - - - - - - - - - - - - - -