Skip to content

Commit

Permalink
fix(ver): Show version correctly (#90)
Browse files Browse the repository at this point in the history
Not Git original one, but from Go official.
  • Loading branch information
5ouma authored May 10, 2024
1 parent bf890e5 commit 0c76a6b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
4 changes: 3 additions & 1 deletion .github/goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
22 changes: 12 additions & 10 deletions lib/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -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])

}

0 comments on commit 0c76a6b

Please sign in to comment.