From 457a0a41e9edd005746e3d02e743db7bbf4b8398 Mon Sep 17 00:00:00 2001 From: samricotta Date: Tue, 10 Oct 2023 21:30:30 +0300 Subject: [PATCH 01/10] add extra info --- simapp/simd/cmd/commands.go | 8 ++++++++ version/command.go | 14 ++++++++++++++ version/version.go | 4 ++++ 3 files changed, 26 insertions(+) diff --git a/simapp/simd/cmd/commands.go b/simapp/simd/cmd/commands.go index abfa351961da..04211c58c1a9 100644 --- a/simapp/simd/cmd/commands.go +++ b/simapp/simd/cmd/commands.go @@ -1,6 +1,7 @@ package cmd import ( + "context" "errors" "io" "os" @@ -26,6 +27,7 @@ import ( servertypes "github.com/cosmos/cosmos-sdk/server/types" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types/module" + "github.com/cosmos/cosmos-sdk/version" authcmd "github.com/cosmos/cosmos-sdk/x/auth/client/cli" banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" "github.com/cosmos/cosmos-sdk/x/crisis" @@ -38,10 +40,16 @@ func initRootCmd( interfaceRegistry codectypes.InterfaceRegistry, appCodec codec.Codec, basicManager module.BasicManager, + extraInfo version.ExtraInfo, ) { cfg := sdk.GetConfig() cfg.Seal() + cmdContext := context.WithValue(rootCmd.Context(), "extraInfo", extraInfo) + rootCmd.SetContext(cmdContext) + + rootCmd.AddCommand(version.NewVersionCommand()) + rootCmd.AddCommand( genutilcli.InitCmd(basicManager), NewTestnetCmd(basicManager, banktypes.GenesisBalancesIterator{}), diff --git a/version/command.go b/version/command.go index 451173400c22..dd72621b377b 100644 --- a/version/command.go +++ b/version/command.go @@ -28,6 +28,9 @@ func NewVersionCommand() *cobra.Command { return nil } + extraInfo := extraInfoFromContext(cmd) + verInfo.ExtraInfo = &extraInfo + var ( bz []byte err error @@ -56,3 +59,14 @@ func NewVersionCommand() *cobra.Command { return cmd } + +func extraInfoFromContext(cmd *cobra.Command) ExtraInfo { + ctx := cmd.Context() + if ctx != nil { + extraInfo, ok := ctx.Value("extraInfo").(ExtraInfo) + if ok { + return extraInfo + } + } + return nil +} diff --git a/version/version.go b/version/version.go index 502185ad9a0c..701192c93bbc 100644 --- a/version/version.go +++ b/version/version.go @@ -55,6 +55,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 +68,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 { From 19423abae8a86c2ed62fded35c09edd1bda69e7c Mon Sep 17 00:00:00 2001 From: samricotta Date: Wed, 11 Oct 2023 13:41:23 +0300 Subject: [PATCH 02/10] updates for test and root2 fix --- CHANGELOG.md | 1 + simapp/simd/cmd/root_v2.go | 2 +- version/version_test.go | 13 ++++++++++--- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 77b0401c5261..00cd64cd36bc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -58,6 +58,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 +* [#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 ### Bug Fixes diff --git a/simapp/simd/cmd/root_v2.go b/simapp/simd/cmd/root_v2.go index 170aa94b0fba..f26449880ce6 100644 --- a/simapp/simd/cmd/root_v2.go +++ b/simapp/simd/cmd/root_v2.go @@ -100,7 +100,7 @@ func NewRootCmd() *cobra.Command { }, } - initRootCmd(rootCmd, clientCtx.TxConfig, clientCtx.InterfaceRegistry, clientCtx.Codec, moduleBasicManager) + initRootCmd(rootCmd, clientCtx.TxConfig, clientCtx.InterfaceRegistry, clientCtx.Codec, moduleBasicManager, nil) if err := autoCliOpts.EnhanceRootCommand(rootCmd); err != nil { panic(err) diff --git a/version/version_test.go b/version/version_test.go index caf0a5fb6b60..7ded29618353 100644 --- a/version/version_test.go +++ b/version/version_test.go @@ -158,7 +158,14 @@ func Test_runVersionCmd(t *testing.T) { info := version.NewInfo() stringInfo, err := json.Marshal(info) - require.NoError(t, err) - require.NoError(t, cmd.Execute()) - assert.Equal(t, string(stringInfo)+"\n", mockOut.String()) + + extraInfo := &version.ExtraInfo{"key1": "value1"} + + for key, value := range *extraInfo { + require.NoError(t, err) + require.NoError(t, cmd.Execute()) + assert.Equal(t, key, "key1") + assert.Equal(t, value, "value1") + assert.Equal(t, string(stringInfo)+"\n", mockOut.String()) + } } From 89b34763d657ec80bc1d1c7408bc501c36cfa168 Mon Sep 17 00:00:00 2001 From: samricotta Date: Wed, 11 Oct 2023 13:56:49 +0300 Subject: [PATCH 03/10] Update commands.go --- simapp/simd/cmd/commands.go | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/simapp/simd/cmd/commands.go b/simapp/simd/cmd/commands.go index 04211c58c1a9..df8d6bc26030 100644 --- a/simapp/simd/cmd/commands.go +++ b/simapp/simd/cmd/commands.go @@ -45,10 +45,14 @@ func initRootCmd( cfg := sdk.GetConfig() cfg.Seal() - cmdContext := context.WithValue(rootCmd.Context(), "extraInfo", extraInfo) - rootCmd.SetContext(cmdContext) + type contextKey struct { + key string + } - rootCmd.AddCommand(version.NewVersionCommand()) + var extraInfoKey = &contextKey{"extraInfo"} + + cmdContext := context.WithValue(rootCmd.Context(), extraInfoKey, extraInfo) + rootCmd.SetContext(cmdContext) rootCmd.AddCommand( genutilcli.InitCmd(basicManager), From 3f95b998893c566fa204799d17024f364a678e6d Mon Sep 17 00:00:00 2001 From: samricotta Date: Thu, 12 Oct 2023 10:55:33 +0300 Subject: [PATCH 04/10] review updates --- simapp/simd/cmd/commands.go | 4 +--- version/command.go | 2 +- version/version.go | 3 +++ version/version_test.go | 22 +++++++++++++++------- 4 files changed, 20 insertions(+), 11 deletions(-) diff --git a/simapp/simd/cmd/commands.go b/simapp/simd/cmd/commands.go index df8d6bc26030..ca4d58241fe6 100644 --- a/simapp/simd/cmd/commands.go +++ b/simapp/simd/cmd/commands.go @@ -49,9 +49,7 @@ func initRootCmd( key string } - var extraInfoKey = &contextKey{"extraInfo"} - - cmdContext := context.WithValue(rootCmd.Context(), extraInfoKey, extraInfo) + cmdContext := context.WithValue(rootCmd.Context(), version.ContextKey{}, extraInfo) rootCmd.SetContext(cmdContext) rootCmd.AddCommand( diff --git a/version/command.go b/version/command.go index dd72621b377b..689fcd4a0da4 100644 --- a/version/command.go +++ b/version/command.go @@ -63,7 +63,7 @@ func NewVersionCommand() *cobra.Command { func extraInfoFromContext(cmd *cobra.Command) ExtraInfo { ctx := cmd.Context() if ctx != nil { - extraInfo, ok := ctx.Value("extraInfo").(ExtraInfo) + extraInfo, ok := ctx.Value(ContextKey{}).(ExtraInfo) if ok { return extraInfo } diff --git a/version/version.go b/version/version.go index 701192c93bbc..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 = "" diff --git a/version/version_test.go b/version/version_test.go index 7ded29618353..3650ceb59fda 100644 --- a/version/version_test.go +++ b/version/version_test.go @@ -1,6 +1,7 @@ package version_test import ( + context "context" "encoding/json" "fmt" "runtime" @@ -160,12 +161,19 @@ func Test_runVersionCmd(t *testing.T) { stringInfo, err := json.Marshal(info) extraInfo := &version.ExtraInfo{"key1": "value1"} + ctx := context.WithValue(context.Background(), version.ContextKey{}, extraInfo) - for key, value := range *extraInfo { - require.NoError(t, err) - require.NoError(t, cmd.Execute()) - assert.Equal(t, key, "key1") - assert.Equal(t, value, "value1") - assert.Equal(t, string(stringInfo)+"\n", mockOut.String()) - } + require.NoError(t, err) + require.NoError(t, cmd.Execute()) + + extraInfoFromContext := ctx.Value(version.ContextKey{}) + assert.NotNil(t, extraInfoFromContext) + + castedExtraInfo, ok := extraInfoFromContext.(*version.ExtraInfo) + assert.True(t, ok) + + key1Value := (*castedExtraInfo)["key1"] + assert.Equal(t, "value1", key1Value) + + assert.Equal(t, string(stringInfo)+"\n", mockOut.String()) } From c1227166373848cbdbad9984a3a4751cee395510 Mon Sep 17 00:00:00 2001 From: samricotta Date: Thu, 12 Oct 2023 11:34:57 +0300 Subject: [PATCH 05/10] Update commands.go --- simapp/simd/cmd/commands.go | 4 ---- 1 file changed, 4 deletions(-) diff --git a/simapp/simd/cmd/commands.go b/simapp/simd/cmd/commands.go index ca4d58241fe6..f1718cf343a7 100644 --- a/simapp/simd/cmd/commands.go +++ b/simapp/simd/cmd/commands.go @@ -45,10 +45,6 @@ func initRootCmd( cfg := sdk.GetConfig() cfg.Seal() - type contextKey struct { - key string - } - cmdContext := context.WithValue(rootCmd.Context(), version.ContextKey{}, extraInfo) rootCmd.SetContext(cmdContext) From db4663adb54a28063701af74fae30ff3789faa0d Mon Sep 17 00:00:00 2001 From: samricotta Date: Thu, 12 Oct 2023 12:57:10 +0300 Subject: [PATCH 06/10] Update root.go --- simapp/simd/cmd/root.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/simapp/simd/cmd/root.go b/simapp/simd/cmd/root.go index 1b8291367900..98f5ba46b496 100644 --- a/simapp/simd/cmd/root.go +++ b/simapp/simd/cmd/root.go @@ -99,7 +99,7 @@ func NewRootCmd() *cobra.Command { }, } - initRootCmd(rootCmd, encodingConfig.TxConfig, encodingConfig.InterfaceRegistry, encodingConfig.Codec, tempApp.BasicModuleManager) + initRootCmd(rootCmd, encodingConfig.TxConfig, encodingConfig.InterfaceRegistry, encodingConfig.Codec, tempApp.BasicModuleManager, nil) // autocli opts customClientTemplate, customClientConfig := initClientConfig() From 4440152a085d179048c25fed1e6933e26f33cd4c Mon Sep 17 00:00:00 2001 From: samricotta Date: Thu, 12 Oct 2023 13:05:57 +0300 Subject: [PATCH 07/10] Update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 432ccbd4b64b..16510c46b93e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -59,7 +59,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 -* [#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 +* (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 ### Bug Fixes From 84b469fa75b67520e6c6d2bf9c5a1fc05b54ceae Mon Sep 17 00:00:00 2001 From: samricotta Date: Thu, 12 Oct 2023 16:58:22 +0300 Subject: [PATCH 08/10] Update commands.go --- simapp/simd/cmd/commands.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/simapp/simd/cmd/commands.go b/simapp/simd/cmd/commands.go index f1718cf343a7..86f91f571d59 100644 --- a/simapp/simd/cmd/commands.go +++ b/simapp/simd/cmd/commands.go @@ -45,7 +45,7 @@ func initRootCmd( cfg := sdk.GetConfig() cfg.Seal() - cmdContext := context.WithValue(rootCmd.Context(), version.ContextKey{}, extraInfo) + cmdContext := context.WithValue(context.Background(), version.ContextKey{}, extraInfo) rootCmd.SetContext(cmdContext) rootCmd.AddCommand( From 0e66412d23d032517816bef3f5524c225015ba2c Mon Sep 17 00:00:00 2001 From: samricotta Date: Mon, 23 Oct 2023 14:29:47 +0300 Subject: [PATCH 09/10] godoc comments --- simapp/simd/cmd/commands.go | 6 ------ simapp/simd/cmd/root.go | 2 +- simapp/simd/cmd/root_v2.go | 2 +- version/command.go | 6 ++++++ 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/simapp/simd/cmd/commands.go b/simapp/simd/cmd/commands.go index 86f91f571d59..abfa351961da 100644 --- a/simapp/simd/cmd/commands.go +++ b/simapp/simd/cmd/commands.go @@ -1,7 +1,6 @@ package cmd import ( - "context" "errors" "io" "os" @@ -27,7 +26,6 @@ import ( servertypes "github.com/cosmos/cosmos-sdk/server/types" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types/module" - "github.com/cosmos/cosmos-sdk/version" authcmd "github.com/cosmos/cosmos-sdk/x/auth/client/cli" banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" "github.com/cosmos/cosmos-sdk/x/crisis" @@ -40,14 +38,10 @@ func initRootCmd( interfaceRegistry codectypes.InterfaceRegistry, appCodec codec.Codec, basicManager module.BasicManager, - extraInfo version.ExtraInfo, ) { cfg := sdk.GetConfig() cfg.Seal() - cmdContext := context.WithValue(context.Background(), version.ContextKey{}, extraInfo) - rootCmd.SetContext(cmdContext) - rootCmd.AddCommand( genutilcli.InitCmd(basicManager), NewTestnetCmd(basicManager, banktypes.GenesisBalancesIterator{}), diff --git a/simapp/simd/cmd/root.go b/simapp/simd/cmd/root.go index 98f5ba46b496..1b8291367900 100644 --- a/simapp/simd/cmd/root.go +++ b/simapp/simd/cmd/root.go @@ -99,7 +99,7 @@ func NewRootCmd() *cobra.Command { }, } - initRootCmd(rootCmd, encodingConfig.TxConfig, encodingConfig.InterfaceRegistry, encodingConfig.Codec, tempApp.BasicModuleManager, nil) + initRootCmd(rootCmd, encodingConfig.TxConfig, encodingConfig.InterfaceRegistry, encodingConfig.Codec, tempApp.BasicModuleManager) // autocli opts customClientTemplate, customClientConfig := initClientConfig() diff --git a/simapp/simd/cmd/root_v2.go b/simapp/simd/cmd/root_v2.go index f26449880ce6..170aa94b0fba 100644 --- a/simapp/simd/cmd/root_v2.go +++ b/simapp/simd/cmd/root_v2.go @@ -100,7 +100,7 @@ func NewRootCmd() *cobra.Command { }, } - initRootCmd(rootCmd, clientCtx.TxConfig, clientCtx.InterfaceRegistry, clientCtx.Codec, moduleBasicManager, nil) + initRootCmd(rootCmd, clientCtx.TxConfig, clientCtx.InterfaceRegistry, clientCtx.Codec, moduleBasicManager) if err := autoCliOpts.EnhanceRootCommand(rootCmd); err != nil { panic(err) diff --git a/version/command.go b/version/command.go index 689fcd4a0da4..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,7 @@ func NewVersionCommand() *cobra.Command { return nil } + // Extract and set extra information from the context extraInfo := extraInfoFromContext(cmd) verInfo.ExtraInfo = &extraInfo From 05efcb3a64cf80d027b10be8d8f75ddb77df6c20 Mon Sep 17 00:00:00 2001 From: marbar3778 Date: Mon, 30 Oct 2023 21:58:44 +0100 Subject: [PATCH 10/10] use require --- version/version_test.go | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/version/version_test.go b/version/version_test.go index 3650ceb59fda..75e52d8076cd 100644 --- a/version/version_test.go +++ b/version/version_test.go @@ -8,7 +8,6 @@ import ( "strings" "testing" - "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "github.com/cosmos/cosmos-sdk/client/flags" @@ -150,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{ @@ -167,13 +166,13 @@ func Test_runVersionCmd(t *testing.T) { require.NoError(t, cmd.Execute()) extraInfoFromContext := ctx.Value(version.ContextKey{}) - assert.NotNil(t, extraInfoFromContext) + require.NotNil(t, extraInfoFromContext) castedExtraInfo, ok := extraInfoFromContext.(*version.ExtraInfo) - assert.True(t, ok) + require.True(t, ok) key1Value := (*castedExtraInfo)["key1"] - assert.Equal(t, "value1", key1Value) + require.Equal(t, "value1", key1Value) - assert.Equal(t, string(stringInfo)+"\n", mockOut.String()) + require.Equal(t, string(stringInfo)+"\n", mockOut.String()) }