Skip to content

Commit 3d98de6

Browse files
committed
fix: show actual version when installed via go install
- Use runtime/debug.ReadBuildInfo() to read version from module info - Falls back to ldflags version for GoReleaser builds - Reads VCS commit hash and build time from build settings - Removes outdated note from README about version showing as "dev" Now when users run: go install github.com/itsdevcoffee/plum/cmd/plum@v0.3.4 plum --version They'll see: plum version v0.3.4 commit: abc1234 built: 2026-01-11T07:35:03Z Instead of: plum version dev commit: none built: unknown
1 parent 2d54563 commit 3d98de6

2 files changed

Lines changed: 39 additions & 4 deletions

File tree

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,6 @@ plum
111111
```
112112

113113
**Requirements:** Go 1.24+
114-
**Note:** Version will show as "dev" when installed this way. Use Homebrew or pre-built binaries for version tracking.
115114

116115
**Troubleshooting:** If `plum` command isn't found after `go install`, add `$GOPATH/bin` to your PATH:
117116
```bash

cmd/plum/main.go

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

78
tea "github.com/charmbracelet/bubbletea"
89
"github.com/itsdevcoffee/plum/internal/ui"
@@ -14,12 +15,47 @@ var (
1415
date = "unknown"
1516
)
1617

18+
func getVersion() (ver, cmt, bDate string) {
19+
// Try to get version from build info (works with go install)
20+
if info, ok := debug.ReadBuildInfo(); ok && version == "dev" {
21+
if info.Main.Version != "" && info.Main.Version != "(devel)" {
22+
ver = info.Main.Version
23+
} else {
24+
ver = version
25+
}
26+
27+
// Try to get commit from build settings
28+
for _, setting := range info.Settings {
29+
if setting.Key == "vcs.revision" && commit == "none" {
30+
cmt = setting.Value[:7] // Short commit hash
31+
}
32+
if setting.Key == "vcs.time" && date == "unknown" {
33+
bDate = setting.Value
34+
}
35+
}
36+
}
37+
38+
// Use ldflags values if set (from GoReleaser)
39+
if ver == "" {
40+
ver = version
41+
}
42+
if cmt == "" {
43+
cmt = commit
44+
}
45+
if bDate == "" {
46+
bDate = date
47+
}
48+
49+
return ver, cmt, bDate
50+
}
51+
1752
func main() {
1853
// Handle --version flag
1954
if len(os.Args) > 1 && (os.Args[1] == "--version" || os.Args[1] == "-v") {
20-
fmt.Printf("plum version %s\n", version)
21-
fmt.Printf(" commit: %s\n", commit)
22-
fmt.Printf(" built: %s\n", date)
55+
ver, cmt, bDate := getVersion()
56+
fmt.Printf("plum version %s\n", ver)
57+
fmt.Printf(" commit: %s\n", cmt)
58+
fmt.Printf(" built: %s\n", bDate)
2359
os.Exit(0)
2460
}
2561

0 commit comments

Comments
 (0)