mirror of
https://codeberg.org/cunicu/cunicu.git
synced 2025-09-26 21:01:14 +08:00
buildinfo: improve version output
Signed-off-by: Steffen Vogel <post@steffenvogel.de>
This commit is contained in:
@@ -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:
|
||||
|
4
Makefile
4
Makefile
@@ -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)
|
||||
|
@@ -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)
|
||||
|
@@ -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 {
|
||||
|
@@ -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
|
||||
|
Reference in New Issue
Block a user