Meaningful error propagation when config not found

This commit is contained in:
Vlad Didenko 2015-05-23 18:52:19 -05:00
parent 9853229d98
commit 7fe5335130
2 changed files with 51 additions and 29 deletions

View file

@ -694,33 +694,42 @@ 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")
if !stringInSlice(v.getConfigType(), SupportedExts) {
return UnsupportedConfigError(v.getConfigType()) cfgType, err := v.getConfigType()
if err != nil {
return err
} }
file, err := ioutil.ReadFile(v.getConfigFile()) if !stringInSlice(cfgType, SupportedExts) {
return UnsupportedConfigError(cfgType)
}
fname, err := v.getConfigFile()
if err != nil {
return err
}
file, err := ioutil.ReadFile(fname)
if err != nil { if err != nil {
return err return err
} }
v.config = make(map[string]interface{}) v.config = make(map[string]interface{})
v.marshalReader(bytes.NewReader(file), v.config) return v.marshalReader(bytes.NewReader(file), v.config)
return nil
} }
func ReadConfig(in io.Reader) error { return v.ReadConfig(in) } func ReadConfig(in io.Reader) error { return v.ReadConfig(in) }
func (v *Viper) ReadConfig(in io.Reader) error { func (v *Viper) ReadConfig(in io.Reader) error {
v.config = make(map[string]interface{}) v.config = make(map[string]interface{})
v.marshalReader(in, v.config) return v.marshalReader(in, v.config)
return nil
} }
// func ReadBufConfig(buf *bytes.Buffer) error { return v.ReadBufConfig(buf) } // func ReadBufConfig(buf *bytes.Buffer) error { return v.ReadBufConfig(buf) }
// func (v *Viper) ReadBufConfig(buf *bytes.Buffer) error { // func (v *Viper) ReadBufConfig(buf *bytes.Buffer) error {
// v.config = make(map[string]interface{}) // v.config = make(map[string]interface{})
// v.marshalReader(buf, v.config) // return v.marshalReader(buf, v.config)
// return nil
// } // }
// Attempts to get configuration from a remote source // Attempts to get configuration from a remote source
@ -745,9 +754,17 @@ func (v *Viper) WatchRemoteConfig() error {
// Marshall a Reader into a map // Marshall a Reader into a map
// Should probably be an unexported function // Should probably be an unexported function
func marshalReader(in io.Reader, c map[string]interface{}) { v.marshalReader(in, c) } func marshalReader(in io.Reader, c map[string]interface{}) error {
func (v *Viper) marshalReader(in io.Reader, c map[string]interface{}) { return v.marshalReader(in, c)
marshallConfigReader(in, c, v.getConfigType()) }
func (v *Viper) marshalReader(in io.Reader, c map[string]interface{}) error {
cfgType, err := v.getConfigType()
if err != nil {
return err
}
marshallConfigReader(in, c, cfgType)
return nil
} }
func (v *Viper) insensitiviseMaps() { func (v *Viper) insensitiviseMaps() {
@ -800,7 +817,7 @@ func (v *Viper) getRemoteConfig(provider *remoteProvider) (map[string]interface{
return nil, err return nil, err
} }
reader := bytes.NewReader(b) reader := bytes.NewReader(b)
v.marshalReader(reader, v.kvstore) err = v.marshalReader(reader, v.kvstore)
return v.kvstore, err return v.kvstore, err
} }
@ -850,7 +867,7 @@ func (v *Viper) watchRemoteConfig(provider *remoteProvider) (map[string]interfac
} }
reader := bytes.NewReader(resp.Value) reader := bytes.NewReader(resp.Value)
v.marshalReader(reader, v.kvstore) err = v.marshalReader(reader, v.kvstore)
return v.kvstore, err return v.kvstore, err
} }
@ -912,34 +929,35 @@ func (v *Viper) SetConfigType(in string) {
} }
} }
func (v *Viper) getConfigType() string { func (v *Viper) getConfigType() (string, error) {
if v.configType != "" { if v.configType != "" {
return v.configType return v.configType, nil
}
cf, err := v.getConfigFile()
if err != nil {
return "", err
} }
cf := v.getConfigFile()
ext := filepath.Ext(cf) ext := filepath.Ext(cf)
if len(ext) > 1 { if len(ext) > 1 {
return ext[1:] return ext[1:], nil
} else { } else {
return "" return "", fmt.Errorf("Missing config file extension in %q", cf)
} }
} }
func (v *Viper) getConfigFile() string { func (v *Viper) getConfigFile() (string, error) {
var err 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() v.configFile, err = v.findConfigFile()
if err != nil { return v.configFile, err
return ""
}
v.configFile = cf
return v.getConfigFile()
} }
func (v *Viper) searchInPath(in string) (filename string) { func (v *Viper) searchInPath(in string) (filename string) {
@ -958,11 +976,13 @@ func (v *Viper) searchInPath(in string) (filename string) {
// Choose where to look for a config file: either // Choose where to look for a config file: either
// in provided directories or in the working directory // in provided directories or in the working directory
func (v *Viper) findConfigFile() (string, error) { func (v *Viper) findConfigFile() (string, error) {
if len(v.configPaths) > 0 { if len(v.configPaths) > 0 {
return v.findConfigInPaths() return v.findConfigInPaths()
} else { } else {
return v.findConfigInWD() return v.findConfigInWD()
} }
} }
// Search all configPaths for any config file. // Search all configPaths for any config file.

View file

@ -147,7 +147,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()) cf, err := v.getConfigFile()
assert.Equal(t, nil, err)
assert.Equal(t, "/tmp/config.yaml", cf)
} }
func TestDefault(t *testing.T) { func TestDefault(t *testing.T) {