From af38364f5ec209f56b6ae69072b60e22f2d6c093 Mon Sep 17 00:00:00 2001 From: Edward Mack Date: Wed, 5 Apr 2023 10:56:18 -0400 Subject: [PATCH] fix(cmd/gossamer): embed default toml config files (#3091) Co-authored-by: Diego Romero --- chain/resources.go | 10 ++++++++++ cmd/gossamer/config.go | 20 ++++++++++---------- cmd/gossamer/export_test.go | 3 +-- cmd/gossamer/main_test.go | 11 +++++------ cmd/gossamer/toml_config.go | 20 +++++++++++++++----- cmd/gossamer/toml_config_test.go | 6 +++--- lib/utils/utils.go | 8 ++++---- 7 files changed, 48 insertions(+), 30 deletions(-) create mode 100644 chain/resources.go diff --git a/chain/resources.go b/chain/resources.go new file mode 100644 index 0000000000..94bc434580 --- /dev/null +++ b/chain/resources.go @@ -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 diff --git a/cmd/gossamer/config.go b/cmd/gossamer/config.go index 59af93aaf7..01009ce741 100644 --- a/cmd/gossamer/config.go +++ b/cmd/gossamer/config.go @@ -26,10 +26,10 @@ 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 @@ -37,7 +37,7 @@ var ( 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 + "...") @@ -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) { @@ -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) diff --git a/cmd/gossamer/export_test.go b/cmd/gossamer/export_test.go index d713ffc465..117ce0a06c 100644 --- a/cmd/gossamer/export_test.go +++ b/cmd/gossamer/export_test.go @@ -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" @@ -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) diff --git a/cmd/gossamer/main_test.go b/cmd/gossamer/main_test.go index 72a5115035..161d06a2f0 100644 --- a/cmd/gossamer/main_test.go +++ b/cmd/gossamer/main_test.go @@ -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 @@ -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()) } @@ -234,7 +233,7 @@ func TestInitCommand_RenameNodeWhenCalled(t *testing.T) { "--basepath", tempDir, "--genesis", genesisPath, "--name", nodeName, - "--config", defaultWestendDevConfigPath, + "--config", testWestendDevConfigPath, "--force", ) @@ -249,7 +248,7 @@ func TestInitCommand_RenameNodeWhenCalled(t *testing.T) { "init", "--basepath", tempDir, "--genesis", genesisPath, - "--config", defaultWestendDevConfigPath, + "--config", testWestendDevConfigPath, "--force", ) diff --git a/cmd/gossamer/toml_config.go b/cmd/gossamer/toml_config.go index e683a80640..eb719f8f5d 100644 --- a/cmd/gossamer/toml_config.go +++ b/cmd/gossamer/toml_config.go @@ -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 diff --git a/cmd/gossamer/toml_config_test.go b/cmd/gossamer/toml_config_test.go index 77bc7233c7..2081c45456 100644 --- a/cmd/gossamer/toml_config_test.go +++ b/cmd/gossamer/toml_config_test.go @@ -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) } @@ -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) } @@ -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) } diff --git a/lib/utils/utils.go b/lib/utils/utils.go index 3920676327..d94d3e4152 100644 --- a/lib/utils/utils.go +++ b/lib/utils/utils.go @@ -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, ",")