Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/vanityrig/interactive.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
22 changes: 19 additions & 3 deletions cmd/vanityrig/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,33 @@ import (
"flag"
"fmt"
"os"
"runtime/debug"
"strings"
"time"

"github.com/bytestrix/vanityrig/internal/vanity"
)

// 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:
Expand Down Expand Up @@ -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
}

Expand Down
18 changes: 18 additions & 0 deletions cmd/vanityrig/version_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}