Skip to content

Commit

Permalink
version: Add moduleVersionFromBuildInfo
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
cfergeau committed Nov 22, 2023
1 parent a70793b commit fce89c8
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion pkg/types/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package types
import (
"flag"
"fmt"
"runtime/debug"
)

var ModuleVersion = ""
Expand All @@ -19,7 +20,7 @@ func NewVersion(binaryName string) *version { //nolint:revive
}

func (ver *version) String() string {
return fmt.Sprintf("%s version %s", ver.binaryName, ModuleVersion)
return fmt.Sprintf("%s version %s", ver.binaryName, moduleVersion())
}

func (ver *version) AddFlag() {
Expand All @@ -29,3 +30,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 ModuleVersion != "":
return ModuleVersion
// 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
}

0 comments on commit fce89c8

Please sign in to comment.