Skip to content

Commit 14925c0

Browse files
committed
core(fix append version)
1 parent 2a3949f commit 14925c0

1 file changed

Lines changed: 40 additions & 0 deletions

File tree

internal/buildinfo/buildinfo.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
package buildinfo
22

3+
import (
4+
"runtime/debug"
5+
"strings"
6+
)
7+
38
var (
49
Version = "dev"
510
Author = "jr-k"
@@ -28,3 +33,38 @@ func Long() string {
2833
return "Version: " + Version + "\nCommit: " + Commit + "\nBuilt: " + Date
2934
}
3035

36+
// init fills Version / Commit / Date from runtime/debug.ReadBuildInfo when the
37+
// ldflags injection (used by goreleaser) hasn't run. This is the case for
38+
// users installing via `go install github.com/jr-k/d4s@vX.Y.Z`, where Go
39+
// embeds the module version into the binary metadata.
40+
func init() {
41+
info, ok := debug.ReadBuildInfo()
42+
if !ok {
43+
return
44+
}
45+
46+
if Version == "dev" {
47+
if v := info.Main.Version; v != "" && v != "(devel)" {
48+
// goreleaser sets Version without the leading "v" (e.g. "0.49.99")
49+
// and the UI prepends a "v" at display time. Keep the same shape
50+
// here so we don't end up with "vv0.49.99". The "+dirty" suffix that
51+
// Go >= 1.24 appends when building from a dirty VCS tree is kept on
52+
// purpose so the displayed version reflects the real binary state.
53+
Version = strings.TrimPrefix(v, "v")
54+
}
55+
}
56+
57+
for _, s := range info.Settings {
58+
switch s.Key {
59+
case "vcs.revision":
60+
if Commit == "none" && s.Value != "" {
61+
Commit = s.Value
62+
}
63+
case "vcs.time":
64+
if Date == "unknown" && s.Value != "" {
65+
Date = s.Value
66+
}
67+
}
68+
}
69+
}
70+

0 commit comments

Comments
 (0)