From 633c5a35de182978b7a57bc50f2d6c31f986483e Mon Sep 17 00:00:00 2001 From: Denis Kirillov Date: Mon, 6 Feb 2023 17:54:49 +0300 Subject: [PATCH] [#44] adm: Support multiple configs Signed-off-by: Denis Kirillov --- cmd/frostfs-adm/internal/commonflags/flags.go | 3 +++ cmd/frostfs-adm/internal/modules/root.go | 21 +++++++++++++++---- config/example/README.md | 3 ++- 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/cmd/frostfs-adm/internal/commonflags/flags.go b/cmd/frostfs-adm/internal/commonflags/flags.go index b31b13255..2f1ae4cc5 100644 --- a/cmd/frostfs-adm/internal/commonflags/flags.go +++ b/cmd/frostfs-adm/internal/commonflags/flags.go @@ -5,6 +5,9 @@ const ( ConfigFlagShorthand = "c" ConfigFlagUsage = "Config file" + ConfigDirFlag = "config-dir" + ConfigDirFlagUsage = "Config directory" + Verbose = "verbose" VerboseShorthand = "v" VerboseUsage = "Verbose output" diff --git a/cmd/frostfs-adm/internal/modules/root.go b/cmd/frostfs-adm/internal/modules/root.go index 4ac9a9b20..67584e921 100644 --- a/cmd/frostfs-adm/internal/modules/root.go +++ b/cmd/frostfs-adm/internal/modules/root.go @@ -9,6 +9,7 @@ import ( "github.com/TrueCloudLab/frostfs-node/cmd/frostfs-adm/internal/modules/storagecfg" "github.com/TrueCloudLab/frostfs-node/misc" "github.com/TrueCloudLab/frostfs-node/pkg/util/autocomplete" + utilConfig "github.com/TrueCloudLab/frostfs-node/pkg/util/config" "github.com/TrueCloudLab/frostfs-node/pkg/util/gendoc" "github.com/spf13/cobra" "github.com/spf13/viper" @@ -34,6 +35,7 @@ func init() { rootCmd.SetOut(os.Stdout) rootCmd.PersistentFlags().StringP(commonflags.ConfigFlag, commonflags.ConfigFlagShorthand, "", commonflags.ConfigFlagUsage) + rootCmd.PersistentFlags().String(commonflags.ConfigDirFlag, "", commonflags.ConfigDirFlagUsage) rootCmd.PersistentFlags().BoolP(commonflags.Verbose, commonflags.VerboseShorthand, false, commonflags.VerboseUsage) _ = viper.BindPFlag(commonflags.Verbose, rootCmd.PersistentFlags().Lookup(commonflags.Verbose)) rootCmd.Flags().Bool("version", false, "Application version") @@ -62,11 +64,22 @@ func entryPoint(cmd *cobra.Command, args []string) error { func initConfig(cmd *cobra.Command) { configFile, err := cmd.Flags().GetString(commonflags.ConfigFlag) - if err != nil || configFile == "" { + if err != nil { return } - viper.SetConfigType("yml") - viper.SetConfigFile(configFile) - _ = viper.ReadInConfig() // if config file is set but unavailable, ignore it + if configFile != "" { + viper.SetConfigType("yml") + viper.SetConfigFile(configFile) + _ = viper.ReadInConfig() // if config file is set but unavailable, ignore it + } + + configDir, err := cmd.Flags().GetString(commonflags.ConfigDirFlag) + if err != nil { + return + } + + if configDir != "" { + _ = utilConfig.ReadConfigDir(viper.GetViper(), configDir) // if config files cannot be read, ignore it + } } diff --git a/config/example/README.md b/config/example/README.md index cbca83731..afb594b14 100644 --- a/config/example/README.md +++ b/config/example/README.md @@ -29,7 +29,8 @@ $ ./bin/frotsfs-node --config ./config/example/node.yaml --config-dir ./dir/with When the `--config-dir` flag set, the application: * reads all `*.y[a]ml` files from provided directory, * use Viper's [MergeConfig](https://pkg.go.dev/github.com/spf13/viper#MergeConfig) functionality to produce the final configuration, -* files are being processing in alphanumerical order so that `01.yaml` may be extended with contents of `02.yaml`. +* files are being processing in alphanumerical order so that `01.yaml` may be extended with contents of `02.yaml`, so +if a field is specified in multiple files, the latest occurrence takes effect. So if we have the following files: ```yaml