mirror of
https://github.com/spf13/viper
synced 2024-11-14 09:07:01 +00:00
return more useful error if config file is missing
Signed-off-by: Joshua Rubin <jrubin@zvelo.com>
This commit is contained in:
parent
25817ada59
commit
1a2e68e1bc
1 changed files with 17 additions and 9 deletions
24
viper.go
24
viper.go
|
@ -197,16 +197,25 @@ func Set(key string, value interface{}) {
|
||||||
override[key] = value
|
override[key] = value
|
||||||
}
|
}
|
||||||
|
|
||||||
func ReadInConfig() {
|
type UnsupportedConfigError string
|
||||||
|
|
||||||
|
func (str UnsupportedConfigError) Error() string {
|
||||||
|
return fmt.Sprintf("Unsupported Config Type %q", string(str))
|
||||||
|
}
|
||||||
|
|
||||||
|
func ReadInConfig() error {
|
||||||
jww.INFO.Println("Attempting to read in config file")
|
jww.INFO.Println("Attempting to read in config file")
|
||||||
if !stringInSlice(getConfigType(), SupportedExts) {
|
if !stringInSlice(getConfigType(), SupportedExts) {
|
||||||
jww.ERROR.Fatalf("Unsupported Config Type %q", getConfigType())
|
return UnsupportedConfigError(getConfigType())
|
||||||
}
|
}
|
||||||
|
|
||||||
file, err := ioutil.ReadFile(getConfigFile())
|
file, err := ioutil.ReadFile(getConfigFile())
|
||||||
if err == nil {
|
if err != nil {
|
||||||
MarshallReader(bytes.NewReader(file))
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
MarshallReader(bytes.NewReader(file))
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func MarshallReader(in io.Reader) {
|
func MarshallReader(in io.Reader) {
|
||||||
|
@ -276,13 +285,12 @@ func getConfigFile() string {
|
||||||
|
|
||||||
cf, err := findConfigFile()
|
cf, err := findConfigFile()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
jww.ERROR.Println(err)
|
return ""
|
||||||
} else {
|
}
|
||||||
|
|
||||||
configFile = cf
|
configFile = cf
|
||||||
return getConfigFile()
|
return getConfigFile()
|
||||||
}
|
}
|
||||||
return ""
|
|
||||||
}
|
|
||||||
|
|
||||||
func searchInPath(in string) (filename string) {
|
func searchInPath(in string) (filename string) {
|
||||||
jww.DEBUG.Println("Searching for config in ", in)
|
jww.DEBUG.Println("Searching for config in ", in)
|
||||||
|
|
Loading…
Reference in a new issue