e2e: fix full path support when reading viper config file

Something changed in Viper such that it now returns the
ConfigFileNotFound error when the config file is not found, for
example when it is specified including the .yaml or .json suffix.
When the code was originally was written, it returned "Unsupported
Config Type".

Found when adding a unit test for this code (separate commit because
it depends on the flag changes).
This commit is contained in:
Patrick Ohly 2019-07-04 14:18:08 +02:00
parent 7340b6341a
commit 3630cfca2b

View File

@ -25,10 +25,6 @@ import (
"github.com/spf13/viper"
)
const (
viperFileNotFound = "Unsupported Config Type \"\""
)
// ViperizeFlags checks whether a configuration file was specified, reads it, and updates
// the configuration variables accordingly. Must be called after framework.HandleFlags()
// and before framework.AfterReadingAllFlags().
@ -68,7 +64,7 @@ func ViperizeFlags(requiredConfig, optionalConfig string) error {
// of file suffices. Therefore try once more without
// suffix.
ext := filepath.Ext(viperConfig)
if ext != "" && err.Error() == viperFileNotFound {
if _, ok := err.(viper.ConfigFileNotFoundError); ok && ext != "" {
viper.SetConfigName(filepath.Base(viperConfig[0 : len(viperConfig)-len(ext)]))
err = viper.ReadInConfig()
}