feat(client): allow overwritting client.toml (#17513)
This commit is contained in:
@@ -49,7 +49,8 @@ func TestStreamingConfig(t *testing.T) {
|
||||
|
||||
testDir := t.TempDir()
|
||||
cfgFile := filepath.Join(testDir, "app.toml")
|
||||
WriteConfigFile(cfgFile, &cfg)
|
||||
err := WriteConfigFile(cfgFile, &cfg)
|
||||
require.NoError(t, err)
|
||||
|
||||
cfgFileBz, err := os.ReadFile(cfgFile)
|
||||
require.NoError(t, err, "reading %s", cfgFile)
|
||||
@@ -100,7 +101,8 @@ func TestParseStreaming(t *testing.T) {
|
||||
func TestReadConfig(t *testing.T) {
|
||||
cfg := DefaultConfig()
|
||||
tmpFile := filepath.Join(t.TempDir(), "config")
|
||||
WriteConfigFile(tmpFile, cfg)
|
||||
err := WriteConfigFile(tmpFile, cfg)
|
||||
require.NoError(t, err)
|
||||
|
||||
v := viper.New()
|
||||
otherCfg, err := GetConfig(v)
|
||||
@@ -117,13 +119,14 @@ func TestIndexEventsWriteRead(t *testing.T) {
|
||||
conf := DefaultConfig()
|
||||
conf.IndexEvents = expected
|
||||
|
||||
WriteConfigFile(confFile, conf)
|
||||
err := WriteConfigFile(confFile, conf)
|
||||
require.NoError(t, err)
|
||||
|
||||
// read the file into Viper
|
||||
vpr := viper.New()
|
||||
vpr.SetConfigFile(confFile)
|
||||
|
||||
err := vpr.ReadInConfig()
|
||||
err = vpr.ReadInConfig()
|
||||
require.NoError(t, err, "reading config file into viper")
|
||||
|
||||
// Check that the raw viper value is correct.
|
||||
@@ -168,7 +171,8 @@ func TestGlobalLabelsWriteRead(t *testing.T) {
|
||||
confFile := filepath.Join(t.TempDir(), "app.toml")
|
||||
conf := DefaultConfig()
|
||||
conf.Telemetry.GlobalLabels = expected
|
||||
WriteConfigFile(confFile, conf)
|
||||
err := WriteConfigFile(confFile, conf)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Read that file into viper.
|
||||
vpr := viper.New()
|
||||
@@ -197,7 +201,7 @@ func TestSetConfigTemplate(t *testing.T) {
|
||||
// Set the template to the default one.
|
||||
initTmpl := configTemplate
|
||||
require.NotPanics(t, func() {
|
||||
SetConfigTemplate(DefaultConfigTemplate)
|
||||
_ = SetConfigTemplate(DefaultConfigTemplate)
|
||||
}, "SetConfigTemplate")
|
||||
setTmpl := configTemplate
|
||||
require.NotSame(t, initTmpl, setTmpl, "configTemplate after set")
|
||||
|
||||
+13
-17
@@ -243,14 +243,12 @@ func init() {
|
||||
var err error
|
||||
|
||||
tmpl := template.New("appConfigFileTemplate")
|
||||
|
||||
if configTemplate, err = tmpl.Parse(DefaultConfigTemplate); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
// ParseConfig retrieves the default environment configuration for the
|
||||
// application.
|
||||
// ParseConfig retrieves the default environment configuration for the application.
|
||||
func ParseConfig(v *viper.Viper) (*Config, error) {
|
||||
conf := DefaultConfig()
|
||||
err := v.Unmarshal(conf)
|
||||
@@ -258,32 +256,30 @@ func ParseConfig(v *viper.Viper) (*Config, error) {
|
||||
return conf, err
|
||||
}
|
||||
|
||||
// SetConfigTemplate sets the custom app config template for
|
||||
// the application
|
||||
func SetConfigTemplate(customTemplate string) {
|
||||
// SetConfigTemplate sets the custom app config template for the application.
|
||||
func SetConfigTemplate(customTemplate string) error {
|
||||
var err error
|
||||
|
||||
tmpl := template.New("appConfigFileTemplate")
|
||||
|
||||
if configTemplate, err = tmpl.Parse(customTemplate); err != nil {
|
||||
panic(err)
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// WriteConfigFile renders config using the template and writes it to
|
||||
// configFilePath.
|
||||
func WriteConfigFile(configFilePath string, config interface{}) {
|
||||
// WriteConfigFile renders config using the template and writes it to configFilePath.
|
||||
func WriteConfigFile(configFilePath string, config interface{}) error {
|
||||
var buffer bytes.Buffer
|
||||
|
||||
if err := configTemplate.Execute(&buffer, config); err != nil {
|
||||
panic(err)
|
||||
return err
|
||||
}
|
||||
|
||||
mustWriteFile(configFilePath, buffer.Bytes(), 0o644)
|
||||
}
|
||||
|
||||
func mustWriteFile(filePath string, contents []byte, mode os.FileMode) {
|
||||
if err := os.WriteFile(filePath, contents, mode); err != nil {
|
||||
panic(fmt.Errorf("failed to write file: %w", err))
|
||||
if err := os.WriteFile(configFilePath, buffer.Bytes(), 0o600); err != nil {
|
||||
return fmt.Errorf("failed to write file: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
+13
-3
@@ -281,21 +281,31 @@ func interceptConfigs(rootViper *viper.Viper, customAppTemplate string, customCo
|
||||
|
||||
appCfgFilePath := filepath.Join(configPath, "app.toml")
|
||||
if _, err := os.Stat(appCfgFilePath); os.IsNotExist(err) {
|
||||
if (customAppTemplate != "" && customConfig == nil) || (customAppTemplate == "" && customConfig != nil) {
|
||||
return nil, fmt.Errorf("customAppTemplate and customConfig should be both nil or not nil")
|
||||
}
|
||||
|
||||
if customAppTemplate != "" {
|
||||
config.SetConfigTemplate(customAppTemplate)
|
||||
if err := config.SetConfigTemplate(customAppTemplate); err != nil {
|
||||
return nil, fmt.Errorf("failed to set config template: %w", err)
|
||||
}
|
||||
|
||||
if err = rootViper.Unmarshal(&customConfig); err != nil {
|
||||
return nil, fmt.Errorf("failed to parse %s: %w", appCfgFilePath, err)
|
||||
}
|
||||
|
||||
config.WriteConfigFile(appCfgFilePath, customConfig)
|
||||
if err := config.WriteConfigFile(appCfgFilePath, customConfig); err != nil {
|
||||
return nil, fmt.Errorf("failed to write %s: %w", appCfgFilePath, err)
|
||||
}
|
||||
} else {
|
||||
appConf, err := config.ParseConfig(rootViper)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to parse %s: %w", appCfgFilePath, err)
|
||||
}
|
||||
|
||||
config.WriteConfigFile(appCfgFilePath, appConf)
|
||||
if err := config.WriteConfigFile(appCfgFilePath, appConf); err != nil {
|
||||
return nil, fmt.Errorf("failed to write %s: %w", appCfgFilePath, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+2
-1
@@ -449,7 +449,8 @@ func TestEmptyMinGasPrices(t *testing.T) {
|
||||
appCfgTempFilePath := filepath.Join(tempDir, "config", "app.toml")
|
||||
appConf := config.DefaultConfig()
|
||||
appConf.BaseConfig.MinGasPrices = ""
|
||||
config.WriteConfigFile(appCfgTempFilePath, appConf)
|
||||
err = config.WriteConfigFile(appCfgTempFilePath, appConf)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Run StartCmd.
|
||||
cmd = server.StartCmd(nil)
|
||||
|
||||
Reference in New Issue
Block a user