From 0c76a6b4b09dc595ea376c27bd8a85a50daefa6a Mon Sep 17 00:00:00 2001 From: Souma <101255979+5ouma@users.noreply.github.com> Date: Fri, 10 May 2024 16:25:08 +0900 Subject: [PATCH] fix(ver): Show version correctly (#90) Not Git original one, but from Go official. --- .github/goreleaser.yml | 4 +++- lib/version.go | 22 ++++++++++++---------- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/.github/goreleaser.yml b/.github/goreleaser.yml index e5e77d0..c8bc840 100644 --- a/.github/goreleaser.yml +++ b/.github/goreleaser.yml @@ -4,13 +4,15 @@ before: hooks: - go mod tidy +gomod: + proxy: true + builds: - flags: - -trimpath ldflags: - -s - -w - - -X "{{ .ModulePath }}/lib.version=v{{ .Version }}" goos: - darwin goarch: diff --git a/lib/version.go b/lib/version.go index 961b6e6..472d5b1 100644 --- a/lib/version.go +++ b/lib/version.go @@ -3,19 +3,21 @@ package lib import ( "fmt" "runtime/debug" + "strings" ) -var version = "HEAD" - func Version() string { - rev := "unknown" - if info, ok := debug.ReadBuildInfo(); ok { - for _, setting := range info.Settings { - if setting.Key == "vcs.revision" { - rev = fmt.Sprintf("#%s", setting.Value[:7]) - break - } + info, ok := debug.ReadBuildInfo() + if !ok { + return "unknown" + } + versions := strings.Split(info.Main.Version, "-") + if len(versions) < 3 { + if versions[0] == "(devel)" { + return "unknown" } + return versions[0] } - return fmt.Sprintf("%s (%s)", version, rev) + return fmt.Sprintf("%s (#%s)", versions[0], versions[2][:7]) + }