1
0
Fork 0

Compare commits

...

10 commits

Author SHA1 Message Date
83a6098724
fix 2024-12-17 17:32:04 +01:00
dc04fe694e
idk 2024-12-17 09:25:45 +01:00
a2c76aabc2
idk 2024-12-17 09:20:21 +01:00
eca4a3e927 fix 2024-12-15 17:01:07 +01:00
111c621643 fix 2024-12-15 16:48:38 +01:00
4545f636ab fix 2024-12-15 16:17:48 +01:00
f39aec1070 fix 2024-12-15 16:15:49 +01:00
bad1448bdd fix 2024-12-15 16:14:21 +01:00
2e5cac9ea6 fix 2024-12-15 16:10:14 +01:00
136397686a fix 2024-12-15 10:07:31 +00:00
3 changed files with 140 additions and 96 deletions

102
get.go
View file

@ -1,6 +1,7 @@
package binfo package binfo
import ( import (
"errors"
"fmt" "fmt"
"runtime/debug" "runtime/debug"
"strconv" "strconv"
@ -10,63 +11,68 @@ import (
) )
func Get() (Binfo, error) { func Get() (Binfo, error) {
o, ok := debug.ReadBuildInfo()
if !ok {
return Binfo{}, errors.New("unable to read build info")
}
var merr *multierror.Error var merr *multierror.Error
b := Binfo{} b := Binfo{}
if o, ok := debug.ReadBuildInfo(); ok { b.Orig = o
b.Orig = o
b.Module.Version = o.Main.Version b.Module.Version = o.Main.Version
b.Module.Path = o.Main.Path b.Module.Path = o.Main.Path
b.Module.Sum = o.Main.Sum b.Module.Sum = o.Main.Sum
for _, setting := range o.Settings { b.Build.GoVersion = o.GoVersion
switch setting.Key {
case "-buildmode":
b.Build.Mode = setting.Value
case "-compiler":
b.Build.Compiler = setting.Value
case "GOARCH":
b.Build.Arch = setting.Value
case "GOOS":
b.Build.OS = setting.Value
case "CGO_ENABLED": for _, setting := range o.Settings {
switch setting.Value { switch setting.Key {
case "1": case "-buildmode":
b.CGO.Enabled = true b.Build.Mode = setting.Value
case "0": case "-compiler":
b.CGO.Enabled = false b.Build.Compiler = setting.Value
default: case "GOARCH":
merr = multierror.Append(merr, fmt.Errorf("failed to parse %s", setting.Key)) b.Build.Arch = setting.Value
} case "GOOS":
case "CGO_CFLAGS": b.Build.OS = setting.Value
b.CGO.Flags.C = setting.Value
case "CGO_CPPFLAGS":
b.CGO.Flags.CPP = setting.Value
case "CGO_CXXFLAGS":
b.CGO.Flags.CXX = setting.Value
case "CGO_LDFLAGS":
b.CGO.Flags.LD = setting.Value
case "vcs": case "CGO_ENABLED":
b.VCS.Name = setting.Value switch setting.Value {
case "vcs.revision": case "1":
b.VCS.Revision = setting.Value b.CGO.Enabled = true
case "vcs.time": case "0":
v, err := time.Parse(time.RFC3339, setting.Value) b.CGO.Enabled = false
if err != nil { default:
merr = multierror.Append(merr, fmt.Errorf("unable to parse VCS time: %w", err)) merr = multierror.Append(merr, fmt.Errorf("failed to parse %s", setting.Key))
}
b.VCS.Time = v
case "vcs.modified":
v, err := strconv.ParseBool(setting.Value)
if err != nil {
merr = multierror.Append(merr, fmt.Errorf("unable to parse VCS modified: %w", err))
}
b.VCS.Modified = v
} }
case "CGO_CFLAGS":
b.CGO.Flags.C = setting.Value
case "CGO_CPPFLAGS":
b.CGO.Flags.CPP = setting.Value
case "CGO_CXXFLAGS":
b.CGO.Flags.CXX = setting.Value
case "CGO_LDFLAGS":
b.CGO.Flags.LD = setting.Value
case "vcs":
b.VCS.Name = setting.Value
case "vcs.revision":
b.VCS.Revision = setting.Value
case "vcs.time":
v, err := time.Parse(time.RFC3339, setting.Value)
if err != nil {
merr = multierror.Append(merr, fmt.Errorf("unable to parse VCS time: %w", err))
}
b.VCS.Time = v
case "vcs.modified":
v, err := strconv.ParseBool(setting.Value)
if err != nil {
merr = multierror.Append(merr, fmt.Errorf("unable to parse VCS modified: %w", err))
}
b.VCS.Modified = v
} }
} }

View file

@ -1,18 +1,40 @@
package binfo package binfo
import ( import (
"fmt" _ "embed"
"strings" "strings"
"text/template"
) )
type SummaryMode uint type SummaryMode uint
const ( const (
ModeModule SummaryMode = 1 << iota Module SummaryMode = 1 << iota
ModeBuild Build
ModeCGO CGO
ModeVCS VCS
ModeMultiline Multiline
)
type params struct {
Name string
Version string
Module bool
Build bool
CGO bool
VCS bool
Brk string
Sep string
I Binfo
}
var (
//go:embed summary.tmpl
st string
t, _ = template.New("").Parse(st)
) )
func (b Binfo) Summarize(name string, version string, mode SummaryMode) string { func (b Binfo) Summarize(name string, version string, mode SummaryMode) string {
@ -25,7 +47,7 @@ func (b Binfo) Summarize(name string, version string, mode SummaryMode) string {
sep string sep string
) )
if wants(ModeMultiline) { if wants(Multiline) {
brk = "\n" brk = "\n"
sep = "\n" sep = "\n"
} else { } else {
@ -33,48 +55,24 @@ func (b Binfo) Summarize(name string, version string, mode SummaryMode) string {
sep = ", " sep = ", "
} }
lines := make([]string, 4) sb := new(strings.Builder)
err := t.Execute(sb, params{
Name: name,
Version: version,
if wants(ModeModule) { Module: wants(Module),
lines = append( Build: wants(Build),
lines, CGO: wants(CGO),
fmt.Sprintf("module %s (%s) (sum %s)", b.Module.Path, b.Module.Version, b.Module.Sum), VCS: wants(VCS),
)
Brk: brk,
Sep: sep,
I: b,
})
if err != nil {
return ""
} }
if wants(ModeBuild) { return sb.String()
lines = append(
lines,
fmt.Sprintf("built with %s (%s) (mode %s)", b.Build.Compiler, b.Build.GoVersion, b.Build.Mode),
)
}
if wants(ModeCGO) {
if b.CGO.Enabled {
lines = append(
lines,
fmt.Sprintf("with cgo (c %q) (cpp %q) (cxx %q) (ld %q)", b.CGO.Flags.C, b.CGO.Flags.CPP, b.CGO.Flags.CXX, b.CGO.Flags.LD),
)
} else {
lines = append(
lines,
"without cgo",
)
}
}
if wants(ModeVCS) {
lines = append(
lines,
fmt.Sprintf("via %s (rev %s) (at %s)", b.VCS.Name, b.VCS.Revision, b.VCS.Time.Format("2006-01-02 15:04:05")),
)
}
j := strings.Join(lines, sep)
if name == "" {
return j
} else {
return fmt.Sprintf("%s %s:%s%s", name, version, brk, j)
}
} }

40
summary.tmpl Normal file
View file

@ -0,0 +1,40 @@
{{- if ne .Name "" -}}
{{ .Name }}{{ if ne .Version "" }} {{ .Version }}{{ end }}
{{- .Brk -}}
{{- end -}}
{{- if .Module -}}
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 }}) ({{ .I.Build.Mode }})
{{- end -}}
{{- .Sep -}}
{{- if .CGO -}}
{{- if .I.CGO.Enabled -}}
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 -}}
{{- end -}}
{{- .Sep -}}
{{- if .VCS -}}
{{- $time := .I.VCS.Time.Format "2006-01-02 15:04:05" -}}
vcs
{{- if ne .I.VCS.Name "" }} {{ .I.VCS.Name }}{{- end -}}
{{- if ne .I.VCS.Revision "" }} (revision {{ .I.VCS.Revision }}){{- end -}}
{{- if ne $time "" }} (at {{ $time }}){{- end -}}
{{- if .I.VCS.Modified }} (modified){{- end -}}
{{- end -}}