-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Get version information from embedded build info (#367)
The version is provided by the version.json file and the revision information is build into the binary by the compiler. Use this information instead of fetching it from github each build. - Installed binary has version information. - Builds work even without connectivity to github.
- Loading branch information
Showing
3 changed files
with
66 additions
and
9 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,52 @@ | ||
package provider | ||
|
||
import ( | ||
_ "embed" | ||
"encoding/json" | ||
"runtime/debug" | ||
) | ||
|
||
var ( | ||
// Release is the release version tag value, e.g. "v1.2.3" | ||
Release string | ||
// Revision is the git commit hash. | ||
Revision string | ||
// Version is the full version string: Release-Revision. | ||
Version string | ||
// Modified indicates if the source tree had local modifications. | ||
Modified bool | ||
) | ||
|
||
//go:embed version.json | ||
var versionJSON []byte | ||
|
||
func init() { | ||
// Read version from embedded JSON file. | ||
var verMap map[string]string | ||
json.Unmarshal(versionJSON, &verMap) | ||
Release = verMap["version"] | ||
|
||
// If running from a module, try to get the build info. | ||
bi, ok := debug.ReadBuildInfo() | ||
if !ok { | ||
return | ||
} | ||
|
||
var found int | ||
|
||
// Append the revision to the version. | ||
for i := range bi.Settings { | ||
switch bi.Settings[i].Key { | ||
case "vcs.revision": | ||
Revision = bi.Settings[i].Value | ||
Version = Release + "-" + Revision | ||
found++ | ||
case "vcs.modified": | ||
Modified = bi.Settings[i].Value == "true" | ||
found++ | ||
} | ||
if found == 2 { | ||
break | ||
} | ||
} | ||
} |