From 107b003715a9bbee73c149043168b6aace06a9f3 Mon Sep 17 00:00:00 2001 From: samricotta <37125168+samricotta@users.noreply.github.com> Date: Mon, 30 Oct 2023 23:34:20 +0200 Subject: [PATCH] feat(version): Add extraInfo to version cmd (#18063) Co-authored-by: Marko (cherry picked from commit 88b76664cd16b10b752dbac8df4356eb95997f47) # Conflicts: # CHANGELOG.md --- CHANGELOG.md | 17 +++++++++++++++++ version/command.go | 20 ++++++++++++++++++++ version/version.go | 7 +++++++ version/version_test.go | 20 +++++++++++++++++--- 4 files changed, 61 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 314ec43314dc..59d9996faf11 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -46,6 +46,23 @@ Ref: https://keepachangelog.com/en/1.0.0/ ### Improvements * (x/gov) [#18025](https://github.com/cosmos/cosmos-sdk/pull/18025) Improve ` q gov proposer` by querying directly a proposal instead of tx events. It is an alias of `q gov proposal` as the proposer is a field of the proposal. +<<<<<<< HEAD +======= +* (x/staking/keeper) [#18049](https://github.com/cosmos/cosmos-sdk/pull/18049) return early if Slash encounters zero tokens to burn. +* (x/staking/keeper) [#18035](https://github.com/cosmos/cosmos-sdk/pull/18035) Hoisted out of the redelegation loop, the non-changing validator and delegator addresses parsing. +* (keyring) [#17913](https://github.com/cosmos/cosmos-sdk/pull/17913) Add `NewAutoCLIKeyring` for creating an AutoCLI keyring from a SDK keyring. +* (codec) [#17913](https://github.com/cosmos/cosmos-sdk/pull/17913) `codectypes.NewAnyWithValue` supports proto v2 messages. +* (client) [#17503](https://github.com/cosmos/cosmos-sdk/pull/17503) Add `client.Context{}.WithAddressCodec`, `WithValidatorAddressCodec`, `WithConsensusAddressCodec` to provide address codecs to the client context. See the [UPGRADING.md](./UPGRADING.md) for more details. +* (crypto/keyring) [#17503](https://github.com/cosmos/cosmos-sdk/pull/17503) Simplify keyring interfaces to use `[]byte` instead of `sdk.Address` for addresses. +* (all) [#16537](https://github.com/cosmos/cosmos-sdk/pull/16537) Properly propagated `fmt.Errorf` errors and using `errors.New` where appropriate. +* (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 +>>>>>>> 88b76664c (feat(version): Add extraInfo to version cmd (#18063)) ## [v0.50.1](https://github.com/cosmos/cosmos-sdk/releases/tag/v0.50.1) - 2023-11-07 diff --git a/version/command.go b/version/command.go index 451173400c22..163e6e5c7d25 100644 --- a/version/command.go +++ b/version/command.go @@ -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", @@ -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 @@ -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 +} diff --git a/version/version.go b/version/version.go index 502185ad9a0c..b407f2b36e56 100644 --- a/version/version.go +++ b/version/version.go @@ -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 = "" @@ -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"` @@ -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 { diff --git a/version/version_test.go b/version/version_test.go index caf0a5fb6b60..75e52d8076cd 100644 --- a/version/version_test.go +++ b/version/version_test.go @@ -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" @@ -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{ @@ -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()) }