diff --git a/client/config/config.go b/client/config/config.go index f9d9608fa340..56c69914c2cb 100644 --- a/client/config/config.go +++ b/client/config/config.go @@ -5,6 +5,7 @@ import ( "fmt" "os" "path/filepath" + "strings" "google.golang.org/grpc" "google.golang.org/grpc/credentials" @@ -166,6 +167,12 @@ func CreateClientConfig(ctx client.Context, customClientTemplate string, customC return ctx, nil } +// CustomizeConfigTemplate inserts custom configuration settings into the default config template by replacing a predefined placeholder +// This approach prevents issues that could arise from direct concatenation, such as incorrect section categorization of custom settings. +func CustomizeConfigTemplate(customConfig string) string { + return strings.Replace(DefaultClientConfigTemplate, CustomConfigKey, customConfig, -1) +} + // getGRPCClient creates and returns a new gRPC client connection based on the GRPCConfig. // It determines the type of connection (secure or insecure) from the GRPCConfig and // uses the specified server address to establish the connection. diff --git a/client/config/config_test.go b/client/config/config_test.go index 76f8bd7eb171..bfe6b1d3e2d8 100644 --- a/client/config/config_test.go +++ b/client/config/config_test.go @@ -80,14 +80,14 @@ func TestCustomTemplateAndConfig(t *testing.T) { Note: "Sent from the CLI.", } - customClientConfigTemplate := config.DefaultClientConfigTemplate + ` + customConfig := ` # This is the gas adjustment factor used by the tx commands. # Sets the default and can be overwritten by the --gas-adjustment flag in tx commands. gas-adjustment = {{ .GasConfig.GasAdjustment }} # Memo to include in all transactions. note = "{{ .Note }}" ` - + customClientConfigTemplate := config.CustomizeConfigTemplate(customConfig) t.Run("custom template and config provided", func(t *testing.T) { clientCtx, cleanup, err := initClientContextWithTemplate(t, "", customClientConfigTemplate, customClientConfig) defer func() { diff --git a/client/config/toml.go b/client/config/toml.go index 21a09d4f8d65..fb4709a849b3 100644 --- a/client/config/toml.go +++ b/client/config/toml.go @@ -8,7 +8,9 @@ import ( "github.com/spf13/viper" ) -const DefaultClientConfigTemplate = `# This is a TOML config file. +const ( + CustomConfigKey = "#{CustomConfigs}" + DefaultClientConfigTemplate = `# This is a TOML config file. # For more information, see https://github.com/toml-lang/toml ############################################################################### @@ -28,6 +30,14 @@ node = "{{ .Node }}" # Transaction broadcasting mode (sync|async) broadcast-mode = "{{ .BroadcastMode }}" +############################################################################### +### custom config ### +############################################################################### + +### Custom Configurations +# The placeholder below is used for injecting custom configurations. +#{CustomConfigs} + ############################################################################### ### gRPC Configuration ### ############################################################################### @@ -41,6 +51,7 @@ address = "{{ .GRPC.Address }}" # It can be overwritten by the --grpc-insecure flag in each command. insecure = {{ .GRPC.Insecure }} ` +) var configTemplate *template.Template diff --git a/simapp/simd/cmd/config.go b/simapp/simd/cmd/config.go index cda73a922dd1..f065f6f44ae2 100644 --- a/simapp/simd/cmd/config.go +++ b/simapp/simd/cmd/config.go @@ -56,15 +56,17 @@ func initClientConfig() (string, interface{}) { }, } - // The default SDK app template is defined in serverconfig.DefaultConfigTemplate. - // We append the custom config template to the default one. - // And we set the default config to the custom app template. - customClientConfigTemplate := clientconfig.DefaultClientConfigTemplate + strings.TrimSpace(` + // Create a customConfig to define specific settings. + customConfig := strings.TrimSpace(` # This is default the gas adjustment factor used in tx commands. # It can be overwritten by the --gas-adjustment flag in each tx command. gas-adjustment = {{ .GasConfig.GasAdjustment }} `) + // The CustomizeConfigTemplate function is then employed to seamlessly integrate these custom settings + // into the default configuration template. + customClientConfigTemplate := clientconfig.CustomizeConfigTemplate(customConfig) + return customClientConfigTemplate, customClientConfig }