1
0
Fork 0

Compare commits

..

No commits in common. "83a60987246a1877eeee60eb8fbdb59de2fe8aaa" and "6e37c7e91d0fd52d77cc33eb7dd3301dafd3f0c4" have entirely different histories.

3 changed files with 96 additions and 140 deletions

10
get.go
View file

@ -1,7 +1,6 @@
package binfo package binfo
import ( import (
"errors"
"fmt" "fmt"
"runtime/debug" "runtime/debug"
"strconv" "strconv"
@ -11,23 +10,17 @@ 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
b.Build.GoVersion = o.GoVersion
for _, setting := range o.Settings { for _, setting := range o.Settings {
switch setting.Key { switch setting.Key {
case "-buildmode": case "-buildmode":
@ -75,6 +68,7 @@ func Get() (Binfo, error) {
b.VCS.Modified = v b.VCS.Modified = v
} }
} }
}
return b, merr.ErrorOrNil() return b, merr.ErrorOrNil()
} }

View file

@ -1,40 +1,18 @@
package binfo package binfo
import ( import (
_ "embed" "fmt"
"strings" "strings"
"text/template"
) )
type SummaryMode uint type SummaryMode uint
const ( const (
Module SummaryMode = 1 << iota ModeModule SummaryMode = 1 << iota
Build ModeBuild
CGO ModeCGO
VCS ModeVCS
Multiline ModeMultiline
)
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 {
@ -47,7 +25,7 @@ func (b Binfo) Summarize(name string, version string, mode SummaryMode) string {
sep string sep string
) )
if wants(Multiline) { if wants(ModeMultiline) {
brk = "\n" brk = "\n"
sep = "\n" sep = "\n"
} else { } else {
@ -55,24 +33,48 @@ func (b Binfo) Summarize(name string, version string, mode SummaryMode) string {
sep = ", " sep = ", "
} }
sb := new(strings.Builder) lines := make([]string, 4)
err := t.Execute(sb, params{
Name: name,
Version: version,
Module: wants(Module), if wants(ModeModule) {
Build: wants(Build), lines = append(
CGO: wants(CGO), lines,
VCS: wants(VCS), fmt.Sprintf("module %s (%s) (sum %s)", b.Module.Path, b.Module.Version, b.Module.Sum),
)
Brk: brk,
Sep: sep,
I: b,
})
if err != nil {
return ""
} }
return sb.String() if wants(ModeBuild) {
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)
}
} }

View file

@ -1,40 +0,0 @@
{{- 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 -}}