Skip to content

Commit 8e2d531

Browse files
committed
feat: add overwrite config with recommended defaults
1 parent 767e06b commit 8e2d531

File tree

3 files changed

+93
-25
lines changed

3 files changed

+93
-25
lines changed

cmd/sentinelhub/config.go

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package main
2+
3+
import (
4+
"os"
5+
"path/filepath"
6+
"time"
7+
8+
tmcfg "github.com/cometbft/cometbft/config"
9+
"github.com/cosmos/cosmos-sdk/client/flags"
10+
serverconfig "github.com/cosmos/cosmos-sdk/server/config"
11+
"github.com/spf13/viper"
12+
)
13+
14+
const flagOverwriteConfigWithDefaults = "overwrite-config-with-defaults"
15+
16+
// applyRecommendedValues sets default values for specific configuration types.
17+
func applyRecommendedValues(cfg interface{}) {
18+
switch c := cfg.(type) {
19+
case *serverconfig.Config:
20+
c.BaseConfig.MinGasPrices = "0.1udvpn"
21+
c.StateSync.SnapshotInterval = 1000
22+
case *tmcfg.Config:
23+
c.Consensus.TimeoutCommit = 3 * time.Second
24+
}
25+
}
26+
27+
// initAppConfig initializes the application configuration with defaults.
28+
func initAppConfig() (string, interface{}) {
29+
cfg := serverconfig.DefaultConfig()
30+
cfgTemplate := serverconfig.DefaultConfigTemplate
31+
applyRecommendedValues(cfg)
32+
return cfgTemplate, cfg
33+
}
34+
35+
// initTendermintConfig initializes the Tendermint configuration with defaults.
36+
func initTendermintConfig() *tmcfg.Config {
37+
cfg := tmcfg.DefaultConfig()
38+
applyRecommendedValues(cfg)
39+
return cfg
40+
}
41+
42+
// overwriteConfig reads, updates, and writes a configuration file.
43+
func overwriteConfig(name string, cfg interface{}, write func(string, interface{}) error) error {
44+
homeDir := viper.GetString(flags.FlagHome)
45+
cfgPath := filepath.Join(homeDir, "config", name)
46+
47+
if _, err := os.Stat(cfgPath); err != nil {
48+
return nil
49+
}
50+
51+
v := viper.New()
52+
v.SetConfigFile(cfgPath)
53+
if err := v.ReadInConfig(); err != nil {
54+
return err
55+
}
56+
if err := v.Unmarshal(cfg); err != nil {
57+
return err
58+
}
59+
60+
applyRecommendedValues(cfg)
61+
return write(cfgPath, cfg)
62+
}
63+
64+
// overwriteAppConfig updates and writes the app configuration.
65+
func overwriteAppConfig() error {
66+
return overwriteConfig("app.toml", &serverconfig.Config{}, func(cfgPath string, cfg interface{}) error {
67+
serverconfig.WriteConfigFile(cfgPath, cfg.(*serverconfig.Config))
68+
return nil
69+
})
70+
}
71+
72+
// overwriteTendermintConfig updates and writes the Tendermint configuration.
73+
func overwriteTendermintConfig() error {
74+
return overwriteConfig("config.toml", &tmcfg.Config{}, func(cfgPath string, cfg interface{}) error {
75+
tmcfg.WriteConfigFile(cfgPath, cfg.(*tmcfg.Config))
76+
return nil
77+
})
78+
}

cmd/sentinelhub/root.go

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"os"
55

66
"github.com/CosmWasm/wasmd/x/wasm"
7-
tmcfg "github.com/cometbft/cometbft/config"
87
tmcli "github.com/cometbft/cometbft/libs/cli"
98
"github.com/cosmos/cosmos-sdk/client"
109
clientconfig "github.com/cosmos/cosmos-sdk/client/config"
@@ -13,39 +12,20 @@ import (
1312
"github.com/cosmos/cosmos-sdk/client/keys"
1413
"github.com/cosmos/cosmos-sdk/client/rpc"
1514
"github.com/cosmos/cosmos-sdk/server"
16-
serverconfig "github.com/cosmos/cosmos-sdk/server/config"
1715
authcli "github.com/cosmos/cosmos-sdk/x/auth/client/cli"
1816
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
1917
"github.com/cosmos/cosmos-sdk/x/crisis"
2018
genutilcli "github.com/cosmos/cosmos-sdk/x/genutil/client/cli"
2119
"github.com/spf13/cobra"
20+
"github.com/spf13/viper"
2221

2322
"github.com/sentinel-official/hub/v12/app"
2423
)
2524

26-
func initAppConfig() (string, interface{}) {
27-
type Config struct {
28-
*serverconfig.Config
29-
}
30-
31-
cfg := Config{Config: serverconfig.DefaultConfig()}
32-
cfg.BaseConfig.MinGasPrices = "0.1udvpn"
33-
cfg.StateSync.SnapshotInterval = 1000
34-
35-
cfgTemplate := serverconfig.DefaultConfigTemplate
36-
37-
return cfgTemplate, cfg
38-
}
39-
40-
func initTendermintConfig() *tmcfg.Config {
41-
cfg := tmcfg.DefaultConfig()
42-
43-
return cfg
44-
}
45-
4625
func moduleInitFlags(cmd *cobra.Command) {
4726
crisis.AddModuleInitFlags(cmd)
4827
wasm.AddModuleInitFlags(cmd)
28+
cmd.Flags().Bool(flagOverwriteConfigWithDefaults, true, "If set to true, recommended default values will overwrite any existing settings in config.toml and app.toml.")
4929
}
5030

5131
func queryCommand() *cobra.Command {
@@ -100,9 +80,19 @@ func txCommand() *cobra.Command {
10080
func NewRootCmd(homeDir string) *cobra.Command {
10181
encCfg := app.DefaultEncodingConfig()
10282
cmd := &cobra.Command{
103-
Use: "sentinelhub",
104-
Short: "Sentinel Hub application",
83+
Use: "sentinelhub",
84+
Short: "Sentinel Hub application",
85+
SilenceUsage: true,
10586
PersistentPreRunE: func(cmd *cobra.Command, _ []string) (err error) {
87+
if viper.GetBool(flagOverwriteConfigWithDefaults) {
88+
if err := overwriteTendermintConfig(); err != nil {
89+
return err
90+
}
91+
if err := overwriteAppConfig(); err != nil {
92+
return err
93+
}
94+
}
95+
10696
clientCtx := client.Context{}.
10797
WithAccountRetriever(authtypes.AccountRetriever{}).
10898
WithCodec(encCfg.Codec).

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ require (
2626
github.com/spf13/cast v1.6.0
2727
github.com/spf13/cobra v1.8.1
2828
github.com/spf13/pflag v1.0.5
29+
github.com/spf13/viper v1.19.0
2930
github.com/stretchr/testify v1.9.0
3031
google.golang.org/genproto/googleapis/api v0.0.0-20240814211410-ddb44dafa142
3132
google.golang.org/grpc v1.67.0
@@ -162,7 +163,6 @@ require (
162163
github.com/sasha-s/go-deadlock v0.3.5 // indirect
163164
github.com/sourcegraph/conc v0.3.0 // indirect
164165
github.com/spf13/afero v1.11.0 // indirect
165-
github.com/spf13/viper v1.19.0 // indirect
166166
github.com/subosito/gotenv v1.6.0 // indirect
167167
github.com/syndtr/goleveldb v1.0.1-0.20220721030215-126854af5e6d // indirect
168168
github.com/tendermint/go-amino v0.16.0 // indirect

0 commit comments

Comments
 (0)