Skip to content

Commit 580f635

Browse files
committed
feat: fall back to runtime/debug.ReadBuildInfo for version info
When ldflags are not set (e.g., via go install), read VCS metadata embedded by the Go toolchain so users get meaningful version output instead of "dev (none) built unknown".
1 parent 1ff0d5e commit 580f635

1 file changed

Lines changed: 40 additions & 3 deletions

File tree

cmd/flashduty/main.go

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,57 @@ package main
33
import (
44
"fmt"
55
"os"
6+
"runtime/debug"
67

78
"github.com/flashcatcloud/flashduty-cli/internal/cli"
89
)
910

1011
var (
11-
version = "dev"
12-
commit = "none"
13-
date = "unknown"
12+
version = ""
13+
commit = ""
14+
date = ""
1415
)
1516

1617
func main() {
18+
if version == "" {
19+
readBuildInfo()
20+
}
21+
if version == "" {
22+
version = "dev"
23+
}
24+
if commit == "" {
25+
commit = "none"
26+
}
27+
if date == "" {
28+
date = "unknown"
29+
}
1730
cli.SetVersionInfo(version, commit, date)
1831
if err := cli.Execute(); err != nil {
1932
fmt.Fprintf(os.Stderr, "Error: %s\n", err)
2033
os.Exit(1)
2134
}
2235
}
36+
37+
func readBuildInfo() {
38+
info, ok := debug.ReadBuildInfo()
39+
if !ok {
40+
return
41+
}
42+
version = info.Main.Version
43+
for _, s := range info.Settings {
44+
switch s.Key {
45+
case "vcs.revision":
46+
if len(s.Value) > 7 {
47+
commit = s.Value[:7]
48+
} else {
49+
commit = s.Value
50+
}
51+
case "vcs.time":
52+
date = s.Value
53+
case "vcs.modified":
54+
if s.Value == "true" {
55+
version += "-dirty"
56+
}
57+
}
58+
}
59+
}

0 commit comments

Comments
 (0)