Merge pull request #573 from linhvo/33-validate-config

33 validate config
This commit is contained in:
Linh Vo 2017-05-25 12:56:37 -05:00 committed by GitHub
commit 37fa3973ec
2 changed files with 36 additions and 1 deletions

View file

@ -109,6 +109,11 @@ func setAllConfig(v *viper.Viper, flags *pflag.FlagSet, envPrefix string) error
v.AutomaticEnv()
c := v.GetString("config")
var flagErr error
validTags := make(map[string]bool)
flags.VisitAll(func(f *pflag.Flag) {
validTags[f.Name] = true
})
// add config file to viper
if c != "" {
@ -118,10 +123,16 @@ func setAllConfig(v *viper.Viper, flags *pflag.FlagSet, envPrefix string) error
if err != nil {
return fmt.Errorf("error reading configuration file '%s': %v", c, err)
}
for _, key := range v.AllKeys() {
if _, ok := validTags[key]; !ok {
return fmt.Errorf("invalid option in configuration file: %v", key)
}
}
}
// set all values from viper
var flagErr error
flags.VisitAll(func(f *pflag.Flag) {
if flagErr != nil {
return

View file

@ -171,3 +171,27 @@ func TestRootCommand(t *testing.T) {
t.Fatalf("Expected standard usage message from RootCommand, but err: '%v', output: '%s'", err, outStr)
}
}
func TestRootCommand_Config(t *testing.T) {
file, err := ioutil.TempFile("", "test.conf")
if err != nil {
panic(err)
}
config := `data-dir = "/tmp/pil5_0"
bind = "127.0.0.1:10101"
[cluster]
poll-interval = "2m0s"
replicas = 2
partitions = 128
hosts = [
"127.0.0.1:10101",
"127.0.0.1:10111",
]`
file.Write([]byte(config))
file.Close()
_, err = ExecNewRootCommand(t, "server", "--config", file.Name())
if err.Error() != "invalid option in configuration file: cluster.partitions" {
t.Fatalf("Expected invalid option in configuration file, but err: '%v'", err)
}
}