Skip to content

Commit

Permalink
feat(version): Add extraInfo to version cmd (#18063)
Browse files Browse the repository at this point in the history
Co-authored-by: Marko <marbar3778@yahoo.com>
  • Loading branch information
samricotta and tac0turtle authored Oct 30, 2023
1 parent 5dae7d8 commit 88b7666
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
* (rpc) [#17470](https://github.com/cosmos/cosmos-sdk/pull/17470) Avoid open 0.0.0.0 to public by default and add `listen-ip-address` argument for `testnet init-files` cmd.
* (types) [#17670](https://github.com/cosmos/cosmos-sdk/pull/17670) Use `ctx.CometInfo` in place of `ctx.VoteInfos`
* [#17733](https://github.com/cosmos/cosmos-sdk/pull/17733) Ensure `buf export` exports all proto dependencies
* (version) [#18063](https://github.com/cosmos/cosmos-sdk/pull/18063) Include additional information in the Info struct. This change enhances the Info struct by adding support for additional information through the ExtraInfo field
* [#18204](https://github.com/cosmos/cosmos-sdk/pull/18204) Use streaming json parser to parse chain-id from genesis file.

### Bug Fixes
Expand Down
20 changes: 20 additions & 0 deletions version/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ const (
)

// NewVersionCommand returns a CLI command to interactively print the application binary version information.
// Note: When seeking to add the extra info to the context
// The below can be added to the initRootCmd to include the extraInfo field
//
// cmdContext := context.WithValue(context.Background(), version.ContextKey{}, extraInfo)
// rootCmd.SetContext(cmdContext)
func NewVersionCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "version",
Expand All @@ -28,6 +33,10 @@ func NewVersionCommand() *cobra.Command {
return nil
}

// Extract and set extra information from the context
extraInfo := extraInfoFromContext(cmd)
verInfo.ExtraInfo = &extraInfo

var (
bz []byte
err error
Expand Down Expand Up @@ -56,3 +65,14 @@ func NewVersionCommand() *cobra.Command {

return cmd
}

func extraInfoFromContext(cmd *cobra.Command) ExtraInfo {
ctx := cmd.Context()
if ctx != nil {
extraInfo, ok := ctx.Value(ContextKey{}).(ExtraInfo)
if ok {
return extraInfo
}
}
return nil
}
7 changes: 7 additions & 0 deletions version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ import (
"runtime/debug"
)

// ContextKey is used to store the ExtraInfo in the context.
type ContextKey struct{}

var (
// application's name
Name = ""
Expand Down Expand Up @@ -55,6 +58,9 @@ func getSDKVersion() string {
return sdkVersion
}

// ExtraInfo contains a set of extra information provided by apps
type ExtraInfo map[string]string

// Info defines the application version information.
type Info struct {
Name string `json:"name" yaml:"name"`
Expand All @@ -65,6 +71,7 @@ type Info struct {
GoVersion string `json:"go" yaml:"go"`
BuildDeps []buildDep `json:"build_deps" yaml:"build_deps"`
CosmosSdkVersion string `json:"cosmos_sdk_version" yaml:"cosmos_sdk_version"`
ExtraInfo *ExtraInfo `json:"extra_info" yaml:"extra_info"`
}

func NewInfo() Info {
Expand Down
20 changes: 17 additions & 3 deletions version/version_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package version_test

import (
context "context"
"encoding/json"
"fmt"
"runtime"
"strings"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/cosmos/cosmos-sdk/client/flags"
Expand Down Expand Up @@ -149,7 +149,7 @@ func Test_runVersionCmd(t *testing.T) {
})

require.NoError(t, cmd.Execute())
assert.Equal(t, "\n", mockOut.String())
require.Equal(t, "\n", mockOut.String())
mockOut.Reset()

cmd.SetArgs([]string{
Expand All @@ -158,7 +158,21 @@ func Test_runVersionCmd(t *testing.T) {

info := version.NewInfo()
stringInfo, err := json.Marshal(info)

extraInfo := &version.ExtraInfo{"key1": "value1"}
ctx := context.WithValue(context.Background(), version.ContextKey{}, extraInfo)

require.NoError(t, err)
require.NoError(t, cmd.Execute())
assert.Equal(t, string(stringInfo)+"\n", mockOut.String())

extraInfoFromContext := ctx.Value(version.ContextKey{})
require.NotNil(t, extraInfoFromContext)

castedExtraInfo, ok := extraInfoFromContext.(*version.ExtraInfo)
require.True(t, ok)

key1Value := (*castedExtraInfo)["key1"]
require.Equal(t, "value1", key1Value)

require.Equal(t, string(stringInfo)+"\n", mockOut.String())
}

0 comments on commit 88b7666

Please sign in to comment.