diff --git a/cmd/vanityrig/interactive.go b/cmd/vanityrig/interactive.go index 62037b7..ad25bd4 100644 --- a/cmd/vanityrig/interactive.go +++ b/cmd/vanityrig/interactive.go @@ -25,7 +25,7 @@ func runInteractive(patterns []string, out string, mode vanity.MatchMode, rate f Rate: rate, StopAfter: stopAfter, EnginePath: enginePath, - Version: version, + Version: resolveVersion(), }) p := tea.NewProgram(m, tea.WithAltScreen(), tea.WithMouseCellMotion()) diff --git a/cmd/vanityrig/main.go b/cmd/vanityrig/main.go index 2ed148f..1d6fe72 100644 --- a/cmd/vanityrig/main.go +++ b/cmd/vanityrig/main.go @@ -12,6 +12,7 @@ import ( "flag" "fmt" "os" + "runtime/debug" "strings" "time" @@ -19,10 +20,25 @@ import ( ) // version is set at build time via -ldflags "-X main.version=...". Release -// binaries get their real tag; anything built with plain `go build` or -// `go install` stays "dev" rather than lying about which release it is. +// binaries get their real tag. Plain `go install ...@vX.Y.Z` leaves this as +// "dev"; resolveVersion then falls back to module build info. var version = "dev" +func resolveVersion() string { + if version != "" && version != "dev" { + return version + } + info, ok := debug.ReadBuildInfo() + if !ok || info == nil { + return version + } + v := info.Main.Version + if v == "" || v == "(devel)" { + return version + } + return v +} + const usage = `vanityrig - find a vanity .onion address Usage: @@ -79,7 +95,7 @@ func run(args []string) int { return 0 } if args[0] == "-version" || args[0] == "--version" { - fmt.Println("vanityrig", version) + fmt.Println("vanityrig", resolveVersion()) return 0 } diff --git a/cmd/vanityrig/version_test.go b/cmd/vanityrig/version_test.go new file mode 100644 index 0000000..8d78885 --- /dev/null +++ b/cmd/vanityrig/version_test.go @@ -0,0 +1,18 @@ +package main + +import "testing" + +func TestResolveVersionFallsBackToDevWithoutModuleVersion(t *testing.T) { + // Under `go test`, ReadBuildInfo reports "(devel)" for the main module, so + // resolveVersion must keep the ldflags/default value. + old := version + t.Cleanup(func() { version = old }) + version = "dev" + if got := resolveVersion(); got != "dev" { + t.Fatalf("resolveVersion() = %q, want %q", got, "dev") + } + version = "1.2.3" + if got := resolveVersion(); got != "1.2.3" { + t.Fatalf("resolveVersion() with ldflags = %q, want 1.2.3", got) + } +}