Files
go/rootfs/usr/local/bin/go-workflow
T
jason 373136fead Add GO_PROD=1 production build mode to go-workflow
When GO_PROD=1 is set at runtime, go build receives -trimpath and
-ldflags="-s -w" to strip source paths, symbol tables, and DWARF debug
info from compiled binaries. Applied to go build only — go test is
unaffected so stack traces remain readable during development.
Usage: docker run --env GO_PROD=1 --rm -v "$(pwd)":/app casjaysdev/go
- rootfs/usr/local/bin/go-workflow: add BUILD_FLAGS array populated when
GO_PROD=1; pass flags to go build step only; print mode notice when active

rootfs/usr/local/bin/go-workflow
2026-06-21 09:51:21 -04:00

57 lines
1.9 KiB
Bash
Executable File

#!/usr/bin/env bash
# shellcheck shell=bash
# Default Go workflow: tidy → fmt → vet → test → build
# Runs automatically when `docker run casjaysdev/go` is called with no args.
# Working directory is expected to be a Go module root (go.mod must exist).
set -euo pipefail
# Resolve the working directory — prefer /app if mounted, else cwd
WORK_DIR="${GOWORKDIR:-${PWD}}"
cd "$WORK_DIR"
# Require a go.mod so we fail fast with a clear message instead of cryptic go errors
if [ ! -f "go.mod" ]; then
echo "Error: no go.mod found in ${WORK_DIR}" >&2
echo "Mount your project with: docker run --rm -v \"\$(pwd)\":/app casjaysdev/go" >&2
exit 1
fi
MODULE="$(awk '/^module /{print $2}' go.mod)"
echo ""
echo "▶ Go workflow: ${MODULE}"
echo " Working dir: ${WORK_DIR}"
echo ""
run_step() {
local label="$1"; shift
echo "── ${label}"
"$@"
echo ""
}
# Production mode: GO_PROD=1 strips binaries and removes local source paths.
# Enabled via: docker run --env GO_PROD=1 ...
# -trimpath removes all local file system paths from the compiled binary
# -ldflags=-s strips the symbol table; -w strips DWARF debug info
# Both reduce binary size and avoid leaking build-host paths into the output.
# Applied to go build only — not go test — so stack traces stay readable.
BUILD_FLAGS=()
if [ "${GO_PROD:-0}" = "1" ]; then
echo " (production mode: -trimpath -ldflags=-s -w)"
BUILD_FLAGS=(-trimpath -ldflags="-s -w")
fi
# 1. Sync module graph and go.sum before anything reads them
run_step "go mod tidy" go mod tidy
# 2. Format all Go source files in place
run_step "gofmt -w ." gofmt -w .
# 3. Catch suspicious constructs
run_step "go vet ./..." go vet ./...
# 4. Run tests — fail fast before wasting time on a build
run_step "go test ./..." go test ./...
# 5. Build all main packages; output lands alongside source in each package dir
run_step "go build ./..." go build "${BUILD_FLAGS[@]}" ./...
echo "✅ Done."