git.iko.soy/typeable/oauth2-proxy
on master help sign in
Return an error when unknown options are found in the config file
commit00fed1a31fc21c97710d2ccebf251d213a1e9172 authorJoel Speed <joel.speed@hotmail.co.uk> dateWed 29 Apr 2020, 19:45 (6 years ago) parentc5be09c
2 files changed+15−1
pkg/apis/options/load.go+3−1 pkg/apis/options/load_test.go+12
pkg/apis/options/load.go+3−1
@@ -38,7 +38,9 @@ func Load(configFileName string, flagSet *pflag.FlagSet, into interface{}) error
3838 return fmt.Errorf("unable to register flags: %w", err)
3939 }
4040
41− err = v.Unmarshal(into, decodeFromCfgTag)
41+ // UnmarhsalExact will return an error if the config includes options that are
42+ // not mapped to felds of the into struct
43+ err = v.UnmarshalExact(into, decodeFromCfgTag)
4244 if err != nil {
4345 return fmt.Errorf("error unmarshalling config: %w", err)
4446 }
pkg/apis/options/load_test.go+12
@@ -283,6 +283,18 @@ var _ = Describe("Load", func() {
283283 unexported: "unexported",
284284 },
285285 }),
286+ Entry("with an unknown option in the config file", &testOptionsTableInput{
287+ configFile: []byte(`unknown_option="foo"`),
288+ flagSet: func() *pflag.FlagSet { return testOptionsFlagSet },
289+ expectedErr: fmt.Errorf("error unmarshalling config: 1 error(s) decoding:\n\n* '' has invalid keys: unknown_option"),
290+ // Viper will unmarshal before returning the error, so this is the default output
291+ expectedOutput: &TestOptions{
292+ StringOption: "default",
293+ Sub: TestOptionSubStruct{
294+ StringSliceOption: []string{"a", "b"},
295+ },
296+ },
297+ }),
286298 )
287299 })
288300})