From: Simon Ruderich Date: Sun, 19 Oct 2025 08:59:18 +0000 (+0200) Subject: version: use embedded build info X-Git-Url: https://ruderich.org/simon/gitweb/?a=commitdiff_plain;h=1caeaca4b569c1b89261805a87c175d5d382c2b5;p=safcm%2Fsafcm.git version: use embedded build info This changes the Git version to the revision hash instead of a possible tag. But this is not relevant for now. --- diff --git a/Makefile b/Makefile index 0cf37fb..759e703 100644 --- a/Makefile +++ b/Makefile @@ -1,8 +1,3 @@ -LDFLAGS = '\ - -X "main.versionGit=$(shell git describe --long --tags --dirty --always) \ -$(shell git show --no-patch --pretty=format:%as)" \ - -X "main.versionGo=$(shell go version)" \ -' # Build with `make GOFLAGS=` if -race is not supported GOFLAGS=-race @@ -12,12 +7,14 @@ all: safcm safcm: go fmt ./... cd cmd/safcm-remote && ./build.sh - go build $(GOFLAGS) -ldflags $(LDFLAGS) ruderich.org/simon/safcm/cmd/safcm + go build $(GOFLAGS) ruderich.org/simon/safcm/cmd/safcm test: ./cmd/safcm/testdata/ssh/prepare.sh go vet ./... go test $(GOFLAGS) ./... + @# Make sure buildinfo can be read + ./safcm version > /dev/null lint: shellcheck ci/run diff --git a/cmd/safcm/version.go b/cmd/safcm/version.go index 06c5291..fe41fec 100644 --- a/cmd/safcm/version.go +++ b/cmd/safcm/version.go @@ -7,16 +7,37 @@ package main import ( "fmt" -) - -// Set via -ldflags -X during build -var ( - versionGit string - versionGo string + "log" + "runtime/debug" ) func MainVersion() error { - fmt.Printf("safcm %s, compiled with %s\n", versionGit, versionGo) + info, ok := debug.ReadBuildInfo() + if !ok { + log.Fatal("ReadBuildInfo failed") + } + + var vcsRev, vcsDirty, vcsTime, goos, goarch string + for _, x := range info.Settings { + switch x.Key { + case "vcs.revision": + vcsRev = x.Value[:7] + case "vcs.modified": + if x.Value == "true" { + vcsDirty = "-dirty" + } + case "vcs.time": + vcsTime = x.Value[:10] // "2006-01-02" + case "GOOS": + goos = x.Value + case "GOARCH": + goarch = x.Value + } + } + + fmt.Printf("safcm %s%s %s, compiled with %s %s/%s\n", + vcsRev, vcsDirty, vcsTime, + info.GoVersion, goos, goarch) return nil }