Config parsing: fail on unknown fields and print them

Useful against config validation or runtime failures
caused by wrong field spelling or YAML indentation.
This commit is contained in:
Alexander A. Klimov 2023-06-19 17:38:52 +02:00
parent 4ed4db3ab6
commit a163694dc6
2 changed files with 2 additions and 2 deletions

View file

@ -125,7 +125,7 @@ func parseConfig(f *Flags) (_ *Config, exit int) {
return nil, 2
}
if err := yaml.NewDecoder(cf).Decode(c); err != nil {
if err := yaml.NewDecoder(cf, yaml.DisallowUnknownField()).Decode(c); err != nil {
_, _ = fmt.Fprintf(os.Stderr, "can't parse config file: %s\n", err.Error())
return nil, 2
}

View file

@ -54,7 +54,7 @@ func FromYAMLFile(name string) (*Config, error) {
defer f.Close()
c := &Config{}
d := yaml.NewDecoder(f)
d := yaml.NewDecoder(f, yaml.DisallowUnknownField())
if err := defaults.Set(c); err != nil {
return nil, errors.Wrap(err, "can't set config defaults")