From 73f43714185df41cd3945d9e4f9b6f5ab578ea97 Mon Sep 17 00:00:00 2001 From: Joshua Rubin Date: Tue, 8 Apr 2014 16:57:45 -0600 Subject: [PATCH] return more useful error if config file is missing Signed-off-by: Joshua Rubin --- viper.go | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/viper.go b/viper.go index b53c26a..01471d9 100644 --- a/viper.go +++ b/viper.go @@ -197,16 +197,25 @@ func Set(key string, value interface{}) { 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") if !stringInSlice(getConfigType(), SupportedExts) { - jww.ERROR.Fatalf("Unsupported Config Type %q", getConfigType()) + return UnsupportedConfigError(getConfigType()) } file, err := ioutil.ReadFile(getConfigFile()) - if err == nil { - MarshallReader(bytes.NewReader(file)) + if err != nil { + return err } + + MarshallReader(bytes.NewReader(file)) + return nil } func MarshallReader(in io.Reader) { @@ -276,12 +285,11 @@ func getConfigFile() string { cf, err := findConfigFile() if err != nil { - jww.ERROR.Println(err) - } else { - configFile = cf - return getConfigFile() + return "" } - return "" + + configFile = cf + return getConfigFile() } func searchInPath(in string) (filename string) {