Skip to content

Commit

Permalink
Refactor config template customization to enhance scalability
Browse files Browse the repository at this point in the history
  • Loading branch information
lucaslopezf committed Mar 30, 2024
1 parent 34eff4f commit 8179af2
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 7 deletions.
7 changes: 7 additions & 0 deletions client/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"os"
"path/filepath"
"strings"

"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
Expand Down Expand Up @@ -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)

Check failure on line 173 in client/config/config.go

View workflow job for this annotation

GitHub Actions / golangci-lint

wrapperFunc: use strings.ReplaceAll method in `strings.Replace(DefaultClientConfigTemplate, CustomConfigKey, customConfig, -1)` (gocritic)

Check failure on line 173 in client/config/config.go

View workflow job for this annotation

GitHub Actions / Analyze

wrapperFunc: use strings.ReplaceAll method in `strings.Replace(DefaultClientConfigTemplate, CustomConfigKey, customConfig, -1)` (gocritic)
}

// 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.
Expand Down
4 changes: 2 additions & 2 deletions client/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
13 changes: 12 additions & 1 deletion client/config/toml.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
###############################################################################
Expand All @@ -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 ###
###############################################################################
Expand All @@ -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

Expand Down
10 changes: 6 additions & 4 deletions simapp/simd/cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down

0 comments on commit 8179af2

Please sign in to comment.