From 50e8d2692a64ba75d6771a968db7f95223ab2e30 Mon Sep 17 00:00:00 2001 From: Giuseppe Grieco Date: Tue, 27 Jan 2026 15:59:41 +0100 Subject: [PATCH] fix: show version info when installed via go install --- .goreleaser.yaml | 6 ++++-- internal/version/version.go | 29 +++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/.goreleaser.yaml b/.goreleaser.yaml index 469b6e6..f2bbf2d 100644 --- a/.goreleaser.yaml +++ b/.goreleaser.yaml @@ -33,7 +33,8 @@ builds: archives: - id: default - format: tar.gz + formats: + - tar.gz name_template: >- {{ .ProjectName }}_ {{- .Version }}_ @@ -42,7 +43,8 @@ archives: {{- if .Arm }}v{{ .Arm }}{{ end }} format_overrides: - goos: windows - format: zip + formats: + - zip files: - LICENSE - README.md diff --git a/internal/version/version.go b/internal/version/version.go index f829c40..20bd94e 100644 --- a/internal/version/version.go +++ b/internal/version/version.go @@ -7,6 +7,7 @@ package version import ( "fmt" "runtime" + "runtime/debug" ) // These variables are set at build time using ldflags. @@ -19,6 +20,34 @@ var ( Date = "unknown" ) +func init() { + // If version wasn't set via ldflags, try to get it from build info. + // This works when installed via "go install module@version". + if Version == "dev" { + if info, ok := debug.ReadBuildInfo(); ok && info.Main.Version != "" && info.Main.Version != "(devel)" { + Version = info.Main.Version + } + } + + // Try to get commit and date from build settings if not set via ldflags. + if Commit == "none" || Date == "unknown" { + if info, ok := debug.ReadBuildInfo(); ok { + for _, setting := range info.Settings { + switch setting.Key { + case "vcs.revision": + if Commit == "none" && len(setting.Value) >= 7 { + Commit = setting.Value[:7] + } + case "vcs.time": + if Date == "unknown" { + Date = setting.Value + } + } + } + } + } +} + // Info returns formatted version information. func Info() string { return fmt.Sprintf("daco version %s (commit: %s, built: %s, go: %s)",