mirror of
https://github.com/containers/gvisor-tap-vsock.git
synced 2025-12-24 13:29:22 +08:00
version: Add moduleVersionFromBuildInfo
This adds correct version information for `gvproxy -version` when it's installed using: ``` go install github.com/containers/gvisor-tap-vsock/cmd/gvproxy ``` Signed-off-by: Christophe Fergeau <cfergeau@redhat.com>
This commit is contained in:
@@ -3,6 +3,7 @@ package types
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"runtime/debug"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -22,7 +23,7 @@ func NewVersion(binaryName string) *version { //nolint:revive
|
||||
}
|
||||
|
||||
func (ver *version) String() string {
|
||||
return fmt.Sprintf("%s version %s", ver.binaryName, gitVersion)
|
||||
return fmt.Sprintf("%s version %s", ver.binaryName, moduleVersion())
|
||||
}
|
||||
|
||||
func (ver *version) AddFlag() {
|
||||
@@ -32,3 +33,25 @@ func (ver *version) AddFlag() {
|
||||
func (ver *version) ShowVersion() bool {
|
||||
return ver.showVersion
|
||||
}
|
||||
|
||||
func moduleVersion() string {
|
||||
switch {
|
||||
// This will be set when building from git using make
|
||||
case gitVersion != "":
|
||||
return gitVersion
|
||||
// moduleVersionFromBuildInfo() will be set when using `go install`
|
||||
default:
|
||||
return moduleVersionFromBuildInfo()
|
||||
}
|
||||
}
|
||||
|
||||
func moduleVersionFromBuildInfo() string {
|
||||
info, ok := debug.ReadBuildInfo()
|
||||
if !ok {
|
||||
return ""
|
||||
}
|
||||
if info.Main.Version == "(devel)" {
|
||||
return ""
|
||||
}
|
||||
return info.Main.Version
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user