Skip to content

Commit

Permalink
cmd/version: Return commit from go.readbuildinfo
Browse files Browse the repository at this point in the history
  • Loading branch information
codebien committed Sep 8, 2023
1 parent bec53bf commit 87e8a3a
Showing 1 changed file with 36 additions and 5 deletions.
41 changes: 36 additions & 5 deletions lib/consts/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,46 @@ var VersionDetails = "" //nolint:gochecknoglobals
// the currently running k6 executable.
func FullVersion() string {
goVersionArch := fmt.Sprintf("%s, %s/%s", runtime.Version(), runtime.GOOS, runtime.GOARCH)
if VersionDetails != "" {
return fmt.Sprintf("%s (%s, %s)", Version, VersionDetails, goVersionArch)

buildInfo, ok := debug.ReadBuildInfo()
if !ok {
return fmt.Sprintf("%s (%s)", Version, goVersionArch)
}

var (
commit string
dirty bool
)
for _, s := range buildInfo.Settings {
switch s.Key {
case "vcs.revision":
if s.Value == "" {
// it should not happen but applying definsive coding
// for edge cases, for example in the event that the local .git repository
// has been deleted before compiling
}
commitLen := 10
if len(s.Value) < commitLen {
commitLen = len(s.Value)
}
commit = s.Value[:commitLen]
case "vcs.modified":
if s.Value == "true" {
dirty = true
}
default:
}
}

if commit == "" {
return fmt.Sprintf("%s (%s)", Version, goVersionArch)
}

if buildInfo, ok := debug.ReadBuildInfo(); ok {
return fmt.Sprintf("%s (%s, %s)", Version, buildInfo.Main.Version, goVersionArch)
if dirty {
commit += "-dirty"
}

return fmt.Sprintf("%s (dev build, %s)", Version, goVersionArch)
return fmt.Sprintf("%s (commit/%s, %s)", Version, commit, goVersionArch)
}

// Banner returns the ASCII-art banner with the k6 logo and stylized website URL
Expand Down

0 comments on commit 87e8a3a

Please sign in to comment.