buildinfo: improve version output

Signed-off-by: Steffen Vogel <post@steffenvogel.de>
This commit is contained in:
Steffen Vogel
2022-09-09 13:38:18 +02:00
parent 02425d847d
commit 0ed03a52a7
5 changed files with 40 additions and 28 deletions

View File

@@ -52,10 +52,10 @@ builds:
- -s -w
- -X github.com/stv0g/cunicu/pkg/util/buildinfo.Version={{.Version}}
- -X github.com/stv0g/cunicu/pkg/util/buildinfo.Commit={{.Commit}}
- -X github.com/stv0g/cunicu/pkg/util/buildinfo.Date={{.Date}}
- -X github.com/stv0g/cunicu/pkg/util/buildinfo.DateStr={{.Date}}
- -X github.com/stv0g/cunicu/pkg/util/buildinfo.Tag={{.Tag}}
- -X github.com/stv0g/cunicu/pkg/util/buildinfo.Branch={{.Branch}}
- -X github.com/stv0g/cunicu/pkg/util/buildinfo.BuiltBy=GoReleaser
- -X github.com/stv0g/cunicu/pkg/util/buildinfo.BuiltBy=goreleaser
archives:
- builds:

View File

@@ -6,8 +6,8 @@ LDFLAGS = -X github.com/stv0g/cunicu/pkg/util/buildinfo.Version=$(shell git desc
-X github.com/stv0g/cunicu/pkg/util/buildinfo.Tag=$(shell git describe --tags) \
-X github.com/stv0g/cunicu/pkg/util/buildinfo.Commit=$(shell git rev-parse HEAD) \
-X github.com/stv0g/cunicu/pkg/util/buildinfo.Branch=$(shell git rev-parse --abbrev-ref HEAD) \
-X github.com/stv0g/cunicu/pkg/util/buildinfo.Date=$(shell date -Iseconds) \
-X github.com/stv0g/cunicu/pkg/util/buildinfo.BuiltBy=Makefile \
-X github.com/stv0g/cunicu/pkg/util/buildinfo.DateStr=$(shell date -Iseconds) \
-X github.com/stv0g/cunicu/pkg/util/buildinfo.BuiltBy=makefile \
PKGS ?= ./cmd/... ./pkg/... ./test
ifeq ($(GOOS),linux)

View File

@@ -75,7 +75,7 @@ func docsManpage(cmd *cobra.Command, args []string) error {
Title: "cunicu",
Section: "3",
Source: "https://github.com/stv0g/cunicu",
Date: buildinfo.BuiltDate,
Date: buildinfo.Date,
}
return doc.GenManTree(rootCmd, header, dir)

View File

@@ -2,6 +2,7 @@ package proto
import (
"fmt"
"strings"
"time"
)
@@ -21,17 +22,28 @@ func (t *Timestamp) Time() time.Time {
}
func (bi *BuildInfo) ToString() string {
commit := bi.Commit
if len(commit) > 8 {
commit = commit[:8]
attrs := []string{
fmt.Sprintf("os=%s", bi.Os),
fmt.Sprintf("arch=%s", bi.Arch),
}
if bi.Commit != "" {
attrs = append(attrs, fmt.Sprintf("commit=%s", bi.Commit[:8]))
}
if bi.Branch != "" {
attrs = append(attrs, fmt.Sprintf("branch=%s", bi.Branch))
}
date := "unknown"
if bi.Date != nil {
date = bi.Date.Time().Format(time.RFC3339)
attrs = append(attrs, fmt.Sprintf("built-at=%s", bi.Date.Time().Format(time.RFC3339)))
}
return fmt.Sprintf("%s (%s, %s/%s, %s)", bi.Version, commit, bi.Os, bi.Arch, date)
if bi.BuiltBy != "" {
attrs = append(attrs, fmt.Sprintf("built-by=%s", bi.BuiltBy))
}
return fmt.Sprintf("%s (%s)", bi.Version, strings.Join(attrs, ", "))
}
func (bi *BuildInfos) ToString() string {

View File

@@ -13,23 +13,23 @@ import (
var (
// set via ldflags -X / goreleaser or from debug.ReadBuildInfo()
Version = "dev"
Commit = "none"
Tag = "unknown"
Branch = "unknown"
BuiltBy = "unknown"
BuiltDateStr = "unknown"
BuiltDate *time.Time
Dirty bool
Version = "dev"
Commit = "none"
Tag = ""
Branch = ""
BuiltBy = "manual"
DateStr = ""
Date *time.Time
Dirty bool
)
func init() {
if Version == "dev" {
_, Commit, Dirty, BuiltDate = ReadVCSInfos()
_, Commit, Dirty, Date = ReadVCSInfos()
} else {
Dirty = strings.Contains(Version, "-dirty")
if bd, err := time.Parse(time.RFC3339, BuiltDateStr); err == nil && !bd.IsZero() {
BuiltDate = &bd
if bd, err := time.Parse(time.RFC3339, DateStr); err == nil && !bd.IsZero() {
Date = &bd
}
}
}
@@ -46,8 +46,8 @@ func BuildInfo() *proto.BuildInfo {
Dirty: Dirty,
}
if BuiltDate != nil {
bi.Date = proto.Time(*BuiltDate)
if Date != nil {
bi.Date = proto.Time(*Date)
}
return bi
@@ -59,14 +59,14 @@ func UserAgent() string {
func ReadVCSInfos() (bool, string, bool, *time.Time) {
if info, ok := debug.ReadBuildInfo(); ok {
rev := "unknown"
commit := ""
dirty := false
var btime *time.Time
for _, v := range info.Settings {
switch v.Key {
case "vcs.revision":
rev = v.Value
commit = v.Value
case "vcs.modified":
dirty = v.Value == "true"
case "vcs.time":
@@ -77,10 +77,10 @@ func ReadVCSInfos() (bool, string, bool, *time.Time) {
}
if dirty {
rev += "-dirty"
commit += "-dirty"
}
return true, rev, dirty, btime
return true, commit, dirty, btime
}
return false, "", false, nil