Skip to content

Commit

Permalink
Set DisallowUnknownFields when config is checked
Browse files Browse the repository at this point in the history
  • Loading branch information
SuperSandro2000 authored and MarcelCoding committed Jun 24, 2024
1 parent b2f1cef commit 3aedeaf
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 8 deletions.
10 changes: 5 additions & 5 deletions cmds/houndd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
9 changes: 7 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down

0 comments on commit 3aedeaf

Please sign in to comment.