Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(cli): refactor and cleanup config #1793

Merged
merged 27 commits into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
b67f39d
refactor
ocnc2 Jul 26, 2024
cebceaa
Merge branch 'main' into toml-is-my-loml
ocnc2 Jul 26, 2024
4b89297
tidy sync
ocnc2 Jul 26, 2024
7ebfd75
Merge branch 'main' into toml-is-my-loml
ocnc2 Jul 26, 2024
e7232d1
check if e2e passes, why blutgang broken
ocnc2 Jul 26, 2024
a51032d
Merge branch 'main' into toml-is-my-loml
ocnc2 Jul 26, 2024
ddb4095
Merge branch 'main' into toml-is-my-loml
ocnc2 Jul 26, 2024
619c0c2
Merge branch 'main' into toml-is-my-loml
ocnc2 Jul 26, 2024
57bbb78
ermset
ocnc2 Jul 26, 2024
ffa19d5
Merge branch 'main' into toml-is-my-loml
ocnc2 Jul 26, 2024
d5e9e58
Merge branch 'main' into toml-is-my-loml
ocnc2 Jul 26, 2024
809b7d2
Merge branch 'main' into toml-is-my-loml
ocnc2 Jul 28, 2024
9d89f13
revert second bind
ocnc2 Jul 28, 2024
4655071
Merge branch 'main' into toml-is-my-loml
ocnc2 Jul 29, 2024
209fa85
e2e not working locally reeee
ocnc2 Jul 29, 2024
1262194
Merge remote-tracking branch 'origin/toml-is-my-loml' into toml-is-my…
ocnc2 Jul 29, 2024
fbcd2fc
Merge branch 'main' into toml-is-my-loml
ocnc2 Jul 29, 2024
9000224
Merge branch 'main' into toml-is-my-loml
ocnc2 Jul 29, 2024
5ac98cc
small cleanup
ocnc2 Jul 29, 2024
2fa8e56
Merge remote-tracking branch 'origin/toml-is-my-loml' into toml-is-my…
ocnc2 Jul 29, 2024
368550a
lint
ocnc2 Jul 29, 2024
62deb9a
Merge branch 'main' into toml-is-my-loml
ocnc2 Jul 29, 2024
3c3fba1
Merge branch 'main' into toml-is-my-loml
ocnc2 Jul 29, 2024
44c2f83
Merge branch 'main' into toml-is-my-loml
ocnc2 Jul 29, 2024
0d1012a
Merge branch 'main' into toml-is-my-loml
ocnc2 Jul 29, 2024
08ec33b
Merge branch 'main' into toml-is-my-loml
ocnc2 Jul 30, 2024
523a541
Merge branch 'main' into toml-is-my-loml
ocnc2 Jul 30, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions mod/cli/pkg/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
"cosmossdk.io/depinject"
sdklog "cosmossdk.io/log"
cmdlib "github.com/berachain/beacon-kit/mod/cli/pkg/commands"
"github.com/berachain/beacon-kit/mod/cli/pkg/utils/context"
"github.com/berachain/beacon-kit/mod/cli/pkg/config"
"github.com/berachain/beacon-kit/mod/log"
"github.com/berachain/beacon-kit/mod/node-core/pkg/components"
"github.com/berachain/beacon-kit/mod/node-core/pkg/types"
Expand Down Expand Up @@ -153,18 +153,20 @@ func (cb *CLIBuilder[T, ExecutionPayloadT]) defaultRunHandler(
}
}

// InterceptConfigsPreRunHandler is identical to
// InterceptConfigsAndCreateContext except it also sets the server context on
// the command and the server logger.
func (cb *CLIBuilder[T, ExecutionPayloadT]) InterceptConfigsPreRunHandler(
cmd *cobra.Command,
logger log.AdvancedLogger[any, sdklog.Logger],
customAppConfigTemplate string,
customAppConfig interface{},
cmtConfig *cmtcfg.Config,
) error {
serverCtx, err := context.InterceptConfigsAndCreateContext(
cmd, customAppConfigTemplate, customAppConfig, cmtConfig, logger)
serverCtx, err := config.SetupConfigAndContext(
cmd,
customAppConfigTemplate,
customAppConfig,
cmtConfig,
logger,
)
if err != nil {
return err
}
Expand Down
106 changes: 106 additions & 0 deletions mod/cli/pkg/config/app.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
// SPDX-License-Identifier: BUSL-1.1
//
// Copyright (C) 2024, Berachain Foundation. All rights reserved.
// Use of this software is governed by the Business Source License included
// in the LICENSE file of this repository and at www.mariadb.com/bsl11.
//
// ANY USE OF THE LICENSED WORK IN VIOLATION OF THIS LICENSE WILL AUTOMATICALLY
// TERMINATE YOUR RIGHTS UNDER THIS LICENSE FOR THE CURRENT AND ALL OTHER
// VERSIONS OF THE LICENSED WORK.
//
// THIS LICENSE DOES NOT GRANT YOU ANY RIGHT IN ANY TRADEMARK OR LOGO OF
// LICENSOR OR ITS AFFILIATES (PROVIDED THAT YOU MAY USE A TRADEMARK OR LOGO OF
// LICENSOR AS EXPRESSLY REQUIRED BY THIS LICENSE).
//
// TO THE EXTENT PERMITTED BY APPLICABLE LAW, THE LICENSED WORK IS PROVIDED ON
// AN “AS IS” BASIS. LICENSOR HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS,
// EXPRESS OR IMPLIED, INCLUDING (WITHOUT LIMITATION) WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND
// TITLE.

package config

import (
"errors"
"fmt"
"os"
"path/filepath"

"github.com/cosmos/cosmos-sdk/server/config"
"github.com/spf13/viper"
)

// handleAppConfig writes the provided <customConfig> to the file at
// <configDirPath>/app.toml, or reads it into the provided <viper> instance
// if it exists.
func handleAppConfig(
viper *viper.Viper,
configDirPath string,
customAppTemplate string,
appConfig any,
) error {
// if the app.toml file does not exist, populate it with the values from
// <appConfig>
appCfgFilePath := filepath.Join(configDirPath, "app.toml")
if _, err := os.Stat(appCfgFilePath); os.IsNotExist(err) {
return writeAppConfig(
viper,
appCfgFilePath,
customAppTemplate,
appConfig,
)
}

// merge the app.toml file into the viper instance
viper.SetConfigType("toml")
viper.SetConfigName("app")
viper.AddConfigPath(configDirPath)
if err := viper.MergeInConfig(); err != nil {
return fmt.Errorf("failed to merge configuration: %w", err)
}

return nil
}

// writeAppConfig creates a new configuration file with default
// values at the specified file path <appCfgFilePath>.
func writeAppConfig(
rootViper *viper.Viper,
appConfigFilePath string,
appTemplate string,
appConfig any,
) error {
var err error
appTemplatePopulated := appTemplate != ""
appConfigPopulated := appConfig != nil

// customAppTemplate == nil ⊕ customConfig == nil
if (appTemplatePopulated && !appConfigPopulated) ||
(!appTemplatePopulated && appConfigPopulated) {
return errors.New("customAppTemplate and customConfig " +
"should be both nil or not nil")
}

if appTemplatePopulated {
// set the config template
if err = config.SetConfigTemplate(appTemplate); err != nil {
return fmt.Errorf("failed to set config template: %w", err)
}
// populate appConfig with the values from the viper instance
if err = rootViper.Unmarshal(&appConfig); err != nil {
return fmt.Errorf("failed to unmarshal app config: %w", err)
}
} else {
// read the appConfig from the file at appConfigFilePath
appConfig, err = config.ParseConfig(rootViper)
if err != nil {
return fmt.Errorf("failed to parse %s: %w", appConfigFilePath, err)
}
}
// write the appConfig to the file at appConfigFilePath
if err = config.WriteConfigFile(appConfigFilePath, appConfig); err != nil {
return fmt.Errorf("failed to write %s: %w", appConfigFilePath, err)
}

return nil
}
10 changes: 5 additions & 5 deletions mod/cli/pkg/config/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@

package config

import "github.com/cosmos/cosmos-sdk/client/config"
import client "github.com/cosmos/cosmos-sdk/client/config"

// InitClientConfig sets up the default client configuration, allowing for
// overrides.
func InitClientConfig() (string, interface{}) {
clientCfg := config.DefaultConfig()
clientCfg.KeyringBackend = "test"
return config.DefaultClientConfigTemplate, clientCfg
func InitClientConfig() (string, any) {
clientConfig := client.DefaultConfig()
clientConfig.KeyringBackend = "test"
return client.DefaultClientConfigTemplate, clientConfig
}
104 changes: 104 additions & 0 deletions mod/cli/pkg/config/comet.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
// SPDX-License-Identifier: BUSL-1.1
//
// Copyright (C) 2024, Berachain Foundation. All rights reserved.
// Use of this software is governed by the Business Source License included
// in the LICENSE file of this repository and at www.mariadb.com/bsl11.
//
// ANY USE OF THE LICENSED WORK IN VIOLATION OF THIS LICENSE WILL AUTOMATICALLY
// TERMINATE YOUR RIGHTS UNDER THIS LICENSE FOR THE CURRENT AND ALL OTHER
// VERSIONS OF THE LICENSED WORK.
//
// THIS LICENSE DOES NOT GRANT YOU ANY RIGHT IN ANY TRADEMARK OR LOGO OF
// LICENSOR OR ITS AFFILIATES (PROVIDED THAT YOU MAY USE A TRADEMARK OR LOGO OF
// LICENSOR AS EXPRESSLY REQUIRED BY THIS LICENSE).
//
// TO THE EXTENT PERMITTED BY APPLICABLE LAW, THE LICENSED WORK IS PROVIDED ON
// AN “AS IS” BASIS. LICENSOR HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS,
// EXPRESS OR IMPLIED, INCLUDING (WITHOUT LIMITATION) WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND
// TITLE.

package config

import (
"fmt"
"os"
"time"

cmtcfg "github.com/cometbft/cometbft/config"
"github.com/spf13/viper"
)

// handleCometConfig reads the comet config at <cometConfigFile> into the
// provided <viper> instance. If the file does not exist, it will be populated
// with the values from <cometConfig>.
// <cometConfig> will then be updated with the latest values from <viper>.
func handleCometConfig(
viper *viper.Viper,
cometConfigFile string,
cometConfig *cmtcfg.Config,
rootDir string,
configDirPath string,
) error {
_, err := os.Stat(cometConfigFile)
if os.IsNotExist(err) {
if err = writeCometConfig(
cometConfigFile,
cometConfig,
rootDir,
); err != nil {
return fmt.Errorf("failed to write comet config: %w", err)
}
} else if err != nil {
return err
}

// read the config.toml file into the viper instance
viper.SetConfigType("toml")
viper.SetConfigName("config")
viper.AddConfigPath(configDirPath)

if err = viper.ReadInConfig(); err != nil {
return fmt.Errorf("failed to read in %s: %w", cometConfigFile, err)
}

// update the comet config with the latest values from viper
if err = viper.Unmarshal(cometConfig); err != nil {
return err
}

cometConfig.SetRoot(rootDir)
return nil
}

// writeCometConfig creates a new comet config file one with default values.
// If the file exists, it reads and merges it into the provided Viper
// instance.
func writeCometConfig(
cometConfigFile string,
cometConfig *cmtcfg.Config,
rootDir string,
) error {
cmtcfg.EnsureRoot(rootDir)

if err := cometConfig.ValidateBasic(); err != nil {
return fmt.Errorf("error in config file: %w", err)
}

// the SDK is very opinionated about these values, so we override them
// if they aren't already set
defaultCometCfg := cmtcfg.DefaultConfig()
if cometConfig.Consensus.TimeoutCommit ==
defaultCometCfg.Consensus.TimeoutCommit {
//nolint:mnd // 5 seconds
cometConfig.Consensus.TimeoutCommit = 5 * time.Second
}
if cometConfig.RPC.PprofListenAddress ==
defaultCometCfg.RPC.PprofListenAddress {
cometConfig.RPC.PprofListenAddress = "localhost:6060"
}

// write the comet config to the specified file path
cmtcfg.WriteConfigFile(cometConfigFile, cometConfig)
return nil
}
Loading
Loading