2022-08-29 02:33:53 +02:00
|
|
|
package buildinfo
|
2022-08-29 01:24:57 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
2022-08-29 02:33:53 +02:00
|
|
|
"path/filepath"
|
2022-08-29 01:24:57 +02:00
|
|
|
"runtime/debug"
|
2022-08-29 02:33:53 +02:00
|
|
|
"strings"
|
|
|
|
"text/template"
|
2022-08-29 01:24:57 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
Name string = ""
|
|
|
|
Version string = ""
|
|
|
|
Commit string = ""
|
|
|
|
BuildTime string = ""
|
|
|
|
OS string = ""
|
|
|
|
Arch string = ""
|
|
|
|
|
2022-08-29 02:33:53 +02:00
|
|
|
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 }}
|
|
|
|
`
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
fmtTmpl = template.Must(template.New("print-format").Parse(PrintFormat))
|
2022-08-29 01:24:57 +02:00
|
|
|
)
|
|
|
|
|
2022-08-29 10:30:43 +02:00
|
|
|
func Print(options Options) {
|
|
|
|
Fprint(os.Stdout, options)
|
2022-08-29 01:24:57 +02:00
|
|
|
}
|
|
|
|
|
2022-08-29 10:30:43 +02:00
|
|
|
func Fprint(w io.Writer, options Options) {
|
2022-08-29 01:24:57 +02:00
|
|
|
if Name == "" {
|
2022-08-29 02:33:53 +02:00
|
|
|
Name = filepath.Base(os.Args[0])
|
2022-08-29 01:24:57 +02:00
|
|
|
}
|
|
|
|
|
2022-08-29 10:30:43 +02:00
|
|
|
if OS == "" || Arch == "" || options.Deps {
|
2022-08-29 02:33:53 +02:00
|
|
|
fillDebugInfo()
|
2022-08-29 01:24:57 +02:00
|
|
|
}
|
|
|
|
|
2022-08-29 02:33:53 +02:00
|
|
|
b := new(strings.Builder)
|
|
|
|
|
2022-08-29 10:30:43 +02:00
|
|
|
err := fmtTmpl.Execute(b, newBuildInfo(options))
|
2022-08-29 02:33:53 +02:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
2022-08-29 01:24:57 +02:00
|
|
|
}
|
|
|
|
|
2022-08-29 02:33:53 +02:00
|
|
|
fmt.Fprintln(w, strings.TrimSpace(b.String()))
|
2022-08-29 01:24:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func overwriteValueIfEmpty(value *string, newValue string) {
|
|
|
|
if *value == "" {
|
|
|
|
*value = newValue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func fillDebugInfo() {
|
|
|
|
info, ok := debug.ReadBuildInfo()
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, setting := range info.Settings {
|
|
|
|
switch setting.Key {
|
|
|
|
case "GOOS":
|
|
|
|
overwriteValueIfEmpty(&OS, setting.Value)
|
|
|
|
case "GOARCH":
|
|
|
|
overwriteValueIfEmpty(&Arch, setting.Value)
|
|
|
|
case "vcs.revision":
|
|
|
|
overwriteValueIfEmpty(&Commit, setting.Value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, dep := range info.Deps {
|
2022-08-29 02:33:53 +02:00
|
|
|
dependencies = append(dependencies, Dep{
|
|
|
|
Name: dep.Path,
|
|
|
|
Version: dep.Version,
|
|
|
|
Sum: dep.Sum,
|
|
|
|
})
|
2022-08-29 01:24:57 +02:00
|
|
|
}
|
|
|
|
}
|