feat(version): Add extraInfo to version cmd (backport #18063) (#18437)

Co-authored-by: samricotta <37125168+samricotta@users.noreply.github.com>
Co-authored-by: Julien Robert <julien@rbrt.fr>
This commit is contained in:
mergify[bot]
2023-11-10 12:11:46 +00:00
committed by GitHub
co-authored by samricotta Julien Robert
parent b93bdea1dc
commit dc30a6a642
4 changed files with 44 additions and 3 deletions
+19
View File
@@ -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,9 @@ func NewVersionCommand() *cobra.Command {
return nil
}
// Extract and set extra information from the context
verInfo.ExtraInfo = extraInfoFromContext(cmd)
var (
bz []byte
err error
@@ -56,3 +64,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
View File
@@ -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,omitempty" yaml:"extra_info,omitempty"`
}
func NewInfo() Info {
+17 -3
View File
@@ -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())
}