From 136397686ac970a80b57525735dd9507d486fcba Mon Sep 17 00:00:00 2001 From: Lukas Wurzinger Date: Sun, 15 Dec 2024 10:07:31 +0000 Subject: [PATCH 01/10] fix --- summary.go | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/summary.go b/summary.go index 040a053..bc1daf0 100644 --- a/summary.go +++ b/summary.go @@ -33,7 +33,7 @@ func (b Binfo) Summarize(name string, version string, mode SummaryMode) string { sep = ", " } - lines := make([]string, 4) + lines := make([]string, 0, 4) if wants(ModeModule) { lines = append( @@ -64,9 +64,16 @@ func (b Binfo) Summarize(name string, version string, mode SummaryMode) string { } 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)", b.VCS.Name, b.VCS.Revision, b.VCS.Time.Format("2006-01-02 15:04:05")), + 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), ) } From 2e5cac9ea6341310b2e0a0bf39ff3a07d3b01859 Mon Sep 17 00:00:00 2001 From: Lukas Wurzinger Date: Sun, 15 Dec 2024 16:10:14 +0100 Subject: [PATCH 02/10] fix --- summary.go | 91 +++++++++++++++++++++++----------------------------- summary.tmpl | 29 +++++++++++++++++ 2 files changed, 70 insertions(+), 50 deletions(-) create mode 100644 summary.tmpl 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 -}} From bad1448bddf409d2f81f163ec5010d50d3e72e53 Mon Sep 17 00:00:00 2001 From: Lukas Wurzinger Date: Sun, 15 Dec 2024 16:14:21 +0100 Subject: [PATCH 03/10] fix --- summary.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/summary.go b/summary.go index 005ebd9..09a3958 100644 --- a/summary.go +++ b/summary.go @@ -1,7 +1,7 @@ package binfo import ( - "embed" + _ "embed" "fmt" "strings" "text/template" From f39aec1070c74eaafc46d1e82e39c480f7ae0967 Mon Sep 17 00:00:00 2001 From: Lukas Wurzinger Date: Sun, 15 Dec 2024 16:15:49 +0100 Subject: [PATCH 04/10] fix --- summary.tmpl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/summary.tmpl b/summary.tmpl index 2a03e4e..6ae61dc 100644 --- a/summary.tmpl +++ b/summary.tmpl @@ -3,20 +3,20 @@ {{- end -}} {{- if .Module -}} - module {{ .I.Module.Path }} ({{ I.Module.Version }}) (sum {{ .I.Module.Sum }}) + 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 }}) + 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 }}") + 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 -}} From 4545f636ab8f8c8b0f9cd18a9871800047b5af73 Mon Sep 17 00:00:00 2001 From: Lukas Wurzinger Date: Sun, 15 Dec 2024 16:17:48 +0100 Subject: [PATCH 05/10] fix --- summary.go | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/summary.go b/summary.go index 09a3958..d13297a 100644 --- a/summary.go +++ b/summary.go @@ -18,13 +18,18 @@ const ( ) type params struct { + Name string + Version string + Module bool Build bool CGO bool VCS bool - Brk string - Sep string - I Binfo + + Brk string + Sep string + + I Binfo } //go:embed summary.tmpl From 111c621643215fe1fa83c0b849a6eb097e719871 Mon Sep 17 00:00:00 2001 From: Lukas Wurzinger Date: Sun, 15 Dec 2024 16:48:38 +0100 Subject: [PATCH 06/10] fix --- get.go | 102 ++++++++++++++++++++++++++------------------------- summary.go | 37 +++++++------------ summary.tmpl | 17 ++++++--- 3 files changed, 78 insertions(+), 78 deletions(-) diff --git a/get.go b/get.go index 3971b2b..3d3ed8f 100644 --- a/get.go +++ b/get.go @@ -1,6 +1,7 @@ package binfo import ( + "errors" "fmt" "runtime/debug" "strconv" @@ -10,63 +11,66 @@ 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{} - if o, ok := debug.ReadBuildInfo(); ok { - b.Orig = o + 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 - 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 "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_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 "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 d13297a..f8697f0 100644 --- a/summary.go +++ b/summary.go @@ -2,7 +2,6 @@ package binfo import ( _ "embed" - "fmt" "strings" "text/template" ) @@ -10,11 +9,11 @@ import ( type SummaryMode uint const ( - ModeModule SummaryMode = 1 << iota - ModeBuild - ModeCGO - ModeVCS - ModeMultiline + Module SummaryMode = 1 << iota + Build + CGO + VCS + Multiline ) type params struct { @@ -35,7 +34,7 @@ type params struct { //go:embed summary.tmpl 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 { return mode&test == test } @@ -45,7 +44,7 @@ func (b Binfo) Summarize(name string, version string, mode SummaryMode) (string, sep string ) - if wants(ModeMultiline) { + if wants(Multiline) { brk = "\n" sep = "\n" } else { @@ -55,29 +54,21 @@ func (b Binfo) Summarize(name string, version string, mode SummaryMode) (string, t, err := template.New("").Parse(st) if err != nil { - return "", fmt.Errorf("cannot parse summary template: %w", err) + return "" } sb := new(strings.Builder) err = t.Execute(sb, params{ - Module: wants(ModeModule), - Build: wants(ModeBuild), - CGO: wants(ModeCGO), - VCS: wants(ModeVCS), + Module: wants(Module), + Build: wants(Build), + CGO: wants(CGO), + VCS: wants(VCS), Brk: brk, Sep: sep, I: b, }) if err != nil { - return "", fmt.Errorf("cannot execute summary template: %w", err) + return "" } - 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 + return sb.String() } diff --git a/summary.tmpl b/summary.tmpl index 6ae61dc..f0bf85e 100644 --- a/summary.tmpl +++ b/summary.tmpl @@ -1,22 +1,27 @@ {{- if ne .Name "" -}} - {{ .Name }}{{ if ne .Version "" }} {{ .Version }}{{ end }}{{ .Brk }} + {{ .Name }}{{ if ne .Version "" }} {{ .Version }}{{ end }} + {{- .Brk -}} {{- end -}} {{- 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 -}} {{- .Sep -}} {{- 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 -}} {{- .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 }}") + 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 -}} @@ -24,6 +29,6 @@ {{- .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 -}} +{{- if and .VCS (ne .I.VCS.Name "") -}} + 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 -}} From eca4a3e92748381df9ae22b07bf00c5825cb01ea Mon Sep 17 00:00:00 2001 From: Lukas Wurzinger Date: Sun, 15 Dec 2024 17:01:07 +0100 Subject: [PATCH 07/10] fix --- get.go | 2 ++ summary.tmpl | 10 ++++++++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/get.go b/get.go index 3d3ed8f..2e0e3a1 100644 --- a/get.go +++ b/get.go @@ -26,6 +26,8 @@ func Get() (Binfo, error) { 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": diff --git a/summary.tmpl b/summary.tmpl index f0bf85e..d3284ea 100644 --- a/summary.tmpl +++ b/summary.tmpl @@ -29,6 +29,12 @@ {{- .Sep -}} -{{- if and .VCS (ne .I.VCS.Name "") -}} - 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 -}} +{{- 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 -}} From a2c76aabc2b2536cb1fffbcbc3fc72f1615d1cf9 Mon Sep 17 00:00:00 2001 From: Lukas Wurzinger Date: Tue, 17 Dec 2024 09:20:21 +0100 Subject: [PATCH 08/10] idk --- summary.go | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/summary.go b/summary.go index f8697f0..46c2076 100644 --- a/summary.go +++ b/summary.go @@ -31,8 +31,11 @@ type params struct { I Binfo } -//go:embed summary.tmpl -var st string +var ( + //go:embed summary.tmpl + st string + t, _ = template.New("").Parse(st) +) func (b Binfo) Summarize(name string, version string, mode SummaryMode) string { wants := func(test SummaryMode) bool { @@ -52,12 +55,8 @@ func (b Binfo) Summarize(name string, version string, mode SummaryMode) string { sep = ", " } - t, err := template.New("").Parse(st) - if err != nil { - return "" - } sb := new(strings.Builder) - err = t.Execute(sb, params{ + err := t.Execute(sb, params{ Module: wants(Module), Build: wants(Build), CGO: wants(CGO), From dc04fe694e0251e74cfa8b1d0c14726a62acdfeb Mon Sep 17 00:00:00 2001 From: Lukas Wurzinger Date: Tue, 17 Dec 2024 09:25:45 +0100 Subject: [PATCH 09/10] idk --- devenv.nix | 2 ++ 1 file changed, 2 insertions(+) diff --git a/devenv.nix b/devenv.nix index 0c2cafe..be32a04 100644 --- a/devenv.nix +++ b/devenv.nix @@ -1,6 +1,8 @@ { languages.go.enable = true; + # TODO test + pre-commit.hooks = { # Go gotest.enable = true; From 83a60987246a1877eeee60eb8fbdb59de2fe8aaa Mon Sep 17 00:00:00 2001 From: Lukas Wurzinger Date: Tue, 17 Dec 2024 17:32:04 +0100 Subject: [PATCH 10/10] fix --- devenv.nix | 2 -- summary.go | 11 ++++++++--- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/devenv.nix b/devenv.nix index be32a04..0c2cafe 100644 --- a/devenv.nix +++ b/devenv.nix @@ -1,8 +1,6 @@ { languages.go.enable = true; - # TODO test - pre-commit.hooks = { # Go gotest.enable = true; diff --git a/summary.go b/summary.go index 46c2076..5f6b34e 100644 --- a/summary.go +++ b/summary.go @@ -57,13 +57,18 @@ func (b Binfo) Summarize(name string, version string, mode SummaryMode) string { sb := new(strings.Builder) err := t.Execute(sb, params{ + Name: name, + Version: version, + Module: wants(Module), Build: wants(Build), CGO: wants(CGO), VCS: wants(VCS), - Brk: brk, - Sep: sep, - I: b, + + Brk: brk, + Sep: sep, + + I: b, }) if err != nil { return ""