File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11package buildinfo
22
3+ import (
4+ "runtime/debug"
5+ "strings"
6+ )
7+
38var (
49 Version = "dev"
510 Author = "jr-k"
@@ -28,3 +33,38 @@ func Long() string {
2833 return "Version: " + Version + "\n Commit: " + Commit + "\n Built: " + 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+
You can’t perform that action at this time.
0 commit comments