diff --git a/get.go b/get.go index 2e0e3a1..3971b2b 100644 --- a/get.go +++ b/get.go @@ -1,7 +1,6 @@ package binfo import ( - "errors" "fmt" "runtime/debug" "strconv" @@ -11,68 +10,63 @@ 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{} - b.Orig = o + if o, ok := debug.ReadBuildInfo(); ok { + b.Orig = o - b.Module.Version = o.Main.Version - b.Module.Path = o.Main.Path - b.Module.Sum = o.Main.Sum + b.Module.Version = o.Main.Version + b.Module.Path = o.Main.Path + b.Module.Sum = o.Main.Sum - b.Build.GoVersion = o.GoVersion + for _, setting := range o.Settings { + 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 - for _, setting := range o.Settings { - 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": + switch setting.Value { + case "1": + b.CGO.Enabled = true + case "0": + b.CGO.Enabled = false + default: + 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 "CGO_ENABLED": - switch setting.Value { - case "1": - b.CGO.Enabled = true - case "0": - b.CGO.Enabled = false - default: - merr = multierror.Append(merr, fmt.Errorf("failed to parse %s", setting.Key)) + 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 } } diff --git a/summary.go b/summary.go index 5f6b34e..040a053 100644 --- a/summary.go +++ b/summary.go @@ -1,40 +1,18 @@ package binfo import ( - _ "embed" + "fmt" "strings" - "text/template" ) type SummaryMode uint const ( - Module SummaryMode = 1 << iota - Build - CGO - VCS - 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) + ModeModule SummaryMode = 1 << iota + ModeBuild + ModeCGO + ModeVCS + ModeMultiline ) 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 ) - if wants(Multiline) { + if wants(ModeMultiline) { brk = "\n" sep = "\n" } else { @@ -55,24 +33,48 @@ func (b Binfo) Summarize(name string, version string, mode SummaryMode) string { sep = ", " } - sb := new(strings.Builder) - err := t.Execute(sb, params{ - Name: name, - Version: version, + lines := make([]string, 4) - Module: wants(Module), - Build: wants(Build), - CGO: wants(CGO), - VCS: wants(VCS), - - Brk: brk, - Sep: sep, - - I: b, - }) - if err != nil { - return "" + if wants(ModeModule) { + lines = append( + lines, + fmt.Sprintf("module %s (%s) (sum %s)", b.Module.Path, b.Module.Version, b.Module.Sum), + ) } - 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) + } } diff --git a/summary.tmpl b/summary.tmpl deleted file mode 100644 index d3284ea..0000000 --- a/summary.tmpl +++ /dev/null @@ -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 -}}