mirror of
https://github.com/spf13/viper
synced 2025-05-06 20:27:17 +00:00
Allow errors to propagate from getConfigFile().
When getConfigFile() fails to find a config file, creating a ConfigFileNotFoundError, propagate the error information rather than just setting filenames and config types to the empty string. This fixes the dreaded 'Unsupported Config Type ""' message that is seen when the user hasn't created a config file.
This commit is contained in:
parent
a212099cbe
commit
d3fc5f547c
2 changed files with 31 additions and 9 deletions
34
viper.go
34
viper.go
|
@ -238,7 +238,13 @@ func (v *Viper) WatchConfig() {
|
||||||
defer watcher.Close()
|
defer watcher.Close()
|
||||||
|
|
||||||
// we have to watch the entire directory to pick up renames/atomic saves in a cross-platform way
|
// 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)
|
configDir, _ := filepath.Split(configFile)
|
||||||
|
|
||||||
done := make(chan bool)
|
done := make(chan bool)
|
||||||
|
@ -896,11 +902,16 @@ func (v *Viper) Set(key string, value interface{}) {
|
||||||
func ReadInConfig() error { return v.ReadInConfig() }
|
func ReadInConfig() error { return v.ReadInConfig() }
|
||||||
func (v *Viper) ReadInConfig() error {
|
func (v *Viper) ReadInConfig() error {
|
||||||
jww.INFO.Println("Attempting to read in config file")
|
jww.INFO.Println("Attempting to read in config file")
|
||||||
|
filename, err := v.getConfigFile()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
if !stringInSlice(v.getConfigType(), SupportedExts) {
|
if !stringInSlice(v.getConfigType(), SupportedExts) {
|
||||||
return UnsupportedConfigError(v.getConfigType())
|
return UnsupportedConfigError(v.getConfigType())
|
||||||
}
|
}
|
||||||
|
|
||||||
file, err := ioutil.ReadFile(v.getConfigFile())
|
file, err := ioutil.ReadFile(filename)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -918,7 +929,12 @@ func (v *Viper) MergeInConfig() error {
|
||||||
return UnsupportedConfigError(v.getConfigType())
|
return UnsupportedConfigError(v.getConfigType())
|
||||||
}
|
}
|
||||||
|
|
||||||
file, err := ioutil.ReadFile(v.getConfigFile())
|
filename, err := v.getConfigFile()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
file, err := ioutil.ReadFile(filename)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -1191,7 +1207,11 @@ func (v *Viper) getConfigType() string {
|
||||||
return v.configType
|
return v.configType
|
||||||
}
|
}
|
||||||
|
|
||||||
cf := v.getConfigFile()
|
cf, err := v.getConfigFile()
|
||||||
|
if err != nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
ext := filepath.Ext(cf)
|
ext := filepath.Ext(cf)
|
||||||
|
|
||||||
if len(ext) > 1 {
|
if len(ext) > 1 {
|
||||||
|
@ -1201,15 +1221,15 @@ func (v *Viper) getConfigType() string {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v *Viper) getConfigFile() string {
|
func (v *Viper) getConfigFile() (string, error) {
|
||||||
// if explicitly set, then use it
|
// if explicitly set, then use it
|
||||||
if v.configFile != "" {
|
if v.configFile != "" {
|
||||||
return v.configFile
|
return v.configFile, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
cf, err := v.findConfigFile()
|
cf, err := v.findConfigFile()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return ""
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
v.configFile = cf
|
v.configFile = cf
|
||||||
|
|
|
@ -225,7 +225,9 @@ func (s *stringValue) String() string {
|
||||||
|
|
||||||
func TestBasics(t *testing.T) {
|
func TestBasics(t *testing.T) {
|
||||||
SetConfigFile("/tmp/config.yaml")
|
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) {
|
func TestDefault(t *testing.T) {
|
||||||
|
@ -718,7 +720,7 @@ func TestWrongDirsSearchNotFound(t *testing.T) {
|
||||||
v.AddConfigPath(`thispathaintthere`)
|
v.AddConfigPath(`thispathaintthere`)
|
||||||
|
|
||||||
err := v.ReadInConfig()
|
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
|
// Even though config did not load and the error might have
|
||||||
// been ignored by the client, the default still loads
|
// been ignored by the client, the default still loads
|
||||||
|
|
Loading…
Add table
Reference in a new issue