]> ruderich.org/simon Gitweb - safcm/safcm.git/commitdiff
version: use embedded build info
authorSimon Ruderich <simon@ruderich.org>
Sun, 19 Oct 2025 08:59:18 +0000 (10:59 +0200)
committerSimon Ruderich <simon@ruderich.org>
Sun, 19 Oct 2025 09:18:48 +0000 (11:18 +0200)
This changes the Git version to the revision hash instead of a possible
tag. But this is not relevant for now.

Makefile
cmd/safcm/version.go

index 0cf37fb827485f0274d11313c828f4061d28a87d..759e7031eb4e516160adf21e0554fa086c7c1970 100644 (file)
--- 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
index 06c529103648bc141e603f06385b3c0745bc99f9..fe41fec2b19ae0ffc8792cb40c777fa85639f816 100644 (file)
@@ -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
 }