Skip to content

Commit

Permalink
fix(cmd/gossamer): embed default toml config files (#3091)
Browse files Browse the repository at this point in the history
Co-authored-by: Diego Romero <diego2737@gmail.com>
  • Loading branch information
edwardmack and dimartiro committed Apr 5, 2023
1 parent d1e9475 commit af38364
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 30 deletions.
10 changes: 10 additions & 0 deletions chain/resources.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Copyright 2023 ChainSafe Systems (ON)
// SPDX-License-Identifier: LGPL-3.0-only
package chain

import "embed"

// DefaultConfigTomlFiles is the embedded file system containing the default toml configurations.
//
//go:embed */*.toml
var DefaultConfigTomlFiles embed.FS
20 changes: 10 additions & 10 deletions cmd/gossamer/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,18 @@ import (
var (
// DefaultCfg is the default configuration for the node.
DefaultCfg = dot.PolkadotConfig
defaultKusamaConfigPath = "./chain/kusama/config.toml"
defaultPolkadotConfigPath = "./chain/polkadot/config.toml"
defaultWestendDevConfigPath = "./chain/westend-dev/config.toml"
defaultWestendConfigPath = "./chain/westend/config.toml"
defaultKusamaConfigPath = "kusama/config.toml"
defaultPolkadotConfigPath = "polkadot/config.toml"
defaultWestendDevConfigPath = "westend-dev/config.toml"
defaultWestendConfigPath = "westend/config.toml"
)

// loadConfigFile loads a default config file if --chain is specified, a specific
// config if --config is specified, or the default gossamer config otherwise.
func loadConfigFile(ctx *cli.Context, cfg *ctoml.Config) (err error) {
cfgPath := ctx.String(ConfigFlag.Name)
if cfgPath == "" {
return loadConfig(cfg, defaultPolkadotConfigPath)
return loadConfigFromResource(cfg, defaultPolkadotConfigPath)
}

logger.Info("loading toml configuration from " + cfgPath + "...")
Expand All @@ -48,7 +48,7 @@ func loadConfigFile(ctx *cli.Context, cfg *ctoml.Config) (err error) {
"overwriting default configuration with id " + cfg.Global.ID +
" with toml configuration values from " + cfgPath)
}
return loadConfig(cfg, cfgPath)
return loadConfigFromFile(cfg, cfgPath)
}

func setupConfigFromChain(ctx *cli.Context) (*ctoml.Config, *dot.Config, error) {
Expand All @@ -68,22 +68,22 @@ func setupConfigFromChain(ctx *cli.Context) (*ctoml.Config, *dot.Config, error)
logger.Info("loading toml configuration from " + defaultKusamaConfigPath + "...")
tomlCfg = &ctoml.Config{}
cfg = dot.KusamaConfig()
err = loadConfig(tomlCfg, defaultKusamaConfigPath)
err = loadConfigFromResource(tomlCfg, defaultKusamaConfigPath)
case "polkadot":
logger.Info("loading toml configuration from " + defaultPolkadotConfigPath + "...")
tomlCfg = &ctoml.Config{}
cfg = dot.PolkadotConfig()
err = loadConfig(tomlCfg, defaultPolkadotConfigPath)
err = loadConfigFromResource(tomlCfg, defaultPolkadotConfigPath)
case "westend-dev":
logger.Info("loading toml configuration from " + defaultWestendDevConfigPath + "...")
tomlCfg = &ctoml.Config{}
cfg = dot.WestendDevConfig()
err = loadConfig(tomlCfg, defaultWestendDevConfigPath)
err = loadConfigFromResource(tomlCfg, defaultWestendDevConfigPath)
case "westend":
logger.Info("loading toml configuration from " + defaultWestendConfigPath + "...")
tomlCfg = &ctoml.Config{}
cfg = dot.WestendConfig()
err = loadConfig(tomlCfg, defaultWestendConfigPath)
err = loadConfigFromResource(tomlCfg, defaultWestendConfigPath)
default:
logger.Info("loading chain config from " + id + "...")
fileInfo, err := os.Stat(id)
Expand Down
3 changes: 1 addition & 2 deletions cmd/gossamer/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"testing"

"github.com/ChainSafe/gossamer/dot"

ctoml "github.com/ChainSafe/gossamer/dot/config/toml"
"github.com/ChainSafe/gossamer/internal/log"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -165,7 +164,7 @@ func TestExportCommand(t *testing.T) {
config := ctx.String(ConfigFlag.Name)

cfg := new(ctoml.Config)
err = loadConfig(cfg, config)
err = loadConfigFromFile(cfg, config)
require.NoError(t, err)

require.Equal(t, dotConfigToToml(c.expected), cfg)
Expand Down
11 changes: 5 additions & 6 deletions cmd/gossamer/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,8 @@ func runTestGossamer(t *testing.T, args ...string) *testgossamer {
return tt
}

var testWestendDevConfigPath string

func TestMain(m *testing.M) {
if reexec.Init() {
return
Expand All @@ -202,10 +204,7 @@ func TestMain(m *testing.M) {
panic(err)
}

defaultKusamaConfigPath = filepath.Join(rootPath, "./chain/kusama/config.toml")
defaultPolkadotConfigPath = filepath.Join(rootPath, "./chain/polkadot/config.toml")
defaultWestendDevConfigPath = filepath.Join(rootPath, "./chain/westend-dev/config.toml")

testWestendDevConfigPath = filepath.Join(rootPath, "./chain/westend-dev/config.toml")
os.Exit(m.Run())
}

Expand Down Expand Up @@ -234,7 +233,7 @@ func TestInitCommand_RenameNodeWhenCalled(t *testing.T) {
"--basepath", tempDir,
"--genesis", genesisPath,
"--name", nodeName,
"--config", defaultWestendDevConfigPath,
"--config", testWestendDevConfigPath,
"--force",
)

Expand All @@ -249,7 +248,7 @@ func TestInitCommand_RenameNodeWhenCalled(t *testing.T) {
"init",
"--basepath", tempDir,
"--genesis", genesisPath,
"--config", defaultWestendDevConfigPath,
"--config", testWestendDevConfigPath,
"--force",
)

Expand Down
20 changes: 15 additions & 5 deletions cmd/gossamer/toml_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,35 @@ import (
"reflect"
"unicode"

"github.com/naoina/toml"

"github.com/ChainSafe/gossamer/chain"
ctoml "github.com/ChainSafe/gossamer/dot/config/toml"
"github.com/naoina/toml"
)

// loadConfig loads the values from the toml configuration file into the provided configuration
func loadConfig(cfg *ctoml.Config, fp string) error {
func loadConfigFromResource(cfg *ctoml.Config, resourcePath string) error {
file, err := chain.DefaultConfigTomlFiles.Open(resourcePath)
if err != nil {
return fmt.Errorf("opening toml configuration file: %w", err)
}
return loadConfig(cfg, file)
}

func loadConfigFromFile(cfg *ctoml.Config, fp string) error {
fp, err := filepath.Abs(fp)
if err != nil {
logger.Errorf("failed to create absolute path for toml configuration file: %s", err)
return err
}

file, err := os.Open(filepath.Clean(fp))
if err != nil {
logger.Errorf("failed to open toml configuration file: %s", err)
return err
}
return loadConfig(cfg, file)
}

// loadConfig loads the values from the toml configuration file into the provided configuration
func loadConfig(cfg *ctoml.Config, file fs.File) (err error) {
var tomlSettings = toml.Config{
NormFieldName: func(rt reflect.Type, key string) string {
return key
Expand Down
6 changes: 3 additions & 3 deletions cmd/gossamer/toml_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func TestLoadConfig(t *testing.T) {
err := dot.InitNode(cfg)
require.NoError(t, err)

err = loadConfig(dotConfigToToml(cfg), cfgFile)
err = loadConfigFromFile(dotConfigToToml(cfg), cfgFile)
require.NoError(t, err)
}

Expand All @@ -43,7 +43,7 @@ func TestLoadConfigWestendDev(t *testing.T) {
projectRootPath := utils.GetProjectRootPathTest(t)
configPath := filepath.Join(projectRootPath, "./chain/westend-dev/config.toml")

err = loadConfig(dotConfigToToml(cfg), configPath)
err = loadConfigFromFile(dotConfigToToml(cfg), configPath)
require.NoError(t, err)
}

Expand All @@ -60,6 +60,6 @@ func TestLoadConfigKusama(t *testing.T) {
projectRootPath := utils.GetProjectRootPathTest(t)
kusamaConfigPath := filepath.Join(projectRootPath, "./chain/kusama/config.toml")

err = loadConfig(dotConfigToToml(cfg), kusamaConfigPath)
err = loadConfigFromFile(dotConfigToToml(cfg), kusamaConfigPath)
require.NoError(t, err)
}
8 changes: 4 additions & 4 deletions lib/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,14 +191,14 @@ var (
ErrFindProjectRoot = errors.New("cannot find project root")
)

// GetProjectRootPath finds the root of the project where `go.mod` is
// and returns it as an absolute path.
// GetProjectRootPath finds the root of the project where directory `cmd`
// and subdirectory `gossamer` is and returns it as an absolute path.
func GetProjectRootPath() (rootPath string, err error) {
_, fullpath, _, _ := runtime.Caller(0)
rootPath = path.Dir(fullpath)

const directoryToFind = "chain"
const subPathsToFind = "westend-dev,westend-local,kusama,polkadot"
const directoryToFind = "cmd"
const subPathsToFind = "gossamer"

subPaths := strings.Split(subPathsToFind, ",")

Expand Down

0 comments on commit af38364

Please sign in to comment.