From 3aedeaf4db5872a8eb187000423ed9a714b53639 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sandro=20J=C3=A4ckel?= Date: Mon, 24 Jun 2024 22:11:15 +0200 Subject: [PATCH] Set DisallowUnknownFields when config is checked --- cmds/houndd/main.go | 10 +++++----- config/config.go | 9 +++++++-- config/config_test.go | 2 +- 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/cmds/houndd/main.go b/cmds/houndd/main.go index 79e7f131..53b265b1 100644 --- a/cmds/houndd/main.go +++ b/cmds/houndd/main.go @@ -143,14 +143,14 @@ func main() { } var cfg config.Config - if err := cfg.LoadFromFile(*flagConf); err != nil { + if err := cfg.LoadFromFile(*flagConf, *flagCheckConf); err != nil { panic(err) } - if *flagCheckConf { - return - } - + if *flagCheckConf { + return + } + // Start the web server on a background routine. ws := web.Start(&cfg, *flagAddr, *flagDev) diff --git a/config/config.go b/config/config.go index c978c009..0bd8ebcf 100644 --- a/config/config.go +++ b/config/config.go @@ -188,14 +188,19 @@ func mergeVCSConfigs(cfg *Config) error { return nil } -func (c *Config) LoadFromFile(filename string) error { +func (c *Config) LoadFromFile(filename string, disallowUnknownFields bool) error { r, err := os.Open(filename) if err != nil { return err } defer r.Close() - if err := json.NewDecoder(r).Decode(c); err != nil { + decoder := json.NewDecoder(r) + if disallowUnknownFields { + decoder.DisallowUnknownFields() + } + + if decoder.Decode(c); err != nil { return err } diff --git a/config/config_test.go b/config/config_test.go index 5b3328af..dd788e0f 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -20,7 +20,7 @@ func rootDir() string { // add examples, we don't muck them up. func TestExampleConfigsAreValid(t *testing.T) { var cfg Config - if err := cfg.LoadFromFile(filepath.Join(rootDir(), exampleConfigFile)); err != nil { + if err := cfg.LoadFromFile(filepath.Join(rootDir(), exampleConfigFile), true); err != nil { t.Fatalf("Unable to parse %s: %s", exampleConfigFile, err) }