Skip to content

Commit 7309a08

Browse files
authored
Makefile,cmd/aperture: go install has proper version info (#6)
fix the -version flag to display useful when aperture-cli is installed with go install ...
1 parent d08506d commit 7309a08

2 files changed

Lines changed: 37 additions & 1 deletion

File tree

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ test:
1616
go test ./...
1717

1818
install:
19-
go install ./cmd/aperture
19+
go install -ldflags "$(LDFLAGS)" ./cmd/aperture
2020

2121
clean:
2222
rm -rf .build/

cmd/aperture/main.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"log/slog"
77
"os"
8+
"runtime/debug"
89

910
tea "github.com/charmbracelet/bubbletea"
1011
"github.com/tailscale/aperture-cli/internal/profiles"
@@ -20,6 +21,41 @@ var (
2021
buildDate = "unknown"
2122
)
2223

24+
func init() {
25+
info, ok := debug.ReadBuildInfo()
26+
if !ok {
27+
return
28+
}
29+
30+
if buildVersion == "v0.0.0-dev" && info.Main.Version != "" && info.Main.Version != "(devel)" {
31+
buildVersion = info.Main.Version
32+
}
33+
34+
// Only fill in VCS info when ldflags haven't already set these values.
35+
if buildCommit != "unknown" {
36+
return
37+
}
38+
39+
var dirty bool
40+
for _, s := range info.Settings {
41+
switch s.Key {
42+
case "vcs.revision":
43+
if len(s.Value) >= 7 {
44+
buildCommit = s.Value[:7]
45+
}
46+
case "vcs.time":
47+
if buildDate == "unknown" {
48+
buildDate = s.Value
49+
}
50+
case "vcs.modified":
51+
dirty = s.Value == "true"
52+
}
53+
}
54+
if dirty && buildCommit != "unknown" {
55+
buildCommit += "-dirty"
56+
}
57+
}
58+
2359
func main() {
2460
flag.Parse()
2561

0 commit comments

Comments
 (0)