diff --git a/summary.go b/summary.go index bc1daf0..005ebd9 100644 --- a/summary.go +++ b/summary.go @@ -1,8 +1,10 @@ package binfo import ( + "embed" "fmt" "strings" + "text/template" ) type SummaryMode uint @@ -15,7 +17,20 @@ const ( ModeMultiline ) -func (b Binfo) Summarize(name string, version string, mode SummaryMode) string { +type params struct { + Module bool + Build bool + CGO bool + VCS bool + Brk string + Sep string + I Binfo +} + +//go:embed summary.tmpl +var st string + +func (b Binfo) Summarize(name string, version string, mode SummaryMode) (string, error) { wants := func(test SummaryMode) bool { return mode&test == test } @@ -33,55 +48,31 @@ func (b Binfo) Summarize(name string, version string, mode SummaryMode) string { sep = ", " } - lines := make([]string, 0, 4) - - if wants(ModeModule) { - lines = append( - lines, - fmt.Sprintf("module %s (%s) (sum %s)", b.Module.Path, b.Module.Version, b.Module.Sum), - ) + t, err := template.New("").Parse(st) + if err != nil { + return "", fmt.Errorf("cannot parse summary template: %w", err) + } + sb := new(strings.Builder) + err = t.Execute(sb, params{ + Module: wants(ModeModule), + Build: wants(ModeBuild), + CGO: wants(ModeCGO), + VCS: wants(ModeVCS), + Brk: brk, + Sep: sep, + I: b, + }) + if err != nil { + return "", fmt.Errorf("cannot execute summary template: %w", err) } - 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) { - var m string - if b.VCS.Modified { - m = " (modified)" - } else { - m = "" - } - - lines = append( - lines, - fmt.Sprintf("via %s (rev %s) (at %s)%s", b.VCS.Name, b.VCS.Revision, b.VCS.Time.Format("2006-01-02 15:04:05"), m), - ) - } - - j := strings.Join(lines, sep) - - if name == "" { - return j - } else { - return fmt.Sprintf("%s %s:%s%s", name, version, brk, j) - } + 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 } diff --git a/summary.tmpl b/summary.tmpl new file mode 100644 index 0000000..2a03e4e --- /dev/null +++ b/summary.tmpl @@ -0,0 +1,29 @@ +{{- if ne .Name "" -}} + {{ .Name }}{{ if ne .Version "" }} {{ .Version }}{{ end }}{{ .Brk }} +{{- end -}} + +{{- if .Module -}} + module {{ .I.Module.Path }} ({{ I.Module.Version }}) (sum {{ .I.Module.Sum }}) +{{- end -}} + +{{- .Sep -}} + +{{- if .Build -}} + built with {{ .I.Build.Compiler }} ({{ I.Build.GoVersion }}) (mode {{ .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 }}") + {{- else -}} + without cgo + {{- end -}} +{{- end -}} + +{{- .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 -}} +{{- end -}}