Skip to content

Commit 03b27f1

Browse files
committed
refactor NewConfig()
1 parent 9b30051 commit 03b27f1

File tree

1 file changed

+51
-55
lines changed

1 file changed

+51
-55
lines changed

vipconfig/config.go

Lines changed: 51 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,40 @@ func setDefaults() {
166166
viper.SetDefault(k, v)
167167
}
168168
}
169+
170+
// apply defaults for endpoints
171+
if !viper.IsSet("dcs-endpoints") {
172+
fmt.Println("No dcs-endpoints specified, trying to use localhost with standard ports!")
173+
switch viper.GetString("dcs-type") {
174+
case "consul":
175+
viper.Set("dcs-endpoints", []string{"http://127.0.0.1:8500"})
176+
case "etcd", "etcd3":
177+
viper.Set("dcs-endpoints", []string{"http://127.0.0.1:2379"})
178+
case "patroni":
179+
viper.Set("dcs-endpoints", []string{"http://127.0.0.1:8008/"})
180+
}
181+
}
182+
183+
// set trigger-key to '/leader' if DCS type is patroni and nothing is specified
184+
if viper.GetString("trigger-key") == "" && viper.GetString("dcs-type") == "patroni" {
185+
viper.Set("trigger-key", "/leader")
186+
}
187+
188+
// set trigger-value to default value if nothing is specified
189+
if triggerValue := viper.GetString("trigger-value"); triggerValue == "" {
190+
var err error
191+
if viper.GetString("dcs-type") == "patroni" {
192+
triggerValue = "200"
193+
} else {
194+
triggerValue, err = os.Hostname()
195+
}
196+
if err != nil {
197+
fmt.Printf("No trigger-value specified, hostname could not be retrieved: %s", err)
198+
} else {
199+
fmt.Printf("No trigger-value specified, instead using: %v", triggerValue)
200+
viper.Set("trigger-value", triggerValue)
201+
}
202+
}
169203
}
170204

171205
func checkSetting(name string) bool {
@@ -192,7 +226,7 @@ func checkMandatory() error {
192226
if !success {
193227
return errors.New("one or more mandatory settings were not set")
194228
}
195-
return nil
229+
return checkImpliedMandatory()
196230
}
197231

198232
// if reason is set, but implied is not set, return false.
@@ -245,6 +279,17 @@ func printSettings() {
245279
}
246280
}
247281

282+
func loadConfigFile() error {
283+
if viper.IsSet("config") {
284+
viper.SetConfigFile(viper.GetString("config"))
285+
if err := viper.ReadInConfig(); err != nil {
286+
return err
287+
}
288+
fmt.Printf("Using config from file: %s\n", viper.ConfigFileUsed())
289+
}
290+
return mapDeprecated()
291+
}
292+
248293
// NewConfig returns a new Config instance
249294
func NewConfig() (*Config, error) {
250295
var err error
@@ -272,74 +317,25 @@ func NewConfig() (*Config, error) {
272317
// - default
273318

274319
// if a configfile has been passed, make viper read it
275-
if viper.IsSet("config") {
276-
viper.SetConfigFile(viper.GetString("config"))
277-
278-
err := viper.ReadInConfig() // Find and read the config file
279-
if err != nil { // Handle errors reading the config file
280-
return nil, fmt.Errorf("Fatal error reading config file: %w", err)
281-
}
282-
fmt.Printf("Using config from file: %s\n", viper.ConfigFileUsed())
283-
}
284-
285-
if err = mapDeprecated(); err != nil {
286-
return nil, err
320+
if err = loadConfigFile(); err != nil {
321+
return nil, fmt.Errorf("Fatal error reading config file: %w", err)
287322
}
288323

289-
setDefaults()
290-
291324
// convert string of csv to String Slice
292-
if viper.IsSet("dcs-endpoints") {
293-
endpointsString := viper.GetString("dcs-endpoints")
294-
if strings.Contains(endpointsString, ",") {
295-
viper.Set("dcs-endpoints", strings.Split(endpointsString, ","))
296-
}
297-
}
298-
299-
// apply defaults for endpoints
300-
if !viper.IsSet("dcs-endpoints") {
301-
fmt.Println("No dcs-endpoints specified, trying to use localhost with standard ports!")
302-
303-
switch viper.GetString("dcs-type") {
304-
case "consul":
305-
viper.Set("dcs-endpoints", []string{"http://127.0.0.1:8500"})
306-
case "etcd", "etcd3":
307-
viper.Set("dcs-endpoints", []string{"http://127.0.0.1:2379"})
308-
case "patroni":
309-
viper.Set("dcs-endpoints", []string{"http://127.0.0.1:8008/"})
310-
}
325+
if endpointsString := viper.GetString("dcs-endpoints"); endpointsString != "" && strings.Contains(endpointsString, ",") {
326+
viper.Set("dcs-endpoints", strings.Split(endpointsString, ","))
311327
}
312-
313-
// set trigger-value to default value if nothing is specified
314-
if triggerValue := viper.GetString("trigger-value"); len(triggerValue) == 0 {
315-
if viper.GetString("dcs-type") == "patroni" {
316-
triggerValue = "200"
317-
} else {
318-
triggerValue, err = os.Hostname()
319-
}
320-
if err != nil {
321-
fmt.Printf("No trigger-value specified, hostname could not be retrieved: %s", err)
322-
} else {
323-
fmt.Printf("No trigger-value specified, instead using: %v", triggerValue)
324-
viper.Set("trigger-value", triggerValue)
325-
}
326-
}
327-
328+
setDefaults()
328329
if err = checkMandatory(); err != nil {
329330
return nil, err
330331
}
331332

332-
if err = checkImpliedMandatory(); err != nil {
333-
return nil, err
334-
}
335-
336333
conf := &Config{}
337334
if err = viper.Unmarshal(conf); err != nil {
338335
zap.L().Fatal("unable to decode viper config into config struct, %v", zap.Error(err))
339336
}
340337

341338
conf.initLogger()
342-
343339
printSettings()
344340

345341
return conf, nil

0 commit comments

Comments
 (0)