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 29f7aeb
Showing 1 changed file with 37 additions and 6 deletions.
43 changes: 37 additions & 6 deletions lib/consts/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

// Version contains the current semantic version of k6.
const Version = "0.46.0"
const Version = "0.46.1"

// VersionDetails can be set externally as part of the build process
var VersionDetails = "" //nolint:gochecknoglobals
Expand All @@ -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 == "" {

Check warning on line 33 in lib/consts/consts.go

View workflow job for this annotation

GitHub Actions / lint

empty-block: this block is empty, you can remove it (revive)
// 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 29f7aeb

Please sign in to comment.