[#44] adm: Support multiple configs

Signed-off-by: Denis Kirillov <d.kirillov@yadro.com>
This commit is contained in:
Denis Kirillov 2023-02-06 17:54:49 +03:00 committed by fyrchik
parent 5f06232d34
commit 633c5a35de
3 changed files with 22 additions and 5 deletions

View file

@ -5,6 +5,9 @@ const (
ConfigFlagShorthand = "c" ConfigFlagShorthand = "c"
ConfigFlagUsage = "Config file" ConfigFlagUsage = "Config file"
ConfigDirFlag = "config-dir"
ConfigDirFlagUsage = "Config directory"
Verbose = "verbose" Verbose = "verbose"
VerboseShorthand = "v" VerboseShorthand = "v"
VerboseUsage = "Verbose output" VerboseUsage = "Verbose output"

View file

@ -9,6 +9,7 @@ import (
"github.com/TrueCloudLab/frostfs-node/cmd/frostfs-adm/internal/modules/storagecfg" "github.com/TrueCloudLab/frostfs-node/cmd/frostfs-adm/internal/modules/storagecfg"
"github.com/TrueCloudLab/frostfs-node/misc" "github.com/TrueCloudLab/frostfs-node/misc"
"github.com/TrueCloudLab/frostfs-node/pkg/util/autocomplete" "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/TrueCloudLab/frostfs-node/pkg/util/gendoc"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper" "github.com/spf13/viper"
@ -34,6 +35,7 @@ func init() {
rootCmd.SetOut(os.Stdout) rootCmd.SetOut(os.Stdout)
rootCmd.PersistentFlags().StringP(commonflags.ConfigFlag, commonflags.ConfigFlagShorthand, "", commonflags.ConfigFlagUsage) 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) rootCmd.PersistentFlags().BoolP(commonflags.Verbose, commonflags.VerboseShorthand, false, commonflags.VerboseUsage)
_ = viper.BindPFlag(commonflags.Verbose, rootCmd.PersistentFlags().Lookup(commonflags.Verbose)) _ = viper.BindPFlag(commonflags.Verbose, rootCmd.PersistentFlags().Lookup(commonflags.Verbose))
rootCmd.Flags().Bool("version", false, "Application version") rootCmd.Flags().Bool("version", false, "Application version")
@ -62,11 +64,22 @@ func entryPoint(cmd *cobra.Command, args []string) error {
func initConfig(cmd *cobra.Command) { func initConfig(cmd *cobra.Command) {
configFile, err := cmd.Flags().GetString(commonflags.ConfigFlag) configFile, err := cmd.Flags().GetString(commonflags.ConfigFlag)
if err != nil || configFile == "" { if err != nil {
return return
} }
if configFile != "" {
viper.SetConfigType("yml") viper.SetConfigType("yml")
viper.SetConfigFile(configFile) viper.SetConfigFile(configFile)
_ = viper.ReadInConfig() // if config file is set but unavailable, ignore it _ = 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
}
}

View file

@ -29,7 +29,8 @@ $ ./bin/frotsfs-node --config ./config/example/node.yaml --config-dir ./dir/with
When the `--config-dir` flag set, the application: When the `--config-dir` flag set, the application:
* reads all `*.y[a]ml` files from provided directory, * 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, * 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: So if we have the following files:
```yaml ```yaml