Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[*] refactor NewConfig() #278

Merged
merged 1 commit into from
Nov 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 51 additions & 55 deletions vipconfig/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Loading