templating

This commit is contained in:
Timon Ringwald 2022-08-29 02:33:53 +02:00
parent b4077ca224
commit c2cf5fd5c9
2 changed files with 80 additions and 25 deletions

31
buildInfo.go Normal file
View File

@ -0,0 +1,31 @@
package buildinfo
type buildInfo struct {
Name string
Version string
Commit string
BuildTime string
OS string
Arch string
ShowDeps bool
Deps []Dep
}
type Dep struct {
Name string
Version string
Sum string
}
func newBuildInfo() *buildInfo {
return &buildInfo{
Name: Name,
Version: Version,
Commit: Commit,
BuildTime: BuildTime,
OS: OS,
Arch: Arch,
Deps: dependencies,
ShowDeps: ShowDeps,
}
}

View File

@ -1,10 +1,13 @@
package main package buildinfo
import ( import (
"fmt" "fmt"
"io" "io"
"os" "os"
"path/filepath"
"runtime/debug" "runtime/debug"
"strings"
"text/template"
) )
var ( var (
@ -14,40 +17,58 @@ var (
BuildTime string = "" BuildTime string = ""
OS string = "" OS string = ""
Arch string = "" Arch string = ""
ShowDeps bool = false ShowDeps bool = true
dependencies = []string{} dependencies = []Dep{}
PrintFormat = `
{{- block "version" . -}}
{{- if .Name -}} {{- .Name -}} {{- end }}
{{- if .Version }} {{ .Version -}} {{- end }}
{{- if and (.OS) (.Arch) }} {{ .OS -}} / {{- .Arch -}} {{- end }}
{{- if (.BuildTime) }} (built at {{ .BuildTime -}}) {{- end }}
{{- if and (not .Version) (.Commit) (not .BuildTime) }} (commit {{ .Commit -}}) {{- end }}
{{- if and (not .Version) (.Commit) (.BuildTime) }} (commit {{ .Commit }} built at {{ .BuildTime -}}) {{- end }}
{{- if and (not .Version) (not .Commit) (.BuildTime) }} (built at {{ .BuildTime -}}) {{- end }}
{{ end -}}
{{- block "deps" . -}}
{{- if and (.ShowDeps) (len .Deps) -}}
dependecies:
{{- range .Deps }}
{{ .Name }} {{ .Version -}}
{{- end -}}
{{- end -}}
{{- end }}
`
) )
func main() { var (
Print() fmtTmpl = template.Must(template.New("print-format").Parse(PrintFormat))
)
func Print() {
Fprint(os.Stdout)
} }
func Fprint(w io.Writer) { func Fprint(w io.Writer) {
if Name == "" {
Name = filepath.Base(os.Args[0])
}
if OS == "" || Arch == "" || ShowDeps { if OS == "" || Arch == "" || ShowDeps {
fillDebugInfo() fillDebugInfo()
} }
if Name == "" { b := new(strings.Builder)
return
err := fmtTmpl.Execute(b, newBuildInfo())
if err != nil {
panic(err)
} }
if Version != "" { fmt.Fprintln(w, strings.TrimSpace(b.String()))
fmt.Fprintf(w, "%s %s (built for %s/%s at %s)\n", Name, Version, OS, Arch, BuildTime)
} else if Commit != "" {
fmt.Fprintf(w, "%s (commit %s) (built at %s)\n", Name, Commit, BuildTime)
}
if ShowDeps {
fmt.Println("\ndependencies:")
for _, dep := range dependencies {
fmt.Fprintf(w, " %s\n", dep)
}
}
}
func Print() {
Fprint(os.Stdout)
} }
func overwriteValueIfEmpty(value *string, newValue string) { func overwriteValueIfEmpty(value *string, newValue string) {
@ -74,7 +95,10 @@ func fillDebugInfo() {
} }
for _, dep := range info.Deps { for _, dep := range info.Deps {
str := fmt.Sprintf("%s %s", dep.Path, dep.Version) dependencies = append(dependencies, Dep{
dependencies = append(dependencies, str) Name: dep.Path,
Version: dep.Version,
Sum: dep.Sum,
})
} }
} }