Files
go/rootfs/usr/local/bin/go-workflow
T
jason f910225d3b Add MODE env var and document GOWORKDIR, GOOS, GOARCH
- rootfs/usr/local/bin/go-workflow: replace GO_PROD=1 check with MODE
resolution; MODE=prod|production enables -trimpath -ldflags=-s -w on
go build; MODE=dev|devel|development is the explicit development path;
GO_PROD=1 remains a legacy alias when MODE is unset; unknown MODE values
warn to stderr and default to development; print active mode on every run
- README.md: add GOWORKDIR, GOOS, GOARCH, MODE, GO_PROD to env vars table
- README.md: update production mode section to use MODE=prod; note GO_PROD
as legacy alias

README.md
rootfs/usr/local/bin/go-workflow
2026-06-21 16:44:18 -04:00

69 lines
2.2 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 ""
}
# Resolve build mode from MODE or the legacy GO_PROD=1 flag.
# MODE=prod|production → strip binary (-trimpath -ldflags=-s -w); applied to go build only
# MODE=dev|devel|development → default; full debug info, readable stack traces
# GO_PROD=1 is kept for backwards compatibility and takes effect when MODE is unset.
_IS_PROD=0
case "${MODE:-}" in
prod | production) _IS_PROD=1 ;;
dev | devel | development) _IS_PROD=0 ;;
"")
[ "${GO_PROD:-0}" = "1" ] && _IS_PROD=1
;;
*)
echo "Warning: unknown MODE '${MODE}' — expected prod|production|dev|devel|development; defaulting to development" >&2
;;
esac
BUILD_FLAGS=()
if [ "$_IS_PROD" = "1" ]; then
echo " (mode: production — -trimpath -ldflags=-s -w)"
BUILD_FLAGS=(-trimpath -ldflags="-s -w")
else
echo " (mode: development)"
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."