This repository has been archived by the owner on Jun 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: improve binary version build string
- Loading branch information
Showing
2 changed files
with
71 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,78 @@ | ||
package mevcommit | ||
|
||
import ( | ||
"runtime/debug" | ||
"strings" | ||
) | ||
|
||
var ( | ||
version string | ||
commitHash string | ||
// version will be the version git tag if the binary is built with | ||
// the Makefile and the tag is set. If the tag does not exist the | ||
// version will be set to "(devel)". If the binary is built some | ||
// other way, it will be set to "unknown". | ||
version = "unknown" | ||
|
||
// revision is set from the vcs.revision tag in Go 1.18+. | ||
revision = "unknown" | ||
|
||
// dirtyBuild is set from the vcs.modified tag in Go 1.18+. | ||
dirtyBuild = true | ||
) | ||
|
||
// Version returns the version of the binary. | ||
func init() { | ||
info, ok := debug.ReadBuildInfo() | ||
if !ok { | ||
return | ||
} | ||
for _, kv := range info.Settings { | ||
if kv.Value == "" { | ||
continue | ||
} | ||
switch kv.Key { | ||
case "vcs.revision": | ||
revision = kv.Value | ||
case "vcs.modified": | ||
dirtyBuild = kv.Value == "true" | ||
} | ||
} | ||
|
||
} | ||
|
||
// Version returns the build version string of the binary in format: | ||
// | ||
// <tag>-<commit-hash>[-dirty] | ||
// | ||
// If the tag is not set during build (the tag does not exist or the binary is | ||
// not build with the Makefile), the tag element will remain empty. If the | ||
// binary is built outside the vcs repository, the returned result will be | ||
// "devel". If the binary is built with the Makefile and the repository | ||
// is dirty, the -dirty element will be appended to the result. | ||
// | ||
// Examples: | ||
// | ||
// "devel" | ||
// "rev-333ab74" | ||
// "rev-333ab74-dirty" | ||
// "v1.0.0-rev-333ab74" | ||
// "v1.0.0-rev-333ab74-dirty" | ||
func Version() string { | ||
if version == "" || commitHash == "" { | ||
return "dev-dirty" | ||
parts := make([]string, 0, 3) | ||
if version != "unknown" && version != "" { | ||
parts = append(parts, version) | ||
} | ||
if revision != "unknown" && revision != "" { | ||
parts = append(parts, "rev") | ||
commit := revision | ||
if len(commit) > 7 { | ||
commit = commit[:7] | ||
} | ||
parts = append(parts, commit) | ||
if dirtyBuild { | ||
parts = append(parts, "dirty") | ||
} | ||
} | ||
if len(parts) == 0 { | ||
return "devel" | ||
} | ||
return version + "-" + commitHash | ||
return strings.Join(parts, "-") | ||
} |