From e172a0833351102d53a94c9dcf4747aec64b8dc1 Mon Sep 17 00:00:00 2001 From: Alessio Treglia Date: Mon, 9 Nov 2020 15:17:36 +0000 Subject: [PATCH] enrich versioning with build deps (#7848) `version --long` output now shows the list of build dependencies. Redirect version's output to `stdout`. --- go.sum | 2 -- version/command.go | 1 + version/version.go | 37 +++++++++++++++++++++++++++++++------ version/version_test.go | 16 ++++++++-------- 4 files changed, 40 insertions(+), 16 deletions(-) diff --git a/go.sum b/go.sum index 6b5e5ed52..8090e2d10 100644 --- a/go.sum +++ b/go.sum @@ -595,8 +595,6 @@ github.com/tendermint/crypto v0.0.0-20191022145703-50d29ede1e15/go.mod h1:z4YtwM github.com/tendermint/go-amino v0.16.0 h1:GyhmgQKvqF82e2oZeuMSp9JTN0N09emoSZlb2lyGa2E= github.com/tendermint/go-amino v0.16.0/go.mod h1:TQU0M1i/ImAo+tYpZi73AU3V/dKeCoMC9Sphe2ZwGME= github.com/tendermint/tendermint v0.34.0-rc4/go.mod h1:yotsojf2C1QBOw4dZrTcxbyxmPUrT4hNuOQWX9XUwB4= -github.com/tendermint/tendermint v0.34.0-rc5 h1:2bnQfWyOMfTCbol5pwB8CgM2nxi6/Kz6zqlS6Udm/Cg= -github.com/tendermint/tendermint v0.34.0-rc5/go.mod h1:yotsojf2C1QBOw4dZrTcxbyxmPUrT4hNuOQWX9XUwB4= github.com/tendermint/tendermint v0.34.0-rc6 h1:SVuKGvvE22KxfuK8QUHctUrmOWJsncZSYXIYtcnoKN0= github.com/tendermint/tendermint v0.34.0-rc6/go.mod h1:ugzyZO5foutZImv0Iyx/gOFCX6mjJTgbLHTwi17VDVg= github.com/tendermint/tm-db v0.6.2 h1:DOn8jwCdjJblrCFJbtonEIPD1IuJWpbRUUdR8GWE4RM= diff --git a/version/command.go b/version/command.go index 9291c0a82..bb632aea5 100644 --- a/version/command.go +++ b/version/command.go @@ -17,6 +17,7 @@ func NewVersionCommand() *cobra.Command { Short: "Print the application binary version information", RunE: func(cmd *cobra.Command, _ []string) error { verInfo := NewInfo() + cmd.SetOut(cmd.OutOrStdout()) if long, _ := cmd.Flags().GetBool(flagLong); !long { cmd.Println(verInfo.Version) diff --git a/version/version.go b/version/version.go index 1e40d483d..25995932f 100644 --- a/version/version.go +++ b/version/version.go @@ -17,8 +17,10 @@ package version import ( + "encoding/json" "fmt" "runtime" + "runtime/debug" ) var ( @@ -36,12 +38,13 @@ var ( // Info defines the application version information. type Info struct { - Name string `json:"name" yaml:"name"` - AppName string `json:"server_name" yaml:"server_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"` + AppName string `json:"server_name" yaml:"server_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 { @@ -52,6 +55,7 @@ func NewInfo() Info { GitCommit: Commit, BuildTags: BuildTags, GoVersion: fmt.Sprintf("go version %s %s/%s", runtime.Version(), runtime.GOOS, runtime.GOARCH), + BuildDeps: depsFromBuildInfo(), } } @@ -63,3 +67,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 } diff --git a/version/version_test.go b/version/version_test.go index ef44f8d31..87608cfdb 100644 --- a/version/version_test.go +++ b/version/version_test.go @@ -1,4 +1,4 @@ -package version +package version_test import ( "encoding/json" @@ -11,10 +11,11 @@ import ( "github.com/tendermint/tendermint/libs/cli" "github.com/cosmos/cosmos-sdk/testutil" + "github.com/cosmos/cosmos-sdk/version" ) func TestNewInfo(t *testing.T) { - info := NewInfo() + info := version.NewInfo() want := fmt.Sprintf(`: git commit: build tags: @@ -23,7 +24,7 @@ build tags: } func TestInfo_String(t *testing.T) { - info := Info{ + info := version.Info{ Name: "testapp", AppName: "testappd", Version: "1.0.0", @@ -39,12 +40,12 @@ go version go1.14 linux/amd64` } func Test_runVersionCmd(t *testing.T) { - cmd := NewVersionCommand() + cmd := version.NewVersionCommand() _, mockOut := testutil.ApplyMockIO(cmd) cmd.SetArgs([]string{ fmt.Sprintf("--%s=''", cli.OutputFlag), - fmt.Sprintf("--%s=false", flagLong), + "--long=false", }) require.NoError(t, cmd.Execute()) @@ -52,11 +53,10 @@ func Test_runVersionCmd(t *testing.T) { mockOut.Reset() cmd.SetArgs([]string{ - fmt.Sprintf("--%s=json", cli.OutputFlag), - fmt.Sprintf("--%s=true", flagLong), + fmt.Sprintf("--%s=json", cli.OutputFlag), "--long=true", }) - info := NewInfo() + info := version.NewInfo() stringInfo, err := json.Marshal(info) require.NoError(t, err) require.NoError(t, cmd.Execute())