From c0bde14212a7b5d6b5e9e650e12bff976018d97b Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Mon, 12 Feb 2024 11:07:59 +0000 Subject: [PATCH] fix(simapp): fix default home (backport #19393) (#19399) Co-authored-by: Julien Robert --- client/config/config.go | 20 ++++++++++++++++++++ simapp/simd/cmd/root.go | 2 +- simapp/simd/cmd/root_v2.go | 2 +- 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/client/config/config.go b/client/config/config.go index d247927c6e55..e3a2a8646d93 100644 --- a/client/config/config.go +++ b/client/config/config.go @@ -46,6 +46,26 @@ func (c *ClientConfig) SetBroadcastMode(broadcastMode string) { c.BroadcastMode = broadcastMode } +// ReadDefaultValuesFromDefaultClientConfig reads default values from default client.toml file and updates them in client.Context +// The client.toml is then discarded. +func ReadDefaultValuesFromDefaultClientConfig(ctx client.Context) (client.Context, error) { + prevHomeDir := ctx.HomeDir + dir, err := os.MkdirTemp("", "simapp") + if err != nil { + return ctx, fmt.Errorf("couldn't create temp dir: %w", err) + } + defer os.RemoveAll(dir) + + ctx.HomeDir = dir + ctx, err = ReadFromClientConfig(ctx) + if err != nil { + return ctx, fmt.Errorf("couldn't create client config: %w", err) + } + + ctx.HomeDir = prevHomeDir + return ctx, nil +} + // ReadFromClientConfig reads values from client.toml file and updates them in client Context func ReadFromClientConfig(ctx client.Context) (client.Context, error) { configPath := filepath.Join(ctx.HomeDir, "config") diff --git a/simapp/simd/cmd/root.go b/simapp/simd/cmd/root.go index 7260f000c39f..5e54ab06c6fc 100644 --- a/simapp/simd/cmd/root.go +++ b/simapp/simd/cmd/root.go @@ -100,7 +100,7 @@ func NewRootCmd() *cobra.Command { // add keyring to autocli opts autoCliOpts := tempApp.AutoCliOpts() - initClientCtx, _ = config.ReadFromClientConfig(initClientCtx) + initClientCtx, _ = config.ReadDefaultValuesFromDefaultClientConfig(initClientCtx) autoCliOpts.Keyring, _ = keyring.NewAutoCLIKeyring(initClientCtx.Keyring) autoCliOpts.ClientCtx = initClientCtx diff --git a/simapp/simd/cmd/root_v2.go b/simapp/simd/cmd/root_v2.go index e6512b452352..dd6681472373 100644 --- a/simapp/simd/cmd/root_v2.go +++ b/simapp/simd/cmd/root_v2.go @@ -109,7 +109,7 @@ func ProvideClientContext( WithViper("") // In simapp, we don't use any prefix for env variables. // Read the config again to overwrite the default values with the values from the config file - clientCtx, _ = config.ReadFromClientConfig(clientCtx) + clientCtx, _ = config.ReadDefaultValuesFromDefaultClientConfig(clientCtx) // re-create the tx config grpc instead of bank keeper txConfigOpts.TextualCoinMetadataQueryFn = authtxconfig.NewGRPCCoinMetadataQueryFn(clientCtx)