From ee0fa0bc5c0065a150c72f4e9a8c9c3c24fa4ec7 Mon Sep 17 00:00:00 2001 From: Alexander Yastrebov Date: Fri, 23 Feb 2024 12:13:43 +0100 Subject: [PATCH] cmd/skipper: fix version and commit for go install When skipper is installed via `go install` it has empty version and commit values: ``` ~$ go install github.com/zalando/skipper/cmd/skipper@latest go: downloading github.com/zalando/skipper v0.21.2 ~$ skipper -version Skipper version (commit: , runtime: go1.22.0) ``` Current build process of binaries specifies version and commit via `-ldflags`. This change uses debug buildinfo to get version and commit if they are not specified by `-ldflags` which is compatible with the current build process but should also work for `go install` case. If this change fixes `go install` case (which can only be tested after package version is published) then we can add the same snippet to other binaries and get rid of COMMIT_HASH ldflag. We can not get rid of `-ldflags` fully though because `go build` does not stamp version (see https://github.com/golang/go/issues/50603). Signed-off-by: Alexander Yastrebov --- cmd/skipper/main.go | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/cmd/skipper/main.go b/cmd/skipper/main.go index bff346ae1d..8281e78c74 100644 --- a/cmd/skipper/main.go +++ b/cmd/skipper/main.go @@ -17,7 +17,6 @@ package main import ( "fmt" "runtime" - "runtime/debug" log "github.com/sirupsen/logrus" @@ -32,6 +31,9 @@ var ( func init() { if info, ok := debug.ReadBuildInfo(); ok { + if version == "" { + version = info.Main.Version + } if commit == "" { for _, setting := range info.Settings { if setting.Key == "vcs.revision" { @@ -39,9 +41,6 @@ func init() { } } } - if version == "" { - version = info.Main.Version - } } }