From b2c2fdd9aa0f9b93b777a890e77a438b164a8a74 Mon Sep 17 00:00:00 2001 From: Alessio Treglia Date: Mon, 9 Nov 2020 15:25:17 +0000 Subject: [PATCH] enrich version --long's output with build deps The output of the version --long command now shows the list of build dependencies. From: #7848 Closes: #7835 --- version/version.go | 39 ++++++++++++++++++++++++++++++++------- 1 file changed, 32 insertions(+), 7 deletions(-) diff --git a/version/version.go b/version/version.go index 9429a9c1f9f1..9ce86e94c5da 100644 --- a/version/version.go +++ b/version/version.go @@ -18,8 +18,10 @@ package version import ( + "encoding/json" "fmt" "runtime" + "runtime/debug" ) var ( @@ -39,13 +41,14 @@ var ( // Info defines the application version information. type Info struct { - Name string `json:"name" yaml:"name"` - ServerName string `json:"server_name" yaml:"server_name"` - ClientName string `json:"client_name" yaml:"client_name"` - Version string `json:"version" yaml:"version"` - GitCommit string `json:"commit" yaml:"commit"` - BuildTags string `json:"build_tags" yaml:"build_tags"` - GoVersion string `json:"go" yaml:"go"` + Name string `json:"name" yaml:"name"` + ServerName string `json:"server_name" yaml:"server_name"` + ClientName string `json:"client_name" yaml:"client_name"` + Version string `json:"version" yaml:"version"` + GitCommit string `json:"commit" yaml:"commit"` + BuildTags string `json:"build_tags" yaml:"build_tags"` + GoVersion string `json:"go" yaml:"go"` + BuildDeps []buildDep `json:"build_deps" yaml:"build_deps"` } func NewInfo() Info { @@ -57,6 +60,7 @@ func NewInfo() Info { GitCommit: Commit, BuildTags: BuildTags, GoVersion: fmt.Sprintf("go version %s %s/%s", runtime.Version(), runtime.GOOS, runtime.GOARCH), + BuildDeps: depsFromBuildInfo(), } } @@ -68,3 +72,24 @@ build tags: %s vi.Name, vi.Version, vi.GitCommit, vi.BuildTags, vi.GoVersion, ) } + +func depsFromBuildInfo() (deps []buildDep) { + buildInfo, ok := debug.ReadBuildInfo() + if !ok { + return nil + } + + for _, dep := range buildInfo.Deps { + deps = append(deps, buildDep{dep}) + } + + return +} + +type buildDep struct { + *debug.Module +} + +func (d buildDep) String() string { return fmt.Sprintf("%s@%s", d.Path, d.Version) } +func (d buildDep) MarshalJSON() ([]byte, error) { return json.Marshal(d.String()) } +func (d buildDep) MarshalYAML() (interface{}, error) { return d.String(), nil }