From 995db117d0cd6df0a7ab61d5fad271f0c1556621 Mon Sep 17 00:00:00 2001 From: Anton Nikiforov Date: Fri, 14 Apr 2023 12:04:46 +0300 Subject: [PATCH] [#238] node: Read cfg from dir even if cfg file not set Signed-off-by: Anton Nikiforov --- CHANGELOG.md | 1 + cmd/frostfs-node/config/configdir_test.go | 24 +++++++++++++++++++++++ pkg/util/config/dir.go | 24 +++++++++-------------- 3 files changed, 34 insertions(+), 15 deletions(-) create mode 100644 cmd/frostfs-node/config/configdir_test.go diff --git a/CHANGELOG.md b/CHANGELOG.md index 9d284f6f..94018e35 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ Changelog for FrostFS Node ### Changed ### Fixed - Take network settings into account during netmap contract update (#100) +- Read config files from dir even if config file not provided via `--config` for node (#238) ### Removed ### Updated diff --git a/cmd/frostfs-node/config/configdir_test.go b/cmd/frostfs-node/config/configdir_test.go new file mode 100644 index 00000000..2c325567 --- /dev/null +++ b/cmd/frostfs-node/config/configdir_test.go @@ -0,0 +1,24 @@ +package config + +import ( + "os" + "path" + "testing" + + "github.com/spf13/cast" + "github.com/stretchr/testify/require" +) + +func TestConfigDir(t *testing.T) { + dir := t.TempDir() + + cfgFileName0 := path.Join(dir, "cfg_00.json") + cfgFileName1 := path.Join(dir, "cfg_01.yml") + + require.NoError(t, os.WriteFile(cfgFileName0, []byte(`{"storage":{"shard_pool_size":15}}`), 0777)) + require.NoError(t, os.WriteFile(cfgFileName1, []byte("logger:\n level: debug"), 0777)) + + c := New(Prm{}, WithConfigDir(dir)) + require.Equal(t, "debug", cast.ToString(c.Sub("logger").Value("level"))) + require.EqualValues(t, 15, cast.ToUint32(c.Sub("storage").Value("shard_pool_size"))) +} diff --git a/pkg/util/config/dir.go b/pkg/util/config/dir.go index a74992d1..0379fe26 100644 --- a/pkg/util/config/dir.go +++ b/pkg/util/config/dir.go @@ -1,6 +1,7 @@ package config import ( + "fmt" "os" "path" @@ -20,7 +21,7 @@ func ReadConfigDir(v *viper.Viper, configDir string) error { continue } ext := path.Ext(entry.Name()) - if ext != ".yaml" && ext != ".yml" { + if ext != ".yaml" && ext != ".yml" && ext != ".json" { continue } @@ -33,22 +34,15 @@ func ReadConfigDir(v *viper.Viper, configDir string) error { } // mergeConfig reads config file and merge its content with current viper. -func mergeConfig(v *viper.Viper, fileName string) (err error) { - var cfgFile *os.File - cfgFile, err = os.Open(fileName) +func mergeConfig(v *viper.Viper, fileName string) error { + cv := viper.New() + cv.SetConfigFile(fileName) + err := cv.ReadInConfig() if err != nil { - return err + return fmt.Errorf("failed to read config: %w", err) } - - defer func() { - errClose := cfgFile.Close() - if err == nil { - err = errClose - } - }() - - if err = v.MergeConfig(cfgFile); err != nil { - return err + if err = v.MergeConfigMap(cv.AllSettings()); err != nil { + return fmt.Errorf("failed to merge config: %w", err) } return nil