fix
This commit is contained in:
parent
4545f636ab
commit
111c621643
102
get.go
102
get.go
|
@ -1,6 +1,7 @@
|
||||||
package binfo
|
package binfo
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"runtime/debug"
|
"runtime/debug"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
@ -10,63 +11,66 @@ 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 {
|
for _, setting := range o.Settings {
|
||||||
switch setting.Key {
|
switch setting.Key {
|
||||||
case "-buildmode":
|
case "-buildmode":
|
||||||
b.Build.Mode = setting.Value
|
b.Build.Mode = setting.Value
|
||||||
case "-compiler":
|
case "-compiler":
|
||||||
b.Build.Compiler = setting.Value
|
b.Build.Compiler = setting.Value
|
||||||
case "GOARCH":
|
case "GOARCH":
|
||||||
b.Build.Arch = setting.Value
|
b.Build.Arch = setting.Value
|
||||||
case "GOOS":
|
case "GOOS":
|
||||||
b.Build.OS = setting.Value
|
b.Build.OS = setting.Value
|
||||||
|
|
||||||
case "CGO_ENABLED":
|
case "CGO_ENABLED":
|
||||||
switch setting.Value {
|
switch setting.Value {
|
||||||
case "1":
|
case "1":
|
||||||
b.CGO.Enabled = true
|
b.CGO.Enabled = true
|
||||||
case "0":
|
case "0":
|
||||||
b.CGO.Enabled = false
|
b.CGO.Enabled = false
|
||||||
default:
|
default:
|
||||||
merr = multierror.Append(merr, fmt.Errorf("failed to parse %s", setting.Key))
|
merr = multierror.Append(merr, fmt.Errorf("failed to parse %s", setting.Key))
|
||||||
}
|
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
37
summary.go
37
summary.go
|
@ -2,7 +2,6 @@ package binfo
|
||||||
|
|
||||||
import (
|
import (
|
||||||
_ "embed"
|
_ "embed"
|
||||||
"fmt"
|
|
||||||
"strings"
|
"strings"
|
||||||
"text/template"
|
"text/template"
|
||||||
)
|
)
|
||||||
|
@ -10,11 +9,11 @@ import (
|
||||||
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 {
|
type params struct {
|
||||||
|
@ -35,7 +34,7 @@ type params struct {
|
||||||
//go:embed summary.tmpl
|
//go:embed summary.tmpl
|
||||||
var st string
|
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 {
|
wants := func(test SummaryMode) bool {
|
||||||
return mode&test == test
|
return mode&test == test
|
||||||
}
|
}
|
||||||
|
@ -45,7 +44,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 {
|
||||||
|
@ -55,29 +54,21 @@ func (b Binfo) Summarize(name string, version string, mode SummaryMode) (string,
|
||||||
|
|
||||||
t, err := template.New("").Parse(st)
|
t, err := template.New("").Parse(st)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", fmt.Errorf("cannot parse summary template: %w", err)
|
return ""
|
||||||
}
|
}
|
||||||
sb := new(strings.Builder)
|
sb := new(strings.Builder)
|
||||||
err = t.Execute(sb, params{
|
err = t.Execute(sb, params{
|
||||||
Module: wants(ModeModule),
|
Module: wants(Module),
|
||||||
Build: wants(ModeBuild),
|
Build: wants(Build),
|
||||||
CGO: wants(ModeCGO),
|
CGO: wants(CGO),
|
||||||
VCS: wants(ModeVCS),
|
VCS: wants(VCS),
|
||||||
Brk: brk,
|
Brk: brk,
|
||||||
Sep: sep,
|
Sep: sep,
|
||||||
I: b,
|
I: b,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", fmt.Errorf("cannot execute summary template: %w", err)
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
return sb.String(), nil
|
return sb.String()
|
||||||
}
|
|
||||||
|
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
17
summary.tmpl
17
summary.tmpl
|
@ -1,22 +1,27 @@
|
||||||
{{- if ne .Name "" -}}
|
{{- if ne .Name "" -}}
|
||||||
{{ .Name }}{{ if ne .Version "" }} {{ .Version }}{{ end }}{{ .Brk }}
|
{{ .Name }}{{ if ne .Version "" }} {{ .Version }}{{ end }}
|
||||||
|
{{- .Brk -}}
|
||||||
{{- end -}}
|
{{- end -}}
|
||||||
|
|
||||||
{{- if .Module -}}
|
{{- 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 -}}
|
{{- end -}}
|
||||||
|
|
||||||
{{- .Sep -}}
|
{{- .Sep -}}
|
||||||
|
|
||||||
{{- if .Build -}}
|
{{- 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 -}}
|
{{- end -}}
|
||||||
|
|
||||||
{{- .Sep -}}
|
{{- .Sep -}}
|
||||||
|
|
||||||
{{- if .CGO -}}
|
{{- if .CGO -}}
|
||||||
{{- if .I.CGO.Enabled -}}
|
{{- 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 -}}
|
{{- else -}}
|
||||||
without cgo
|
without cgo
|
||||||
{{- end -}}
|
{{- end -}}
|
||||||
|
@ -24,6 +29,6 @@
|
||||||
|
|
||||||
{{- .Sep -}}
|
{{- .Sep -}}
|
||||||
|
|
||||||
{{- if .VCS -}}
|
{{- if and .VCS (ne .I.VCS.Name "") -}}
|
||||||
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 -}}
|
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 -}}
|
{{- end -}}
|
||||||
|
|
Loading…
Reference in a new issue