Strict decoding of config file

Abort on unrecognized options or if general.domain is missing

Fixes #292
This commit is contained in:
Brian Candler 2022-02-22 09:37:33 +00:00
parent a33c09accf
commit 621c8c701c

View File

@ -32,16 +32,23 @@ func fileIsAccessible(fname string) bool {
func readConfig(fname string) (DNSConfig, error) {
var conf DNSConfig
_, err := toml.DecodeFile(fname, &conf)
md, err := toml.DecodeFile(fname, &conf)
if err != nil {
// Return with config file parsing errors from toml package
return conf, err
}
undecoded := md.Undecoded()
if len(undecoded) > 0 {
return conf, fmt.Errorf("Unexpected keys: %v", undecoded)
}
return prepareConfig(conf)
}
// prepareConfig checks that mandatory values exist, and can be used to set default values in the future
func prepareConfig(conf DNSConfig) (DNSConfig, error) {
if conf.General.Domain == "" {
return conf, errors.New("missing general configuration option \"domain\"")
}
if conf.Database.Engine == "" {
return conf, errors.New("missing database configuration option \"engine\"")
}