-
Notifications
You must be signed in to change notification settings - Fork 3.7k
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
feat(version): Add extraInfo to version cmd #18063
Changes from all commits
457a0a4
19423ab
89b3476
2a81599
e597e0f
3f95b99
03d6f44
c122716
37b18dd
db4663a
4440152
84b469f
0e66412
20bc6f8
05efcb3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
Comment on lines
+36
to
+38
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
|
||
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 | ||
} | ||
samricotta marked this conversation as resolved.
Show resolved
Hide resolved
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
||
Comment on lines
+61
to
+63
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
// 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 { | ||
|
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
@@ -1,13 +1,13 @@ | ||||||||||
package version_test | ||||||||||
|
||||||||||
import ( | ||||||||||
context "context" | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The import of the - context "context" Commitable suggestion (Beta)
Suggested change
|
||||||||||
"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) | ||||||||||
tac0turtle marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||
|
||||||||||
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()) | ||||||||||
} | ||||||||||
julienrbrt marked this conversation as resolved.
Show resolved
Hide resolved
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comment provides instructions on how to add extra info to the context. However, it would be more maintainable and less error-prone if this was handled by the function itself, rather than requiring the caller to do it. Consider refactoring
NewVersionCommand
to accept anExtraInfo
parameter, which it can then add to the context.