Enforce config fields, fail on unknown keys (#1051)

If/when users have a typo or misindentation in their config file, we
would have silently failed to assign the value and moved on.

With this change it'll error out, alerting user of the problem.
This commit is contained in:
David Zhao
2022-09-28 22:13:52 -07:00
committed by GitHub
parent d70843bc5a
commit 9c8d9fca0c
2 changed files with 12 additions and 1 deletions
+4 -1
View File
@@ -3,6 +3,7 @@ package config
import (
"fmt"
"os"
"strings"
"time"
"github.com/mitchellh/go-homedir"
@@ -258,7 +259,9 @@ func NewConfig(confString string, c *cli.Context) (*Config, error) {
Keys: map[string]string{},
}
if confString != "" {
if err := yaml.Unmarshal([]byte(confString), conf); err != nil {
decoder := yaml.NewDecoder(strings.NewReader(confString))
decoder.KnownFields(true)
if err := decoder.Decode(conf); err != nil {
return nil, fmt.Errorf("could not parse config: %v", err)
}
}
+8
View File
@@ -22,3 +22,11 @@ func TestConfig_DefaultsKept(t *testing.T) {
require.Equal(t, true, conf.Room.AutoCreate)
require.Equal(t, uint32(10), conf.Room.EmptyTimeout)
}
func TestConfig_UnknownKeys(t *testing.T) {
const content = `unknown: 10
room:
empty_timeout: 10`
_, err := NewConfig(content, nil)
require.Error(t, err)
}