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
|
|
|
ShowDeps bool = true
|
2022-08-29 01:24:57 +02:00
|
|
|
|
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 02:33:53 +02:00
|
|
|
func Print() {
|
|
|
|
Fprint(os.Stdout)
|
2022-08-29 01:24:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func Fprint(w io.Writer) {
|
|
|
|
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 02:33:53 +02:00
|
|
|
if OS == "" || Arch == "" || ShowDeps {
|
|
|
|
fillDebugInfo()
|
2022-08-29 01:24:57 +02:00
|
|
|
}
|
|
|
|
|
2022-08-29 02:33:53 +02:00
|
|
|
b := new(strings.Builder)
|
|
|
|
|
|
|
|
err := fmtTmpl.Execute(b, newBuildInfo())
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|