-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: use the right version information when go install is used
Signed-off-by: Christian Ege <[email protected]>
- Loading branch information
Showing
3 changed files
with
59 additions
and
0 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
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 |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// Package versioninfo uses runtime.ReadBuildInfo() to set global executable revision information if possible. | ||
package versioninfo | ||
|
||
// SPDX-License-Identifier: MIT | ||
// Copyright (c) 2021 Carl Johnson | ||
// Based on https://github.com/earthboundkid/versioninfo | ||
|
||
import ( | ||
"runtime/debug" | ||
"time" | ||
) | ||
|
||
var ( | ||
// Version will be the version tag if the binary is built with "go install url/tool@version". | ||
// If the binary is built some other way, it will be "(devel)". | ||
Version = "unknown" | ||
// Revision is taken from the vcs.revision tag in Go 1.18+. | ||
Revision = "unknown" | ||
// LastCommit is taken from the vcs.time tag in Go 1.18+. | ||
LastCommit time.Time | ||
// DirtyBuild is taken from the vcs.modified tag in Go 1.18+. | ||
DirtyBuild = true | ||
) | ||
|
||
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.time": | ||
LastCommit, _ = time.Parse(time.RFC3339, kv.Value) | ||
case "vcs.modified": | ||
DirtyBuild = kv.Value == "true" | ||
} | ||
} | ||
if info.Main.Version != "" { | ||
Version = info.Main.Version | ||
} | ||
} |