From 03b27f185959db77445dc891cb668a0576cf1a04 Mon Sep 17 00:00:00 2001 From: Pavlo Golub Date: Fri, 15 Nov 2024 15:08:04 +0100 Subject: [PATCH] refactor NewConfig() --- vipconfig/config.go | 106 +++++++++++++++++++++----------------------- 1 file changed, 51 insertions(+), 55 deletions(-) diff --git a/vipconfig/config.go b/vipconfig/config.go index 6d2aeb0..6c1c93e 100644 --- a/vipconfig/config.go +++ b/vipconfig/config.go @@ -166,6 +166,40 @@ func setDefaults() { viper.SetDefault(k, v) } } + + // apply defaults for endpoints + if !viper.IsSet("dcs-endpoints") { + fmt.Println("No dcs-endpoints specified, trying to use localhost with standard ports!") + switch viper.GetString("dcs-type") { + case "consul": + viper.Set("dcs-endpoints", []string{"http://127.0.0.1:8500"}) + case "etcd", "etcd3": + viper.Set("dcs-endpoints", []string{"http://127.0.0.1:2379"}) + case "patroni": + viper.Set("dcs-endpoints", []string{"http://127.0.0.1:8008/"}) + } + } + + // set trigger-key to '/leader' if DCS type is patroni and nothing is specified + if viper.GetString("trigger-key") == "" && viper.GetString("dcs-type") == "patroni" { + viper.Set("trigger-key", "/leader") + } + + // set trigger-value to default value if nothing is specified + if triggerValue := viper.GetString("trigger-value"); triggerValue == "" { + var err error + if viper.GetString("dcs-type") == "patroni" { + triggerValue = "200" + } else { + triggerValue, err = os.Hostname() + } + if err != nil { + fmt.Printf("No trigger-value specified, hostname could not be retrieved: %s", err) + } else { + fmt.Printf("No trigger-value specified, instead using: %v", triggerValue) + viper.Set("trigger-value", triggerValue) + } + } } func checkSetting(name string) bool { @@ -192,7 +226,7 @@ func checkMandatory() error { if !success { return errors.New("one or more mandatory settings were not set") } - return nil + return checkImpliedMandatory() } // if reason is set, but implied is not set, return false. @@ -245,6 +279,17 @@ func printSettings() { } } +func loadConfigFile() error { + if viper.IsSet("config") { + viper.SetConfigFile(viper.GetString("config")) + if err := viper.ReadInConfig(); err != nil { + return err + } + fmt.Printf("Using config from file: %s\n", viper.ConfigFileUsed()) + } + return mapDeprecated() +} + // NewConfig returns a new Config instance func NewConfig() (*Config, error) { var err error @@ -272,74 +317,25 @@ func NewConfig() (*Config, error) { // - default // if a configfile has been passed, make viper read it - if viper.IsSet("config") { - viper.SetConfigFile(viper.GetString("config")) - - err := viper.ReadInConfig() // Find and read the config file - if err != nil { // Handle errors reading the config file - return nil, fmt.Errorf("Fatal error reading config file: %w", err) - } - fmt.Printf("Using config from file: %s\n", viper.ConfigFileUsed()) - } - - if err = mapDeprecated(); err != nil { - return nil, err + if err = loadConfigFile(); err != nil { + return nil, fmt.Errorf("Fatal error reading config file: %w", err) } - setDefaults() - // convert string of csv to String Slice - if viper.IsSet("dcs-endpoints") { - endpointsString := viper.GetString("dcs-endpoints") - if strings.Contains(endpointsString, ",") { - viper.Set("dcs-endpoints", strings.Split(endpointsString, ",")) - } - } - - // apply defaults for endpoints - if !viper.IsSet("dcs-endpoints") { - fmt.Println("No dcs-endpoints specified, trying to use localhost with standard ports!") - - switch viper.GetString("dcs-type") { - case "consul": - viper.Set("dcs-endpoints", []string{"http://127.0.0.1:8500"}) - case "etcd", "etcd3": - viper.Set("dcs-endpoints", []string{"http://127.0.0.1:2379"}) - case "patroni": - viper.Set("dcs-endpoints", []string{"http://127.0.0.1:8008/"}) - } + if endpointsString := viper.GetString("dcs-endpoints"); endpointsString != "" && strings.Contains(endpointsString, ",") { + viper.Set("dcs-endpoints", strings.Split(endpointsString, ",")) } - - // set trigger-value to default value if nothing is specified - if triggerValue := viper.GetString("trigger-value"); len(triggerValue) == 0 { - if viper.GetString("dcs-type") == "patroni" { - triggerValue = "200" - } else { - triggerValue, err = os.Hostname() - } - if err != nil { - fmt.Printf("No trigger-value specified, hostname could not be retrieved: %s", err) - } else { - fmt.Printf("No trigger-value specified, instead using: %v", triggerValue) - viper.Set("trigger-value", triggerValue) - } - } - + setDefaults() if err = checkMandatory(); err != nil { return nil, err } - if err = checkImpliedMandatory(); err != nil { - return nil, err - } - conf := &Config{} if err = viper.Unmarshal(conf); err != nil { zap.L().Fatal("unable to decode viper config into config struct, %v", zap.Error(err)) } conf.initLogger() - printSettings() return conf, nil