This commit is contained in:
Lukas Wurzinger 2024-12-15 16:48:38 +01:00
parent 4545f636ab
commit 111c621643
3 changed files with 78 additions and 78 deletions

8
get.go
View file

@ -1,6 +1,7 @@
package binfo
import (
"errors"
"fmt"
"runtime/debug"
"strconv"
@ -10,11 +11,15 @@ import (
)
func Get() (Binfo, error) {
o, ok := debug.ReadBuildInfo()
if !ok {
return Binfo{}, errors.New("unable to read build info")
}
var merr *multierror.Error
b := Binfo{}
if o, ok := debug.ReadBuildInfo(); ok {
b.Orig = o
b.Module.Version = o.Main.Version
@ -68,7 +73,6 @@ func Get() (Binfo, error) {
b.VCS.Modified = v
}
}
}
return b, merr.ErrorOrNil()
}

View file

@ -2,7 +2,6 @@ package binfo
import (
_ "embed"
"fmt"
"strings"
"text/template"
)
@ -10,11 +9,11 @@ import (
type SummaryMode uint
const (
ModeModule SummaryMode = 1 << iota
ModeBuild
ModeCGO
ModeVCS
ModeMultiline
Module SummaryMode = 1 << iota
Build
CGO
VCS
Multiline
)
type params struct {
@ -35,7 +34,7 @@ type params struct {
//go:embed summary.tmpl
var st string
func (b Binfo) Summarize(name string, version string, mode SummaryMode) (string, error) {
func (b Binfo) Summarize(name string, version string, mode SummaryMode) string {
wants := func(test SummaryMode) bool {
return mode&test == test
}
@ -45,7 +44,7 @@ func (b Binfo) Summarize(name string, version string, mode SummaryMode) (string,
sep string
)
if wants(ModeMultiline) {
if wants(Multiline) {
brk = "\n"
sep = "\n"
} else {
@ -55,29 +54,21 @@ func (b Binfo) Summarize(name string, version string, mode SummaryMode) (string,
t, err := template.New("").Parse(st)
if err != nil {
return "", fmt.Errorf("cannot parse summary template: %w", err)
return ""
}
sb := new(strings.Builder)
err = t.Execute(sb, params{
Module: wants(ModeModule),
Build: wants(ModeBuild),
CGO: wants(ModeCGO),
VCS: wants(ModeVCS),
Module: wants(Module),
Build: wants(Build),
CGO: wants(CGO),
VCS: wants(VCS),
Brk: brk,
Sep: sep,
I: b,
})
if err != nil {
return "", fmt.Errorf("cannot execute summary template: %w", err)
return ""
}
return sb.String(), nil
}
func (b Binfo) MustSummarize(name string, version string, mode SummaryMode) string {
s, err := b.Summarize(name, version, mode)
if err != nil {
panic(err)
}
return s
return sb.String()
}

View file

@ -1,22 +1,27 @@
{{- if ne .Name "" -}}
{{ .Name }}{{ if ne .Version "" }} {{ .Version }}{{ end }}{{ .Brk }}
{{ .Name }}{{ if ne .Version "" }} {{ .Version }}{{ end }}
{{- .Brk -}}
{{- end -}}
{{- if .Module -}}
module {{ .I.Module.Path }} ({{ .I.Module.Version }}) (sum {{ .I.Module.Sum }})
module {{ .I.Module.Path }} {{ .I.Module.Version }}{{ if ne .I.Module.Sum "" }} {{ .I.Module.Sum }}{{ end }}
{{- end -}}
{{- .Sep -}}
{{- if .Build -}}
built with {{ .I.Build.Compiler }} ({{ .I.Build.GoVersion }}) (mode {{ .I.Build.Mode }})
built with {{ .I.Build.Compiler }} ({{ .I.Build.GoVersion }}) ({{ .I.Build.Mode }})
{{- end -}}
{{- .Sep -}}
{{- if .CGO -}}
{{- if .I.CGO.Enabled -}}
with cgo (c "{{ .I.CGO.Flags.C }}") (cpp "{{ .I.CGO.Flags.CPP }}") (cxx "{{ .I.CGO.Flags.CXX }}") (ld "{{ .I.CGO.Flags.LD }}")
with cgo
{{- if ne .I.CGO.Flags.C "" }} (c {{ .I.CGO.Flags.C }}){{- end -}}
{{- if ne .I.CGO.Flags.CPP "" }} (cpp {{ .I.CGO.Flags.CPP }}){{- end -}}
{{- if ne .I.CGO.Flags.CXX "" }} (cxx {{ .I.CGO.Flags.CXX }}){{- end -}}
{{- if ne .I.CGO.Flags.LD "" }} (ld {{ .I.CGO.Flags.LD }}){{- end -}}
{{- else -}}
without cgo
{{- end -}}
@ -24,6 +29,6 @@
{{- .Sep -}}
{{- if .VCS -}}
via {{ .I.VCS.Name }} (rev {{ .I.VCS.Revision }}) (at {{ .I.VCS.Time.Format "2006-01-02 15:04:05" }}){{- if .I.VCS.Modified -}} (modified){{- end -}}
{{- if and .VCS (ne .I.VCS.Name "") -}}
vcs {{ .I.VCS.Name }} (rev {{ .I.VCS.Revision }}) (at {{ .I.VCS.Time.Format "2006-01-02 15:04:05" }}){{- if .I.VCS.Modified }} (modified){{- end -}}
{{- end -}}