From ef2fbbac2693da07d6d20031d5556a246a1da7b0 Mon Sep 17 00:00:00 2001 From: Connor Davis <17688291+PoisonPhang@users.noreply.github.com> Date: Tue, 30 Jan 2024 15:38:22 -0600 Subject: [PATCH] fix(simapp): home flag is not respected (#18994) Co-authored-by: Julien Robert --- client/cmd.go | 14 ++++++++------ scripts/init-simapp.sh | 4 ++-- server/util.go | 12 +++++++----- server/util_test.go | 18 ++++++++++++++++++ simapp/simd/cmd/root_v2.go | 2 +- 5 files changed, 36 insertions(+), 14 deletions(-) diff --git a/client/cmd.go b/client/cmd.go index b98b5d6e895..0b261c00690 100644 --- a/client/cmd.go +++ b/client/cmd.go @@ -1,6 +1,7 @@ package client import ( + "context" "crypto/tls" "fmt" "strings" @@ -358,13 +359,14 @@ func GetClientContextFromCmd(cmd *cobra.Command) Context { // SetCmdClientContext sets a command's Context value to the provided argument. // If the context has not been set, set the given context as the default. func SetCmdClientContext(cmd *cobra.Command, clientCtx Context) error { - v := cmd.Context().Value(ClientContextKey) - if v == nil { - v = &clientCtx - } + var cmdCtx context.Context - clientCtxPtr := v.(*Context) - *clientCtxPtr = clientCtx + if cmd.Context() == nil { + cmdCtx = context.Background() + } else { + cmdCtx = cmd.Context() + } + cmd.SetContext(context.WithValue(cmdCtx, ClientContextKey, &clientCtx)) return nil } diff --git a/scripts/init-simapp.sh b/scripts/init-simapp.sh index 9225e9b1245..982208da016 100755 --- a/scripts/init-simapp.sh +++ b/scripts/init-simapp.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/usr/bin/env bash SIMD_BIN=${SIMD_BIN:=$(which simd 2>/dev/null)} @@ -14,4 +14,4 @@ $SIMD_BIN init test --chain-id demo $SIMD_BIN genesis add-genesis-account alice 5000000000stake --keyring-backend test $SIMD_BIN genesis add-genesis-account bob 5000000000stake --keyring-backend test $SIMD_BIN genesis gentx alice 1000000stake --chain-id demo -$SIMD_BIN genesis collect-gentxs \ No newline at end of file +$SIMD_BIN genesis collect-gentxs diff --git a/server/util.go b/server/util.go index 1b772313c54..4174b6ee4b2 100644 --- a/server/util.go +++ b/server/util.go @@ -215,13 +215,15 @@ func GetServerContextFromCmd(cmd *cobra.Command) *Context { // SetCmdServerContext sets a command's Context value to the provided argument. // If the context has not been set, set the given context as the default. func SetCmdServerContext(cmd *cobra.Command, serverCtx *Context) error { - v := cmd.Context().Value(ServerContextKey) - if v == nil { - v = serverCtx + var cmdCtx context.Context + + if cmd.Context() == nil { + cmdCtx = context.Background() + } else { + cmdCtx = cmd.Context() } - serverCtxPtr := v.(*Context) - *serverCtxPtr = *serverCtx + cmd.SetContext(context.WithValue(cmdCtx, ServerContextKey, serverCtx)) return nil } diff --git a/server/util_test.go b/server/util_test.go index a9d17f0e050..915ecff9c6d 100644 --- a/server/util_test.go +++ b/server/util_test.go @@ -63,6 +63,8 @@ func TestInterceptConfigsPreRunHandlerCreatesConfigFilesWhenMissing(t *testing.T t.Fatalf("function failed with [%T] %v", err, err) } + serverCtx = server.GetServerContextFromCmd(cmd) + // Test that config.toml is created configTomlPath := path.Join(tempDir, "config", "config.toml") s, err := os.Stat(configTomlPath) @@ -140,6 +142,8 @@ func TestInterceptConfigsPreRunHandlerReadsConfigToml(t *testing.T) { t.Fatalf("function failed with [%T] %v", err, err) } + serverCtx = server.GetServerContextFromCmd(cmd) + if testDbBackend != serverCtx.Config.DBBackend { t.Error("backend was not set from config.toml") } @@ -177,6 +181,8 @@ func TestInterceptConfigsPreRunHandlerReadsAppToml(t *testing.T) { t.Fatalf("function failed with [%T] %v", err, err) } + serverCtx = server.GetServerContextFromCmd(cmd) + if testHaltTime != serverCtx.Viper.GetInt("halt-time") { t.Error("Halt time was not set from app.toml") } @@ -205,6 +211,8 @@ func TestInterceptConfigsPreRunHandlerReadsFlags(t *testing.T) { t.Fatalf("function failed with [%T] %v", err, err) } + serverCtx = server.GetServerContextFromCmd(cmd) + if testAddr != serverCtx.Config.RPC.ListenAddress { t.Error("RPCListenAddress was not set from command flags") } @@ -240,6 +248,8 @@ func TestInterceptConfigsPreRunHandlerReadsEnvVars(t *testing.T) { t.Fatalf("function failed with [%T] %v", err, err) } + serverCtx = server.GetServerContextFromCmd(cmd) + if testAddr != serverCtx.Config.RPC.ListenAddress { t.Errorf("RPCListenAddress was not set from env. var. %q", envVarName) } @@ -344,6 +354,8 @@ func TestInterceptConfigsPreRunHandlerPrecedenceFlag(t *testing.T) { t.Fatalf("function failed with [%T] %v", err, err) } + serverCtx = server.GetServerContextFromCmd(testCommon.cmd) + if TestAddrExpected != serverCtx.Config.RPC.ListenAddress { t.Fatalf("RPCListenAddress was not set from flag %q", testCommon.flagName) } @@ -360,6 +372,8 @@ func TestInterceptConfigsPreRunHandlerPrecedenceEnvVar(t *testing.T) { t.Fatalf("function failed with [%T] %v", err, err) } + serverCtx = server.GetServerContextFromCmd(testCommon.cmd) + if TestAddrExpected != serverCtx.Config.RPC.ListenAddress { t.Errorf("RPCListenAddress was not set from env. var. %q", testCommon.envVarName) } @@ -376,6 +390,8 @@ func TestInterceptConfigsPreRunHandlerPrecedenceConfigFile(t *testing.T) { t.Fatalf("function failed with [%T] %v", err, err) } + serverCtx = server.GetServerContextFromCmd(testCommon.cmd) + if TestAddrExpected != serverCtx.Config.RPC.ListenAddress { t.Errorf("RPCListenAddress was not read from file %q", testCommon.configTomlPath) } @@ -392,6 +408,8 @@ func TestInterceptConfigsPreRunHandlerPrecedenceConfigDefault(t *testing.T) { t.Fatalf("function failed with [%T] %v", err, err) } + serverCtx = server.GetServerContextFromCmd(testCommon.cmd) + if serverCtx.Config.RPC.ListenAddress != "tcp://127.0.0.1:26657" { t.Error("RPCListenAddress is not using default") } diff --git a/simapp/simd/cmd/root_v2.go b/simapp/simd/cmd/root_v2.go index d951fdf18c9..e6512b45235 100644 --- a/simapp/simd/cmd/root_v2.go +++ b/simapp/simd/cmd/root_v2.go @@ -62,7 +62,7 @@ func NewRootCmd() *cobra.Command { cmd.SetOut(cmd.OutOrStdout()) cmd.SetErr(cmd.ErrOrStderr()) - clientCtx = clientCtx.WithCmdContext(cmd.Context()) + clientCtx = clientCtx.WithCmdContext(cmd.Context()).WithViper("") clientCtx, err := client.ReadPersistentCommandFlags(clientCtx, cmd.Flags()) if err != nil { return err