-
Notifications
You must be signed in to change notification settings - Fork 1
fix(cmd): register the --version flag, and report a real version on go install #55
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
ac9eb28
92110c4
3fd855b
cf1ac28
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,7 @@ | |
| package version | ||
|
|
||
| import ( | ||
| "runtime/debug" | ||
| "strings" | ||
| ) | ||
|
|
||
|
|
@@ -14,6 +15,41 @@ var ( | |
| BuildDate = "unknown" | ||
| ) | ||
|
|
||
| // Release builds get these values from -ldflags. A `go install` build gets no | ||
| // ldflags at all, so it reported itself as "dev" forever -- even though the | ||
| // toolchain had already stamped the module version and the VCS revision into | ||
| // the binary. Read them back out. | ||
| func init() { | ||
| info, ok := debug.ReadBuildInfo() | ||
| if !ok { | ||
| return | ||
| } | ||
|
|
||
| // Each value falls back on its own. A build that sets only Version through | ||
| // -ldflags should still pick up the commit and the build time the toolchain | ||
| // recorded, rather than being left reporting "unknown" for both. | ||
| if Version == "dev" { | ||
| // "(devel)" is what a build from a local working tree reports. | ||
| if info.Main.Version != "" && info.Main.Version != "(devel)" { | ||
| Version = info.Main.Version | ||
| } | ||
| } | ||
| for _, setting := range info.Settings { | ||
| switch setting.Key { | ||
| case "vcs.revision": | ||
| // An empty value must not overwrite "unknown" -- it would render as | ||
| // "y509 version dev ()". | ||
| if GitCommit == "unknown" && setting.Value != "" { | ||
| GitCommit = setting.Value | ||
| } | ||
| case "vcs.time": | ||
| if BuildDate == "unknown" && setting.Value != "" { | ||
| BuildDate = setting.Value | ||
| } | ||
| } | ||
| } | ||
|
kanywst marked this conversation as resolved.
kanywst marked this conversation as resolved.
kanywst marked this conversation as resolved.
Comment on lines
+37
to
+50
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When building with var modified bool
var commitUpdated bool
for _, setting := range info.Settings {
switch setting.Key {
case "vcs.revision":
// An empty value must not overwrite "unknown" -- it would render as
// "y509 version dev ()".
if GitCommit == "unknown" && setting.Value != "" {
GitCommit = setting.Value
commitUpdated = true
}
case "vcs.time":
if BuildDate == "unknown" && setting.Value != "" {
BuildDate = setting.Value
}
case "vcs.modified":
modified = setting.Value == "true"
}
}
if modified && commitUpdated && !strings.HasSuffix(GitCommit, "-dirty") {
GitCommit += "-dirty"
} |
||
| } | ||
|
kanywst marked this conversation as resolved.
|
||
|
|
||
| // GetVersion returns the version string | ||
| func GetVersion() string { | ||
| return Version | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.