Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enrich versioning with build deps #7848

Merged
merged 4 commits into from
Nov 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
1 change: 1 addition & 0 deletions version/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
37 changes: 31 additions & 6 deletions version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
package version

import (
"encoding/json"
"fmt"
"runtime"
"runtime/debug"
)

var (
Expand All @@ -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 {
Expand All @@ -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(),
}
}

Expand All @@ -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 }
16 changes: 8 additions & 8 deletions version/version_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package version
package version_test

import (
"encoding/json"
Expand All @@ -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:
Expand All @@ -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",
Expand All @@ -39,24 +40,23 @@ 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())
assert.Equal(t, "\n", mockOut.String())
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())
Expand Down