Skip to content

Commit

Permalink
key as type
Browse files Browse the repository at this point in the history
  • Loading branch information
hieuvubk committed May 22, 2024
1 parent 7c4e602 commit baae5d4
Show file tree
Hide file tree
Showing 11 changed files with 59 additions and 59 deletions.
6 changes: 3 additions & 3 deletions client/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ func SetCmdClientContext(cmd *cobra.Command, clientCtx Context) error {
}

func GetViperFromCmd(cmd *cobra.Command) *viper.Viper {
value := cmd.Context().Value(corectx.ViperContextKey)
value := cmd.Context().Value(corectx.ViperContextKey{})
v, ok := value.(*viper.Viper)
if !ok {
return viper.New()
Expand All @@ -388,7 +388,7 @@ func GetViperFromCmd(cmd *cobra.Command) *viper.Viper {
}

func GetConfigFromCmd(cmd *cobra.Command) *cmtcfg.Config {
v := cmd.Context().Value(corectx.ViperContextKey)
v := cmd.Context().Value(corectx.ViperContextKey{})
viper, ok := v.(*viper.Viper)
if !ok {
return cmtcfg.DefaultConfig()
Expand All @@ -397,7 +397,7 @@ func GetConfigFromCmd(cmd *cobra.Command) *cmtcfg.Config {
}

func GetLoggerFromCmd(cmd *cobra.Command) log.Logger {
v := cmd.Context().Value(corectx.LoggerContextKey)
v := cmd.Context().Value(corectx.LoggerContextKey{})
logger, ok := v.(log.Logger)
if !ok {
return log.NewLogger(cmd.OutOrStdout())
Expand Down
4 changes: 2 additions & 2 deletions core/context/server_context.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package context

var (
type (
LoggerContextKey struct{}
ViperContextKey struct{}
ViperContextKey struct{}
)
4 changes: 2 additions & 2 deletions server/cmd/execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ func Execute(rootCmd *cobra.Command, envPrefix, defaultHome string) error {
func CreateExecuteContext(ctx context.Context) context.Context {
// srvCtx := server.NewDefaultContext()
ctx = context.WithValue(ctx, client.ClientContextKey, &client.Context{})
ctx = context.WithValue(ctx, corectx.LoggerContextKey, log.NewLogger(os.Stdout))
ctx = context.WithValue(ctx, corectx.ViperContextKey, viper.New())
ctx = context.WithValue(ctx, corectx.LoggerContextKey{}, log.NewLogger(os.Stdout))
ctx = context.WithValue(ctx, corectx.ViperContextKey{}, viper.New())

return ctx
}
10 changes: 5 additions & 5 deletions server/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ func CreateSDKLogger(v *viper.Viper, out io.Writer) (log.Logger, error) {
// if it has not been set.
func GetServerContextFromCmd(cmd *cobra.Command) *LegacyContext {
serverCtx := &LegacyContext{}
if v := cmd.Context().Value(corectx.ViperContextKey); v != nil {
if v := cmd.Context().Value(corectx.ViperContextKey{}); v != nil {
viper := v.(*viper.Viper)
serverCtx.Viper = viper
serverCtx.Config = client.GetConfigFromViper(viper)
Expand All @@ -196,13 +196,13 @@ func GetServerContextFromCmd(cmd *cobra.Command) *LegacyContext {
serverCtx.Config = cmtcfg.DefaultConfig()
}

if v := cmd.Context().Value(corectx.LoggerContextKey); v != nil {
if v := cmd.Context().Value(corectx.LoggerContextKey{}); v != nil {
logger := v.(log.Logger)
serverCtx.Logger = logger
} else {
serverCtx.Logger = log.NewLogger(cmd.OutOrStdout())
}

return serverCtx
}

Expand All @@ -217,8 +217,8 @@ func SetCmdServerContext(cmd *cobra.Command, viper *viper.Viper, logger log.Logg
cmdCtx = cmd.Context()
}

cmd.SetContext(context.WithValue(cmdCtx, corectx.LoggerContextKey, logger))
cmd.SetContext(context.WithValue(cmdCtx, corectx.ViperContextKey, viper))
cmd.SetContext(context.WithValue(cmdCtx, corectx.LoggerContextKey{}, logger))
cmd.SetContext(context.WithValue(cmdCtx, corectx.ViperContextKey{}, viper))

return nil
}
Expand Down
46 changes: 23 additions & 23 deletions server/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ func TestInterceptConfigsPreRunHandlerCreatesConfigFilesWhenMissing(t *testing.T

cmd.PreRunE = preRunETestImpl

ctx := context.WithValue(context.Background(), corectx.LoggerContextKey, log.NewLogger(os.Stdout))
ctx = context.WithValue(ctx, corectx.ViperContextKey, viper.New())
ctx := context.WithValue(context.Background(), corectx.LoggerContextKey{}, log.NewLogger(os.Stdout))
ctx = context.WithValue(ctx, corectx.ViperContextKey{}, viper.New())
if err := cmd.ExecuteContext(ctx); !errors.Is(err, errCanceledInPreRun) {
t.Fatalf("function failed with [%T] %v", err, err)
}
Expand Down Expand Up @@ -106,7 +106,7 @@ func TestInterceptConfigsPreRunHandlerCreatesConfigFilesWhenMissing(t *testing.T
}

// Test that the config for use in server/start.go is created
v := ctx.Value(corectx.ViperContextKey)
v := ctx.Value(corectx.ViperContextKey{})
viper, _ := v.(*viper.Viper)

if viper == nil {
Expand Down Expand Up @@ -144,8 +144,8 @@ func TestInterceptConfigsPreRunHandlerReadsConfigToml(t *testing.T) {

cmd.PreRunE = preRunETestImpl

ctx := context.WithValue(context.Background(), corectx.LoggerContextKey, log.NewLogger(os.Stdout))
ctx = context.WithValue(ctx, corectx.ViperContextKey, viper.New())
ctx := context.WithValue(context.Background(), corectx.LoggerContextKey{}, log.NewLogger(os.Stdout))
ctx = context.WithValue(ctx, corectx.ViperContextKey{}, viper.New())

if err := cmd.ExecuteContext(ctx); !errors.Is(err, errCanceledInPreRun) {
t.Fatalf("function failed with [%T] %v", err, err)
Expand Down Expand Up @@ -184,8 +184,8 @@ func TestInterceptConfigsPreRunHandlerReadsAppToml(t *testing.T) {

cmd.PreRunE = preRunETestImpl

ctx := context.WithValue(context.Background(), corectx.LoggerContextKey, log.NewLogger(os.Stdout))
ctx = context.WithValue(ctx, corectx.ViperContextKey, viper.New())
ctx := context.WithValue(context.Background(), corectx.LoggerContextKey{}, log.NewLogger(os.Stdout))
ctx = context.WithValue(ctx, corectx.ViperContextKey{}, viper.New())

if err := cmd.ExecuteContext(ctx); !errors.Is(err, errCanceledInPreRun) {
t.Fatalf("function failed with [%T] %v", err, err)
Expand Down Expand Up @@ -214,8 +214,8 @@ func TestInterceptConfigsPreRunHandlerReadsFlags(t *testing.T) {

cmd.PreRunE = preRunETestImpl

ctx := context.WithValue(context.Background(), corectx.LoggerContextKey, log.NewLogger(os.Stdout))
ctx = context.WithValue(ctx, corectx.ViperContextKey, viper.New())
ctx := context.WithValue(context.Background(), corectx.LoggerContextKey{}, log.NewLogger(os.Stdout))
ctx = context.WithValue(ctx, corectx.ViperContextKey{}, viper.New())

if err := cmd.ExecuteContext(ctx); !errors.Is(err, errCanceledInPreRun) {
t.Fatalf("function failed with [%T] %v", err, err)
Expand Down Expand Up @@ -252,8 +252,8 @@ func TestInterceptConfigsPreRunHandlerReadsEnvVars(t *testing.T) {

cmd.PreRunE = preRunETestImpl

ctx := context.WithValue(context.Background(), corectx.LoggerContextKey, log.NewLogger(os.Stdout))
ctx = context.WithValue(ctx, corectx.ViperContextKey, viper.New())
ctx := context.WithValue(context.Background(), corectx.LoggerContextKey{}, log.NewLogger(os.Stdout))
ctx = context.WithValue(ctx, corectx.ViperContextKey{}, viper.New())

if err := cmd.ExecuteContext(ctx); !errors.Is(err, errCanceledInPreRun) {
t.Fatalf("function failed with [%T] %v", err, err)
Expand Down Expand Up @@ -361,8 +361,8 @@ func TestInterceptConfigsPreRunHandlerPrecedenceFlag(t *testing.T) {
testCommon := newPrecedenceCommon(t)
testCommon.setAll(t, &TestAddrExpected, &TestAddrNotExpected, &TestAddrNotExpected)

ctx := context.WithValue(context.Background(), corectx.LoggerContextKey, log.NewLogger(os.Stdout))
ctx = context.WithValue(ctx, corectx.ViperContextKey, viper.New())
ctx := context.WithValue(context.Background(), corectx.LoggerContextKey{}, log.NewLogger(os.Stdout))
ctx = context.WithValue(ctx, corectx.ViperContextKey{}, viper.New())

if err := testCommon.cmd.ExecuteContext(ctx); !errors.Is(err, errCanceledInPreRun) {
t.Fatalf("function failed with [%T] %v", err, err)
Expand All @@ -379,8 +379,8 @@ func TestInterceptConfigsPreRunHandlerPrecedenceEnvVar(t *testing.T) {
testCommon := newPrecedenceCommon(t)
testCommon.setAll(t, nil, &TestAddrExpected, &TestAddrNotExpected)

ctx := context.WithValue(context.Background(), corectx.LoggerContextKey, log.NewLogger(os.Stdout))
ctx = context.WithValue(ctx, corectx.ViperContextKey, viper.New())
ctx := context.WithValue(context.Background(), corectx.LoggerContextKey{}, log.NewLogger(os.Stdout))
ctx = context.WithValue(ctx, corectx.ViperContextKey{}, viper.New())

if err := testCommon.cmd.ExecuteContext(ctx); !errors.Is(err, errCanceledInPreRun) {
t.Fatalf("function failed with [%T] %v", err, err)
Expand All @@ -397,8 +397,8 @@ func TestInterceptConfigsPreRunHandlerPrecedenceConfigFile(t *testing.T) {
testCommon := newPrecedenceCommon(t)
testCommon.setAll(t, nil, nil, &TestAddrExpected)

ctx := context.WithValue(context.Background(), corectx.LoggerContextKey, log.NewLogger(os.Stdout))
ctx = context.WithValue(ctx, corectx.ViperContextKey, viper.New())
ctx := context.WithValue(context.Background(), corectx.LoggerContextKey{}, log.NewLogger(os.Stdout))
ctx = context.WithValue(ctx, corectx.ViperContextKey{}, viper.New())

if err := testCommon.cmd.ExecuteContext(ctx); !errors.Is(err, errCanceledInPreRun) {
t.Fatalf("function failed with [%T] %v", err, err)
Expand All @@ -415,8 +415,8 @@ func TestInterceptConfigsPreRunHandlerPrecedenceConfigDefault(t *testing.T) {
testCommon := newPrecedenceCommon(t)
// Do not set anything

ctx := context.WithValue(context.Background(), corectx.LoggerContextKey, log.NewLogger(os.Stdout))
ctx = context.WithValue(ctx, corectx.ViperContextKey, viper.New())
ctx := context.WithValue(context.Background(), corectx.LoggerContextKey{}, log.NewLogger(os.Stdout))
ctx = context.WithValue(ctx, corectx.ViperContextKey{}, viper.New())

if err := testCommon.cmd.ExecuteContext(ctx); !errors.Is(err, errCanceledInPreRun) {
t.Fatalf("function failed with [%T] %v", err, err)
Expand Down Expand Up @@ -446,8 +446,8 @@ func TestInterceptConfigsWithBadPermissions(t *testing.T) {

cmd.PreRunE = preRunETestImpl

ctx := context.WithValue(context.Background(), corectx.LoggerContextKey, log.NewNopLogger())
ctx = context.WithValue(ctx, corectx.ViperContextKey, viper.New())
ctx := context.WithValue(context.Background(), corectx.LoggerContextKey{}, log.NewNopLogger())
ctx = context.WithValue(ctx, corectx.ViperContextKey{}, viper.New())

if err := cmd.ExecuteContext(ctx); !os.IsPermission(err) {
t.Fatalf("Failed to catch permissions error, got: [%T] %v", err, err)
Expand All @@ -464,8 +464,8 @@ func TestEmptyMinGasPrices(t *testing.T) {
clientCtx := client.Context{}.WithHomeDir(tempDir).WithCodec(encCfg.Codec)
viper := viper.New()
viper.Set(flags.FlagHome, tempDir)
ctx := context.WithValue(context.Background(), corectx.ViperContextKey, viper)
ctx = context.WithValue(ctx, corectx.LoggerContextKey, log.NewLogger(os.Stdout))
ctx := context.WithValue(context.Background(), corectx.ViperContextKey{}, viper)
ctx = context.WithValue(ctx, corectx.LoggerContextKey{}, log.NewLogger(os.Stdout))
ctx = context.WithValue(ctx, client.ClientContextKey, &clientCtx)
cmd := genutilcli.InitCmd(module.NewManager())
cmd.SetArgs([]string{"appnode-test"})
Expand Down
4 changes: 2 additions & 2 deletions simapp/simd/cmd/testnet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ func Test_TestnetCmd(t *testing.T) {
WithValidatorAddressCodec(cdcOpts.GetValidatorCodec())

ctx := context.Background()
ctx = context.WithValue(ctx, corectx.ViperContextKey, viper)
ctx = context.WithValue(ctx, corectx.LoggerContextKey, logger)
ctx = context.WithValue(ctx, corectx.ViperContextKey{}, viper)
ctx = context.WithValue(ctx, corectx.LoggerContextKey{}, logger)
ctx = context.WithValue(ctx, client.ClientContextKey, &clientCtx)
cmd := testnetInitFilesCmd(moduleManager, banktypes.GenesisBalancesIterator{})
cmd.SetArgs([]string{fmt.Sprintf("--%s=test", flags.FlagKeyringBackend), fmt.Sprintf("--output-dir=%s", home)})
Expand Down
4 changes: 2 additions & 2 deletions tests/e2e/genutil/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func TestExportCmd_ConsensusParams(t *testing.T) {
func TestExportCmd_HomeDir(t *testing.T) {
_, ctx, _, cmd := setupApp(t, t.TempDir())

v := ctx.Value(corectx.ViperContextKey)
v := ctx.Value(corectx.ViperContextKey{})
viper, ok := v.(*viper.Viper)
require.True(t, ok)
viper.Set(flags.FlagHome, "foobar")
Expand Down Expand Up @@ -220,7 +220,7 @@ func setupApp(t *testing.T, tempDir string) (*simapp.SimApp, context.Context, ge

ctx := context.Background()
ctx = context.WithValue(ctx, client.ClientContextKey, &clientCtx)
ctx = context.WithValue(ctx, corectx.ViperContextKey, viper)
ctx = context.WithValue(ctx, corectx.ViperContextKey{}, viper)

return app, ctx, appGenesis, cmd
}
Expand Down
4 changes: 2 additions & 2 deletions x/genutil/client/cli/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ func NewExportSystem(t *testing.T, exporter types.AppExporter) *ExportSystem {

cCtx := (client.Context{}).WithHomeDir(homeDir)

ctx := context.WithValue(context.Background(), corectx.ViperContextKey, viper)
ctx = context.WithValue(ctx, corectx.LoggerContextKey, logger)
ctx := context.WithValue(context.Background(), corectx.ViperContextKey{}, viper)
ctx = context.WithValue(ctx, corectx.LoggerContextKey{}, logger)

ctx = context.WithValue(ctx, client.ClientContextKey, &cCtx)

Expand Down
4 changes: 2 additions & 2 deletions x/genutil/client/cli/genaccount_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ func TestAddGenesisAccountCmd(t *testing.T) {

ctx := context.Background()
ctx = context.WithValue(ctx, client.ClientContextKey, &clientCtx)
ctx = context.WithValue(ctx, corectx.ViperContextKey, viper)
ctx = context.WithValue(ctx, corectx.LoggerContextKey, logger)
ctx = context.WithValue(ctx, corectx.ViperContextKey{}, viper)
ctx = context.WithValue(ctx, corectx.LoggerContextKey{}, logger)

cmd := genutilcli.AddGenesisAccountCmd(addresscodec.NewBech32Codec("cosmos"))
cmd.SetArgs([]string{
Expand Down
28 changes: 14 additions & 14 deletions x/genutil/client/cli/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ func TestInitCmd(t *testing.T) {

ctx := context.Background()
ctx = context.WithValue(ctx, client.ClientContextKey, &clientCtx)
ctx = context.WithValue(ctx, corectx.ViperContextKey, viper)
ctx = context.WithValue(ctx, corectx.LoggerContextKey, logger)
ctx = context.WithValue(ctx, corectx.ViperContextKey{}, viper)
ctx = context.WithValue(ctx, corectx.LoggerContextKey{}, logger)

cmd := genutilcli.InitCmd(testMbm)
cmd.SetArgs(
Expand Down Expand Up @@ -111,8 +111,8 @@ func TestInitRecover(t *testing.T) {

ctx := context.Background()
ctx = context.WithValue(ctx, client.ClientContextKey, &clientCtx)
ctx = context.WithValue(ctx, corectx.ViperContextKey, viper)
ctx = context.WithValue(ctx, corectx.LoggerContextKey, logger)
ctx = context.WithValue(ctx, corectx.ViperContextKey{}, viper)
ctx = context.WithValue(ctx, corectx.LoggerContextKey{}, logger)

cmd := genutilcli.InitCmd(testMbm)
cmd.SetContext(ctx)
Expand Down Expand Up @@ -144,8 +144,8 @@ func TestInitDefaultBondDenom(t *testing.T) {

ctx := context.Background()
ctx = context.WithValue(ctx, client.ClientContextKey, &clientCtx)
ctx = context.WithValue(ctx, corectx.ViperContextKey, viper)
ctx = context.WithValue(ctx, corectx.LoggerContextKey, logger)
ctx = context.WithValue(ctx, corectx.ViperContextKey{}, viper)
ctx = context.WithValue(ctx, corectx.LoggerContextKey{}, logger)

cmd := genutilcli.InitCmd(testMbm)

Expand All @@ -172,8 +172,8 @@ func TestEmptyState(t *testing.T) {

ctx := context.Background()
ctx = context.WithValue(ctx, client.ClientContextKey, &clientCtx)
ctx = context.WithValue(ctx, corectx.ViperContextKey, viper)
ctx = context.WithValue(ctx, corectx.LoggerContextKey, logger)
ctx = context.WithValue(ctx, corectx.ViperContextKey{}, viper)
ctx = context.WithValue(ctx, corectx.LoggerContextKey{}, logger)

cmd := genutilcli.InitCmd(testMbm)
cmd.SetArgs([]string{"appnode-test"})
Expand Down Expand Up @@ -267,8 +267,8 @@ func TestInitConfig(t *testing.T) {

ctx := context.Background()
ctx = context.WithValue(ctx, client.ClientContextKey, &clientCtx)
ctx = context.WithValue(ctx, corectx.ViperContextKey, viper)
ctx = context.WithValue(ctx, corectx.LoggerContextKey, logger)
ctx = context.WithValue(ctx, corectx.ViperContextKey{}, viper)
ctx = context.WithValue(ctx, corectx.LoggerContextKey{}, logger)

cmd := genutilcli.InitCmd(testMbm)
cmd.SetArgs([]string{"testnode"})
Expand Down Expand Up @@ -317,8 +317,8 @@ func TestInitWithHeight(t *testing.T) {

ctx := context.Background()
ctx = context.WithValue(ctx, client.ClientContextKey, &clientCtx)
ctx = context.WithValue(ctx, corectx.ViperContextKey, viper)
ctx = context.WithValue(ctx, corectx.LoggerContextKey, logger)
ctx = context.WithValue(ctx, corectx.ViperContextKey{}, viper)
ctx = context.WithValue(ctx, corectx.LoggerContextKey{}, logger)

testInitialHeight := int64(333)

Expand Down Expand Up @@ -354,8 +354,8 @@ func TestInitWithNegativeHeight(t *testing.T) {

ctx := context.Background()
ctx = context.WithValue(ctx, client.ClientContextKey, &clientCtx)
ctx = context.WithValue(ctx, corectx.ViperContextKey, viper)
ctx = context.WithValue(ctx, corectx.LoggerContextKey, logger)
ctx = context.WithValue(ctx, corectx.ViperContextKey{}, viper)
ctx = context.WithValue(ctx, corectx.LoggerContextKey{}, logger)

testInitialHeight := int64(-333)

Expand Down
4 changes: 2 additions & 2 deletions x/genutil/client/testutil/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ func ExecInitCmd(mm *module.Manager, home string, cdc codec.Codec) error {

ctx := context.Background()
ctx = context.WithValue(ctx, client.ClientContextKey, &clientCtx)
ctx = context.WithValue(ctx, corectx.ViperContextKey, viper)
ctx = context.WithValue(ctx, corectx.LoggerContextKey, logger)
ctx = context.WithValue(ctx, corectx.ViperContextKey{}, viper)
ctx = context.WithValue(ctx, corectx.LoggerContextKey{}, logger)

cmd.SetArgs([]string{"appnode-test"})

Expand Down

0 comments on commit baae5d4

Please sign in to comment.