From a365a98d014f50335d24923b18971960a4e29e03 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20H=C3=BCbner?= Date: Wed, 1 May 2024 16:30:48 +0200 Subject: [PATCH] goreleaser inject buildinfo --- .goreleaser.yaml | 5 ++++- buildinfo/info.go | 55 +++++++++++++++++++---------------------------- web/client.go | 3 +-- 3 files changed, 27 insertions(+), 36 deletions(-) diff --git a/.goreleaser.yaml b/.goreleaser.yaml index ee12543..180e345 100644 --- a/.goreleaser.yaml +++ b/.goreleaser.yaml @@ -16,7 +16,10 @@ builds: ldflags: - '-s' - '-w' - - '-X github.com/nothub/mrpack-install/buildinfo.Version={{ .Version }}' + - '-X github.com/nothub/mrpack-install/buildinfo.version={{ .Version }}' + - '-X github.com/nothub/mrpack-install/buildinfo.commit={{ .Commit }}' + - '-X github.com/nothub/mrpack-install/buildinfo.date={{ .Date }}' + - '-X github.com/nothub/mrpack-install/buildinfo.tool=goreleaser' goos: - linux - darwin diff --git a/buildinfo/info.go b/buildinfo/info.go index ca6bcac..7aeed15 100644 --- a/buildinfo/info.go +++ b/buildinfo/info.go @@ -5,21 +5,23 @@ import ( "log" "path" "runtime/debug" - "strings" ) const unknown string = "unknown" -var name = unknown -var module = unknown +var ( + name = unknown + module = unknown -var version = unknown -var revision = unknown -var dirty = false + version = unknown + commit = unknown + date = unknown + tool = unknown -var arch = unknown -var os = unknown -var compiler = unknown + arch = unknown + os = unknown + compiler = unknown +) func init() { bi, ok := debug.ReadBuildInfo() @@ -30,12 +32,11 @@ func init() { name = path.Base(bi.Main.Path) module = bi.Main.Path - version = bi.Main.Version + + var dirty = false for _, kv := range bi.Settings { switch kv.Key { - case "vcs.revision": - revision = kv.Value[:8] case "vcs.modified": if kv.Value == "true" { dirty = true @@ -49,8 +50,8 @@ func init() { } } - if version == unknown { - version = revision + if dirty { + version = version + "+DIRTY" } } @@ -62,23 +63,11 @@ func Module() string { return module } -func Version() string { - return version -} - -func Revision() string { - return revision -} - -func PrintInfos() { - sb := strings.Builder{} - sb.WriteString(fmt.Sprintf("%s %s", Name(), Version())) - if Version() != Revision() { - sb.WriteString(fmt.Sprintf(" %s", Revision())) - } - sb.WriteString(fmt.Sprintf(" %s-%s-%s", arch, os, compiler)) - if dirty { - sb.WriteString(" DIRTY") - } - fmt.Println(sb.String()) +func Print() { + fmt.Printf("module: %s\n", module) + fmt.Printf("version: %s\n", version) + fmt.Printf("triplet: %s-%s-%s\n", arch, os, compiler) + fmt.Printf("built at: %s\n", date) + fmt.Printf("built from: %s\n", commit) + fmt.Printf("built with: %s\n", tool) } diff --git a/web/client.go b/web/client.go index e540b77..157754a 100644 --- a/web/client.go +++ b/web/client.go @@ -36,9 +36,8 @@ func NewTransport() *http.Transport { func UserAgent() string { return fmt.Sprintf( - "%s/%s (+https://%s)", + "%s (+https://%s)", buildinfo.Name(), - buildinfo.Version(), buildinfo.Module(), ) }