From 797373159a48e2074432b5b7ef07c1285354e3c6 Mon Sep 17 00:00:00 2001 From: Peter Sheehan <145384599+peter-bread@users.noreply.github.com> Date: Fri, 2 Jan 2026 17:10:59 +0000 Subject: [PATCH 01/12] build: use `-buildvcs` to embed version info and sync make+goreleaser --- .goreleaser.yaml | 9 +++++++++ Makefile | 26 ++++++++++++++++++++++---- cmd/root.go | 6 +++--- main.go | 43 +++++++++++++++++++++++++++++++++++++++++-- 4 files changed, 75 insertions(+), 9 deletions(-) diff --git a/.goreleaser.yaml b/.goreleaser.yaml index 2c43208..1370f8f 100644 --- a/.goreleaser.yaml +++ b/.goreleaser.yaml @@ -9,12 +9,21 @@ before: hooks: - go mod tidy +gomod: + proxy: true + builds: - env: - CGO_ENABLED=0 goos: - darwin - linux + flags: + - -v + - -buildvcs=true + - -trimpath + ldflags: + - -s -w archives: - formats: [tar.gz] diff --git a/Makefile b/Makefile index 3387fcd..6442f3f 100644 --- a/Makefile +++ b/Makefile @@ -1,13 +1,31 @@ .PHONY: build test cover clean install goreleaser -BUILD_DIR = build +override VALID_BUILD_MODES := release debug -VERSION ?= $(shell git describe --tags --dirty --always) -LDFLAGS ?= -X main.version=$(VERSION) +# Release builds by default, must explicitly set BUILD_MODE=debug for dev builds. +BUILD_MODE ?= release + +ifneq ($(filter $(BUILD_MODE),$(VALID_BUILD_MODES)),$(BUILD_MODE)) +$(error Invalid BUILD_MODE '$(BUILD_MODE)'; must be one of: [$(VALID_BUILD_MODES)]) +endif + +BUILD_DIR = build +CGO_ENABLED = 0 + +LDFLAGS_COMMON := +GOFLAGS_COMMON := -v -buildvcs=true + +ifeq ($(BUILD_MODE), release) +LDFLAGS := -s -w $(LDFLAGS_COMMON) +GOFLAGS := -trimpath $(GOFLAGS_COMMON) +else ifeq ($(BUILD_MODE), debug) +LDFLAGS := $(LDFLAGS_COMMON) +GOFLAGS := $(GOFLAGS_COMMON) +endif build: mkdir -p $(BUILD_DIR) - go build -o $(BUILD_DIR) -v -ldflags "$(LDFLAGS)" + CGO_ENABLED=$(CGO_ENABLED) go build $(GOFLAGS) -o "$(BUILD_DIR)" -ldflags "$(LDFLAGS)" test: go test -v ./... diff --git a/cmd/root.go b/cmd/root.go index ee11208..c0a9bd2 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -24,6 +24,7 @@ THE SOFTWARE. package cmd import ( + "fmt" "os" "github.com/peter-bread/gamon3/v2/cmd/hook" @@ -52,9 +53,8 @@ func Execute() { } } -func SetVersion(version, commit, date string) { - // TODO: Use `commit` and `date` in version output (MAYBE). - rootCmd.Version = version +func SetVersion(version, commit, date, os, arch string) { + rootCmd.Version = fmt.Sprintf("%s %s-%s\nCommit: %s (%s)", version, os, arch, commit, date) } func init() { diff --git a/main.go b/main.go index c0fbb13..39b7cdb 100644 --- a/main.go +++ b/main.go @@ -23,16 +23,55 @@ THE SOFTWARE. package main import ( + "runtime" + "runtime/debug" + "github.com/peter-bread/gamon3/v2/cmd" ) +// VCS build info. These should be set via debug.ReadBuildInfo. var ( version = "dev" commit = "none" - date = "unknown" + date = "unknown" // Specifically commit date, not build date. This is for reproducible builds. ) +func buildSettingsMap(info *debug.BuildInfo) map[string]string { + m := make(map[string]string) + + for _, s := range info.Settings { + m[s.Key] = s.Value + } + + return m +} + +func init() { + var ( + os string + arch string + ) + + if info, ok := debug.ReadBuildInfo(); ok { + settings := buildSettingsMap(info) + + os = settings["GOOS"] + arch = settings["GOARCH"] + + if info.Main.Version != "(devel)" { + version = info.Main.Version + commit = settings["vcs.revision"] + date = settings["vcs.time"] + } + + } else { + os = runtime.GOOS + arch = runtime.GOARCH + } + + cmd.SetVersion(version, commit, date, os, arch) +} + func main() { - cmd.SetVersion(version, commit, date) cmd.Execute() } From c9259e2d3bddc3ef5af1d6374c9a0537e41d6317 Mon Sep 17 00:00:00 2001 From: Peter Sheehan <145384599+peter-bread@users.noreply.github.com> Date: Fri, 2 Jan 2026 17:11:48 +0000 Subject: [PATCH 02/12] build: adds script to check makefile and goreleaser builds are the same --- scripts/check-reproducible-builds | 116 ++++++++++++++++++++++++++++++ 1 file changed, 116 insertions(+) create mode 100755 scripts/check-reproducible-builds diff --git a/scripts/check-reproducible-builds b/scripts/check-reproducible-builds new file mode 100755 index 0000000..e31ac22 --- /dev/null +++ b/scripts/check-reproducible-builds @@ -0,0 +1,116 @@ +#!/usr/bin/env bash + +# This script ensures that release builds from the Makefile are identical to +# release builds from GoReleaser. + +set -e + +# Logging options. +: "${ENABLE_COLOR:=1}" # 1 = colored output, 0 = plain +DATE_FORMAT="%Y-%m-%d %H:%M:%S" + +# ANSI color codes. +RESET="\e[0m" +RED="\e[31m" +GREEN="\e[32m" +YELLOW="\e[33m" +BLUE="\e[34m" + +# Internal logging function. +_log() { + local level="$1" + local msg="$2" + local color="$3" + local stream=${4:-1} + local timestamp + timestamp=$(date +"$DATE_FORMAT") + + if [[ $ENABLE_COLOR -eq 1 && -n "$color" ]]; then + printf "%s ${color}[%-7s]${RESET} %s\n" "$timestamp" "$level" "$msg" >&"$stream" + else + printf "%s [%-7s] %s\n" "$timestamp" "$level" "$msg" >&"$stream" + fi +} + +# Public logging functions. +info() { _log "INFO" "$1" "$BLUE"; } +warn() { _log "WARN" "$1" "$YELLOW" 2; } +error() { _log "ERROR" "$1" "$RED" 2; } +success() { _log "SUCCESS" "$1" "$GREEN"; } + +available() { + [[ -n "$1" ]] && command -v -- "$1" &>/dev/null +} + +unavailable() { + ! available "$1" +} + +check_files() { + local file1="$1" + local file2="$2" + + local hash1 + local hash2 + hash1=$(sha256sum "$file1" | awk '{print $1}') + hash2=$(sha256sum "$file2" | awk '{print $1}') + + if [ "$hash1" = "$hash2" ]; then + return 0 + else + return 1 + fi +} + +goreleaser_path() { + local version + case $GOARCH in + arm64) version="8.0" ;; + amd64) version="1" ;; + *) exit 1 ;; + esac + + echo "./dist/gamon3_${GOOS}_${GOARCH}_v${version}/gamon3" +} + +verify() { + local goos=$1 + local goarch=$2 + + ( + export GOOS=$goos GOARCH=$goarch + info "Building for $GOOS-$GOARCH" + make BUILD_DIR="$TMP" + info "Verifying release build checksums for $GOOS-$GOARCH" + if ! check_files "$TMP"/gamon3 "$(goreleaser_path)"; then + error "Release build checksums did not match for $GOOS-$GOARCH" + return 1 + fi + success "Release build checksums matched for $GOOS-$GOARCH" + ) +} + +info "Verifying Makefile release builds are the same as GoReleaser builds" + +if unavailable sha256sum; then + error "sha256sum is required" + exit 1 +fi + +info "Building GoReleaser snapshot" +make goreleaser + +info "Building from Makefile and checking" + +TMP=$(mktemp -d) +cleanup() { rm -rf "$TMP"; } +trap cleanup EXIT + +export TMP + +verify darwin arm64 +verify darwin amd64 +verify linux arm64 +verify linux amd64 + +success "All release build checksums match" From 52f95c6091559d0668b91ecbd04bfef604495aee Mon Sep 17 00:00:00 2001 From: Peter Sheehan <145384599+peter-bread@users.noreply.github.com> Date: Fri, 2 Jan 2026 17:14:24 +0000 Subject: [PATCH 03/12] docs(README): update build instructions --- README.md | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 304b685..457637c 100644 --- a/README.md +++ b/README.md @@ -68,7 +68,8 @@ go install github.com/peter-bread/gamon3/v2@latest ### Build From Source -To build and install Gamon3 under the default prefix (`/usr/local`), run: +To build and install a release version Gamon3 under the default prefix +(`/usr/local`), run: ```bash git clone https://github.com/peter-bread/gamon3 @@ -83,6 +84,12 @@ To install under a custom prefix, e.g. `~/.local`, run: make install PREFIX=~/.local ``` +If you wish to build with debug information, use + +```bash +make BUILD_MODE=debug +``` + ## Usage ### Authenticate with GH CLI From 1b41b4c3493b3c983953a5016f5e9a977d72f095 Mon Sep 17 00:00:00 2001 From: Peter Sheehan <145384599+peter-bread@users.noreply.github.com> Date: Fri, 2 Jan 2026 17:25:11 +0000 Subject: [PATCH 04/12] ci: update ci - Use `go.mod` for go version - Compare Makefile builds with GoReleaser builds --- .github/workflows/ci.yaml | 57 +++++++++++++++++++++++++++++++--- .github/workflows/release.yaml | 6 ++-- 2 files changed, 56 insertions(+), 7 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 914fcf5..4e3437e 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -8,6 +8,40 @@ on: pull_request: jobs: + build: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v6 + with: + fetch-depth: 0 + + - uses: actions/setup-go@v6 + with: + go-version-file: 'go.mod' + + - run: go version + + - name: Build + run: make + + test: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v6 + with: + fetch-depth: 0 + + - uses: actions/setup-go@v6 + with: + go-version-file: 'go.mod' + + - run: go version + + - name: Run tests + run: make test + lint: runs-on: ubuntu-latest @@ -18,14 +52,16 @@ jobs: - uses: actions/setup-go@v6 with: - go-version: '1.25' + go-version-file: 'go.mod' + + - run: go version - name: golangci-lint uses: golangci/golangci-lint-action@v9 with: version: v2.6 - test: + check-release-build-parity: runs-on: ubuntu-latest steps: @@ -35,7 +71,18 @@ jobs: - uses: actions/setup-go@v6 with: - go-version: '1.25' + go-version-file: 'go.mod' - - name: Run tests - run: make test + - run: go version + + - name: Install GoReleaser + uses: goreleaser/goreleaser-action@v6 + with: + version: "~> v2" + install-only: true + + - name: Show GoReleaser version + run: goreleaser -v + + - name: Compare Makefile builds to GoReleaser builds + run: scripts/check-reproducible-builds diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml index e080819..6367972 100644 --- a/.github/workflows/release.yaml +++ b/.github/workflows/release.yaml @@ -14,13 +14,15 @@ jobs: pull-requests: write steps: - - uses: actions/checkout@v5 + - uses: actions/checkout@v6 with: fetch-depth: 0 - uses: actions/setup-go@v6 with: - go-version: '1.25' + go-version-file: 'go.mod' + + - run: go version - name: Install GoReleaser uses: goreleaser/goreleaser-action@v6 From 53cfe32ccd659b60d56a3fdc7226cb018f78e738 Mon Sep 17 00:00:00 2001 From: Peter Sheehan <145384599+peter-bread@users.noreply.github.com> Date: Fri, 2 Jan 2026 17:29:20 +0000 Subject: [PATCH 05/12] ci: update ci --- .github/workflows/ci.yaml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 4e3437e..9b1c8df 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -25,6 +25,9 @@ jobs: - name: Build run: make + - name: Check version + run: build/gamon3 -v + test: runs-on: ubuntu-latest From 98a00251aa93854f7a35d8fc7a51022907e32a70 Mon Sep 17 00:00:00 2001 From: Peter Sheehan <145384599+peter-bread@users.noreply.github.com> Date: Fri, 2 Jan 2026 20:30:54 +0000 Subject: [PATCH 06/12] ci: update ci --- .github/workflows/ci.yaml | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 9b1c8df..d51e7ed 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -25,9 +25,12 @@ jobs: - name: Build run: make - - name: Check version + - name: Print --version output run: build/gamon3 -v + - name: Print embedded module version information + run: go version -m build/gamon3 + test: runs-on: ubuntu-latest From 89791188d97c1ed820ad142e1408619a9952f041 Mon Sep 17 00:00:00 2001 From: Peter Sheehan <145384599+peter-bread@users.noreply.github.com> Date: Fri, 2 Jan 2026 22:44:03 +0000 Subject: [PATCH 07/12] build(go): don't specify minor go version --- go.mod | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go.mod b/go.mod index a061d57..40bb64a 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/peter-bread/gamon3/v2 -go 1.25.0 +go 1.25 require ( github.com/goccy/go-yaml v1.18.0 From 2e0769dd50cac6c22894a7b9d490fa5923d2ef28 Mon Sep 17 00:00:00 2001 From: Peter Sheehan <145384599+peter-bread@users.noreply.github.com> Date: Sat, 3 Jan 2026 01:04:20 +0000 Subject: [PATCH 08/12] ci: better logging when comparing builds --- scripts/check-reproducible-builds | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/scripts/check-reproducible-builds b/scripts/check-reproducible-builds index e31ac22..17c122c 100755 --- a/scripts/check-reproducible-builds +++ b/scripts/check-reproducible-builds @@ -38,13 +38,9 @@ warn() { _log "WARN" "$1" "$YELLOW" 2; } error() { _log "ERROR" "$1" "$RED" 2; } success() { _log "SUCCESS" "$1" "$GREEN"; } -available() { - [[ -n "$1" ]] && command -v -- "$1" &>/dev/null -} +available() { [[ -n "$1" ]] && command -v -- "$1" &>/dev/null; } -unavailable() { - ! available "$1" -} +unavailable() { ! available "$1"; } check_files() { local file1="$1" @@ -58,6 +54,12 @@ check_files() { if [ "$hash1" = "$hash2" ]; then return 0 else + # NOTE: Using '-trimpath' at build time hides '-ldflags' in 'go version -m' + # output. + warn "Makefile hash: $hash1" + warn "GoReleaser hash: $hash2" + info "Printing diff between go version -m outputs" + diff <(go version -m "$file1") <(go version -m "$file2") return 1 fi } From 019a6209287aaa9b15f477e5f12df75ad57cab0a Mon Sep 17 00:00:00 2001 From: Peter Sheehan <145384599+peter-bread@users.noreply.github.com> Date: Sat, 3 Jan 2026 01:14:09 +0000 Subject: [PATCH 09/12] build(makefile): rename output directory --- .gitignore | 2 +- Makefile | 10 +++++----- scripts/check-reproducible-builds | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/.gitignore b/.gitignore index 71ea8a0..ee0f618 100644 --- a/.gitignore +++ b/.gitignore @@ -34,6 +34,6 @@ go.work.sum # Added by goreleaser init: dist/ -build/ +bin/ node_modules/ diff --git a/Makefile b/Makefile index 6442f3f..c633f6d 100644 --- a/Makefile +++ b/Makefile @@ -9,7 +9,7 @@ ifneq ($(filter $(BUILD_MODE),$(VALID_BUILD_MODES)),$(BUILD_MODE)) $(error Invalid BUILD_MODE '$(BUILD_MODE)'; must be one of: [$(VALID_BUILD_MODES)]) endif -BUILD_DIR = build +BIN = bin CGO_ENABLED = 0 LDFLAGS_COMMON := @@ -24,8 +24,8 @@ GOFLAGS := $(GOFLAGS_COMMON) endif build: - mkdir -p $(BUILD_DIR) - CGO_ENABLED=$(CGO_ENABLED) go build $(GOFLAGS) -o "$(BUILD_DIR)" -ldflags "$(LDFLAGS)" + mkdir -p $(BIN) + CGO_ENABLED=$(CGO_ENABLED) go build $(GOFLAGS) -o "$(BIN)" -ldflags "$(LDFLAGS)" test: go test -v ./... @@ -35,14 +35,14 @@ cover: clean: go clean - rm -rf $(BUILD_DIR) + rm -rf $(BIN) PREFIX ?= /usr/local install: build install -d $(PREFIX)/bin - install $(BUILD_DIR)/gamon3 $(PREFIX)/bin + install $(BIN)/gamon3 $(PREFIX)/bin ################################################################################ diff --git a/scripts/check-reproducible-builds b/scripts/check-reproducible-builds index 17c122c..78dbde7 100755 --- a/scripts/check-reproducible-builds +++ b/scripts/check-reproducible-builds @@ -82,7 +82,7 @@ verify() { ( export GOOS=$goos GOARCH=$goarch info "Building for $GOOS-$GOARCH" - make BUILD_DIR="$TMP" + make BIN="$TMP" info "Verifying release build checksums for $GOOS-$GOARCH" if ! check_files "$TMP"/gamon3 "$(goreleaser_path)"; then error "Release build checksums did not match for $GOOS-$GOARCH" From 6e3f4040567b5678aca54db29ba60f0432dbca63 Mon Sep 17 00:00:00 2001 From: Peter Sheehan <145384599+peter-bread@users.noreply.github.com> Date: Sat, 3 Jan 2026 01:21:52 +0000 Subject: [PATCH 10/12] ci: fix paths --- .github/workflows/ci.yaml | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index d51e7ed..ee56813 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -25,11 +25,10 @@ jobs: - name: Build run: make - - name: Print --version output - run: build/gamon3 -v + - run: bin/gamon3 -v - name: Print embedded module version information - run: go version -m build/gamon3 + run: go version -m bin/gamon3 test: runs-on: ubuntu-latest From 90dc7c388bd3d86c0f1f52dcf488f595074dd31a Mon Sep 17 00:00:00 2001 From: Peter Sheehan <145384599+peter-bread@users.noreply.github.com> Date: Sat, 3 Jan 2026 14:31:57 +0000 Subject: [PATCH 11/12] build: use -ldflags to pass version and omit commit and date --- .goreleaser.yaml | 4 +++- Makefile | 21 +++++++++++++++------ cmd/root.go | 4 ++-- main.go | 44 ++++++++++---------------------------------- 4 files changed, 30 insertions(+), 43 deletions(-) diff --git a/.goreleaser.yaml b/.goreleaser.yaml index 1370f8f..032ca18 100644 --- a/.goreleaser.yaml +++ b/.goreleaser.yaml @@ -15,15 +15,17 @@ gomod: builds: - env: - CGO_ENABLED=0 + - GAMON3_VERSION={{ if index .Env "GAMON3_VERSION" }}{{ .Env.GAMON3_VERSION }}{{ else }}{{ .Version }}{{ end }} goos: - darwin - linux flags: - -v - - -buildvcs=true + - -buildvcs=auto - -trimpath ldflags: - -s -w + - -X main.Version={{.Env.GAMON3_VERSION}} archives: - formats: [tar.gz] diff --git a/Makefile b/Makefile index c633f6d..b35862b 100644 --- a/Makefile +++ b/Makefile @@ -9,16 +9,22 @@ ifneq ($(filter $(BUILD_MODE),$(VALID_BUILD_MODES)),$(BUILD_MODE)) $(error Invalid BUILD_MODE '$(BUILD_MODE)'; must be one of: [$(VALID_BUILD_MODES)]) endif -BIN = bin -CGO_ENABLED = 0 +# This is the default value. In contexts where VCS information is unavaliable, +# this needs to be set manually. +# +# For example, in a Homebrew formula, this set to 'version.to_s'. +GAMON3_VERSION ?= $(shell git describe --tags --dirty --always) -LDFLAGS_COMMON := -GOFLAGS_COMMON := -v -buildvcs=true +BIN := bin +CGO_ENABLED := 0 + +LDFLAGS_COMMON := -X main.Version=$(GAMON3_VERSION) +GOFLAGS_COMMON := -v -buildvcs=auto ifeq ($(BUILD_MODE), release) LDFLAGS := -s -w $(LDFLAGS_COMMON) GOFLAGS := -trimpath $(GOFLAGS_COMMON) -else ifeq ($(BUILD_MODE), debug) +else ifeq ($(BUILD_MODE), debug) LDFLAGS := $(LDFLAGS_COMMON) GOFLAGS := $(GOFLAGS_COMMON) endif @@ -47,5 +53,8 @@ install: build ################################################################################ # This will use '.goreleaser.yaml' and build in 'dist/'. +# GAMON3_VERSION is passed to ensure version output is the same as using 'make'. +# This is just for checking that GoReleaser and Makefile builds are the same. goreleaser: - goreleaser release --snapshot --clean + GAMON3_VERSION=$(GAMON3_VERSION) goreleaser release --snapshot --clean + # goreleaser release --snapshot --clean diff --git a/cmd/root.go b/cmd/root.go index c0a9bd2..0af41f2 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -53,8 +53,8 @@ func Execute() { } } -func SetVersion(version, commit, date, os, arch string) { - rootCmd.Version = fmt.Sprintf("%s %s-%s\nCommit: %s (%s)", version, os, arch, commit, date) +func SetVersion(version, os, arch string) { + rootCmd.Version = fmt.Sprintf("%s %s-%s", version, os, arch) } func init() { diff --git a/main.go b/main.go index 39b7cdb..44402c5 100644 --- a/main.go +++ b/main.go @@ -29,47 +29,23 @@ import ( "github.com/peter-bread/gamon3/v2/cmd" ) -// VCS build info. These should be set via debug.ReadBuildInfo. var ( - version = "dev" - commit = "none" - date = "unknown" // Specifically commit date, not build date. This is for reproducible builds. + Version = "dev" + Os = "unknown" + Arch = "unknown" ) -func buildSettingsMap(info *debug.BuildInfo) map[string]string { - m := make(map[string]string) - - for _, s := range info.Settings { - m[s.Key] = s.Value - } - - return m -} - func init() { - var ( - os string - arch string - ) - - if info, ok := debug.ReadBuildInfo(); ok { - settings := buildSettingsMap(info) - - os = settings["GOOS"] - arch = settings["GOARCH"] - - if info.Main.Version != "(devel)" { - version = info.Main.Version - commit = settings["vcs.revision"] - date = settings["vcs.time"] + if Version == "dev" { + if info, ok := debug.ReadBuildInfo(); ok && info.Main.Version != "(devel)" { + Version = info.Main.Version } - - } else { - os = runtime.GOOS - arch = runtime.GOARCH } - cmd.SetVersion(version, commit, date, os, arch) + Os = runtime.GOOS + Arch = runtime.GOARCH + + cmd.SetVersion(Version, Os, Arch) } func main() { From 7eb9c5d2ecbe3a602fe67880cea24fa57a471f60 Mon Sep 17 00:00:00 2001 From: Peter Sheehan <145384599+peter-bread@users.noreply.github.com> Date: Sat, 3 Jan 2026 18:27:32 +0000 Subject: [PATCH 12/12] refactor: dedicated build module for establishing build info --- .goreleaser.yaml | 2 +- Makefile | 2 +- internal/build/build.go | 46 +++++++++++++++++++++++++++++++++++++++++ main.go | 21 ++----------------- 4 files changed, 50 insertions(+), 21 deletions(-) create mode 100644 internal/build/build.go diff --git a/.goreleaser.yaml b/.goreleaser.yaml index 032ca18..04d59ec 100644 --- a/.goreleaser.yaml +++ b/.goreleaser.yaml @@ -25,7 +25,7 @@ builds: - -trimpath ldflags: - -s -w - - -X main.Version={{.Env.GAMON3_VERSION}} + - -X github.com/peter-bread/gamon3/v2/internal/build.Version={{ .Env.GAMON3_VERSION }} archives: - formats: [tar.gz] diff --git a/Makefile b/Makefile index b35862b..d526773 100644 --- a/Makefile +++ b/Makefile @@ -18,7 +18,7 @@ GAMON3_VERSION ?= $(shell git describe --tags --dirty --always) BIN := bin CGO_ENABLED := 0 -LDFLAGS_COMMON := -X main.Version=$(GAMON3_VERSION) +LDFLAGS_COMMON := -X github.com/peter-bread/gamon3/v2/internal/build.Version=$(GAMON3_VERSION) GOFLAGS_COMMON := -v -buildvcs=auto ifeq ($(BUILD_MODE), release) diff --git a/internal/build/build.go b/internal/build/build.go new file mode 100644 index 0000000..81f56dc --- /dev/null +++ b/internal/build/build.go @@ -0,0 +1,46 @@ +/* +Copyright © 2025 Peter Sheehan + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +*/ + +// Package build sets build information for the binary. +package build + +import ( + "runtime" + "runtime/debug" +) + +var ( + Version = "dev" + Os = "unknown" + Arch = "unknown" +) + +func init() { + if Version == "dev" { + if info, ok := debug.ReadBuildInfo(); ok && info.Main.Version != "(devel)" { + Version = info.Main.Version + } + } + + Os = runtime.GOOS + Arch = runtime.GOARCH +} diff --git a/main.go b/main.go index 44402c5..b3c726f 100644 --- a/main.go +++ b/main.go @@ -23,29 +23,12 @@ THE SOFTWARE. package main import ( - "runtime" - "runtime/debug" - "github.com/peter-bread/gamon3/v2/cmd" -) - -var ( - Version = "dev" - Os = "unknown" - Arch = "unknown" + "github.com/peter-bread/gamon3/v2/internal/build" ) func init() { - if Version == "dev" { - if info, ok := debug.ReadBuildInfo(); ok && info.Main.Version != "(devel)" { - Version = info.Main.Version - } - } - - Os = runtime.GOOS - Arch = runtime.GOARCH - - cmd.SetVersion(Version, Os, Arch) + cmd.SetVersion(build.Version, build.Os, build.Arch) } func main() {