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 `vfkit -version` when
it's installed using:
```
go install github.com/crc-org/vfkit/cmd/vfkit
```

However, due to missing entitlements, a binary installed this way is
unlikely to be able to start VMs.
I'm keeping this codepath to use the same code in gvisor-tap-vsock and
vfkit.

Signed-off-by: Christophe Fergeau <[email protected]>
  • Loading branch information
cfergeau committed Nov 23, 2023
1 parent 72cf914 commit 2ad4228
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion pkg/cmdline/version.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,30 @@
package cmdline

import (
"runtime/debug"
)

// set using the '-X github.com/crc-org/vfkit/pkg/cmdline.gitVersion' linker flag
var gitVersion = "unknown"

func Version() string {
return gitVersion
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
}

0 comments on commit 2ad4228

Please sign in to comment.