Skip to content

Commit

Permalink
goreleaser inject buildinfo
Browse files Browse the repository at this point in the history
  • Loading branch information
nothub committed May 1, 2024
1 parent b1b086f commit a365a98
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 36 deletions.
5 changes: 4 additions & 1 deletion .goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ builds:
ldflags:
- '-s'
- '-w'
- '-X github.com/nothub/mrpack-install/buildinfo.Version={{ .Version }}'
- '-X github.com/nothub/mrpack-install/buildinfo.version={{ .Version }}'
- '-X github.com/nothub/mrpack-install/buildinfo.commit={{ .Commit }}'
- '-X github.com/nothub/mrpack-install/buildinfo.date={{ .Date }}'
- '-X github.com/nothub/mrpack-install/buildinfo.tool=goreleaser'
goos:
- linux
- darwin
Expand Down
55 changes: 22 additions & 33 deletions buildinfo/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,23 @@ import (
"log"
"path"
"runtime/debug"
"strings"
)

const unknown string = "unknown"

var name = unknown
var module = unknown
var (
name = unknown
module = unknown

var version = unknown
var revision = unknown
var dirty = false
version = unknown
commit = unknown
date = unknown
tool = unknown

var arch = unknown
var os = unknown
var compiler = unknown
arch = unknown
os = unknown
compiler = unknown
)

func init() {
bi, ok := debug.ReadBuildInfo()
Expand All @@ -30,12 +32,11 @@ func init() {

name = path.Base(bi.Main.Path)
module = bi.Main.Path
version = bi.Main.Version

var dirty = false

for _, kv := range bi.Settings {
switch kv.Key {
case "vcs.revision":
revision = kv.Value[:8]
case "vcs.modified":
if kv.Value == "true" {
dirty = true
Expand All @@ -49,8 +50,8 @@ func init() {
}
}

if version == unknown {
version = revision
if dirty {
version = version + "+DIRTY"
}
}

Expand All @@ -62,23 +63,11 @@ func Module() string {
return module
}

func Version() string {
return version
}

func Revision() string {
return revision
}

func PrintInfos() {
sb := strings.Builder{}
sb.WriteString(fmt.Sprintf("%s %s", Name(), Version()))
if Version() != Revision() {
sb.WriteString(fmt.Sprintf(" %s", Revision()))
}
sb.WriteString(fmt.Sprintf(" %s-%s-%s", arch, os, compiler))
if dirty {
sb.WriteString(" DIRTY")
}
fmt.Println(sb.String())
func Print() {
fmt.Printf("module: %s\n", module)
fmt.Printf("version: %s\n", version)
fmt.Printf("triplet: %s-%s-%s\n", arch, os, compiler)
fmt.Printf("built at: %s\n", date)
fmt.Printf("built from: %s\n", commit)
fmt.Printf("built with: %s\n", tool)
}
3 changes: 1 addition & 2 deletions web/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,8 @@ func NewTransport() *http.Transport {

func UserAgent() string {
return fmt.Sprintf(
"%s/%s (+https://%s)",
"%s (+https://%s)",
buildinfo.Name(),
buildinfo.Version(),
buildinfo.Module(),
)
}
Expand Down

0 comments on commit a365a98

Please sign in to comment.