mirror of https://github.com/spf13/viper.git
Allow errors to propagate from getConfigFile(). (#161)
- propagate ConfigFileNotFoundError instead of using unsupported config type error when config file is not found
This commit is contained in:
parent
c14ce6d43c
commit
44208030b3
34
viper.go
34
viper.go
|
@ -241,7 +241,13 @@ func (v *Viper) WatchConfig() {
|
|||
defer watcher.Close()
|
||||
|
||||
// we have to watch the entire directory to pick up renames/atomic saves in a cross-platform way
|
||||
configFile := filepath.Clean(v.getConfigFile())
|
||||
filename, err := v.getConfigFile()
|
||||
if err != nil {
|
||||
log.Println("error:", err)
|
||||
return
|
||||
}
|
||||
|
||||
configFile := filepath.Clean(filename)
|
||||
configDir, _ := filepath.Split(configFile)
|
||||
|
||||
done := make(chan bool)
|
||||
|
@ -1102,11 +1108,16 @@ func (v *Viper) Set(key string, value interface{}) {
|
|||
func ReadInConfig() error { return v.ReadInConfig() }
|
||||
func (v *Viper) ReadInConfig() error {
|
||||
jww.INFO.Println("Attempting to read in config file")
|
||||
filename, err := v.getConfigFile()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if !stringInSlice(v.getConfigType(), SupportedExts) {
|
||||
return UnsupportedConfigError(v.getConfigType())
|
||||
}
|
||||
|
||||
file, err := afero.ReadFile(v.fs, v.getConfigFile())
|
||||
file, err := afero.ReadFile(v.fs, filename)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -1124,7 +1135,12 @@ func (v *Viper) MergeInConfig() error {
|
|||
return UnsupportedConfigError(v.getConfigType())
|
||||
}
|
||||
|
||||
file, err := afero.ReadFile(v.fs, v.getConfigFile())
|
||||
filename, err := v.getConfigFile()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
file, err := afero.ReadFile(v.fs, filename)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -1460,7 +1476,11 @@ func (v *Viper) getConfigType() string {
|
|||
return v.configType
|
||||
}
|
||||
|
||||
cf := v.getConfigFile()
|
||||
cf, err := v.getConfigFile()
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
|
||||
ext := filepath.Ext(cf)
|
||||
|
||||
if len(ext) > 1 {
|
||||
|
@ -1470,15 +1490,15 @@ func (v *Viper) getConfigType() string {
|
|||
return ""
|
||||
}
|
||||
|
||||
func (v *Viper) getConfigFile() string {
|
||||
func (v *Viper) getConfigFile() (string, error) {
|
||||
// if explicitly set, then use it
|
||||
if v.configFile != "" {
|
||||
return v.configFile
|
||||
return v.configFile, nil
|
||||
}
|
||||
|
||||
cf, err := v.findConfigFile()
|
||||
if err != nil {
|
||||
return ""
|
||||
return "", err
|
||||
}
|
||||
|
||||
v.configFile = cf
|
||||
|
|
|
@ -243,7 +243,9 @@ func (s *stringValue) String() string {
|
|||
|
||||
func TestBasics(t *testing.T) {
|
||||
SetConfigFile("/tmp/config.yaml")
|
||||
assert.Equal(t, "/tmp/config.yaml", v.getConfigFile())
|
||||
filename, err := v.getConfigFile()
|
||||
assert.Equal(t, "/tmp/config.yaml", filename)
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
|
||||
func TestDefault(t *testing.T) {
|
||||
|
@ -745,7 +747,7 @@ func TestWrongDirsSearchNotFound(t *testing.T) {
|
|||
v.AddConfigPath(`thispathaintthere`)
|
||||
|
||||
err := v.ReadInConfig()
|
||||
assert.Equal(t, reflect.TypeOf(UnsupportedConfigError("")), reflect.TypeOf(err))
|
||||
assert.Equal(t, reflect.TypeOf(ConfigFileNotFoundError{"", ""}), reflect.TypeOf(err))
|
||||
|
||||
// Even though config did not load and the error might have
|
||||
// been ignored by the client, the default still loads
|
||||
|
@ -915,7 +917,11 @@ func TestUnmarshalingWithAliases(t *testing.T) {
|
|||
func TestSetConfigNameClearsFileCache(t *testing.T) {
|
||||
SetConfigFile("/tmp/config.yaml")
|
||||
SetConfigName("default")
|
||||
assert.Empty(t, v.getConfigFile())
|
||||
f, err := v.getConfigFile()
|
||||
if err == nil {
|
||||
t.Fatalf("config file cache should have been cleared")
|
||||
}
|
||||
assert.Empty(t, f)
|
||||
}
|
||||
|
||||
func TestShadowedNestedValue(t *testing.T) {
|
||||
|
|
Loading…
Reference in New Issue