Skip to content

Commit

Permalink
feat(config): validate config (#49)
Browse files Browse the repository at this point in the history
* feat: improved error handling

* added missing os.Exit

* added type and apikey to check

* refactor
  • Loading branch information
s0up4200 authored May 1, 2023
1 parent 33b51c4 commit fe135bd
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions internal/domain/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,15 @@ func (c *Config) defaults() {

var k = koanf.New(".")

func validateConfig(condition bool, field, entityType, entityName string) {
if condition {
log.Fatal().
Str("service", "config").
Msgf("%s not set for %s: %s", field, entityType, entityName)
os.Exit(1)
}
}

func NewConfig(configPath string) *Config {
cfg := &Config{}

Expand Down Expand Up @@ -125,6 +134,20 @@ func NewConfig(configPath string) *Config {
Str("service", "config").
Msgf("failed unmarshalling %q", configPath)
}

for _, list := range cfg.Lists {
validateConfig(len(list.Filters) < 1, "Filters", "list", list.Name)
validateConfig(list.URL == "", "URL", "list", list.Name)
validateConfig(list.Type == "", "Type", "list", list.Name)
}

for _, arr := range cfg.Clients.Arr {
validateConfig(len(arr.Filters) < 1, "Filters", "arr", arr.Name)
validateConfig(arr.Host == "", "Host", "arr", arr.Name)
validateConfig(arr.Apikey == "", "API", "arr", arr.Name)
validateConfig(arr.Type == "", "Type", "arr", arr.Name)
}

}

return cfg
Expand Down

0 comments on commit fe135bd

Please sign in to comment.