-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add overwrite config with recommended defaults
- Loading branch information
1 parent
767e06b
commit 8e2d531
Showing
3 changed files
with
93 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package main | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
"time" | ||
|
||
tmcfg "github.com/cometbft/cometbft/config" | ||
"github.com/cosmos/cosmos-sdk/client/flags" | ||
serverconfig "github.com/cosmos/cosmos-sdk/server/config" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
const flagOverwriteConfigWithDefaults = "overwrite-config-with-defaults" | ||
|
||
// applyRecommendedValues sets default values for specific configuration types. | ||
func applyRecommendedValues(cfg interface{}) { | ||
switch c := cfg.(type) { | ||
case *serverconfig.Config: | ||
c.BaseConfig.MinGasPrices = "0.1udvpn" | ||
c.StateSync.SnapshotInterval = 1000 | ||
case *tmcfg.Config: | ||
c.Consensus.TimeoutCommit = 3 * time.Second | ||
} | ||
} | ||
|
||
// initAppConfig initializes the application configuration with defaults. | ||
func initAppConfig() (string, interface{}) { | ||
cfg := serverconfig.DefaultConfig() | ||
cfgTemplate := serverconfig.DefaultConfigTemplate | ||
applyRecommendedValues(cfg) | ||
return cfgTemplate, cfg | ||
} | ||
|
||
// initTendermintConfig initializes the Tendermint configuration with defaults. | ||
func initTendermintConfig() *tmcfg.Config { | ||
cfg := tmcfg.DefaultConfig() | ||
applyRecommendedValues(cfg) | ||
return cfg | ||
} | ||
|
||
// overwriteConfig reads, updates, and writes a configuration file. | ||
func overwriteConfig(name string, cfg interface{}, write func(string, interface{}) error) error { | ||
homeDir := viper.GetString(flags.FlagHome) | ||
cfgPath := filepath.Join(homeDir, "config", name) | ||
|
||
if _, err := os.Stat(cfgPath); err != nil { | ||
return nil | ||
} | ||
|
||
v := viper.New() | ||
v.SetConfigFile(cfgPath) | ||
if err := v.ReadInConfig(); err != nil { | ||
return err | ||
} | ||
if err := v.Unmarshal(cfg); err != nil { | ||
return err | ||
} | ||
|
||
applyRecommendedValues(cfg) | ||
return write(cfgPath, cfg) | ||
} | ||
|
||
// overwriteAppConfig updates and writes the app configuration. | ||
func overwriteAppConfig() error { | ||
return overwriteConfig("app.toml", &serverconfig.Config{}, func(cfgPath string, cfg interface{}) error { | ||
serverconfig.WriteConfigFile(cfgPath, cfg.(*serverconfig.Config)) | ||
return nil | ||
}) | ||
} | ||
|
||
// overwriteTendermintConfig updates and writes the Tendermint configuration. | ||
func overwriteTendermintConfig() error { | ||
return overwriteConfig("config.toml", &tmcfg.Config{}, func(cfgPath string, cfg interface{}) error { | ||
tmcfg.WriteConfigFile(cfgPath, cfg.(*tmcfg.Config)) | ||
return nil | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters