From ccca4d111e956c3c26067d0b6bc6c14e53ef6d69 Mon Sep 17 00:00:00 2001 From: Jasper Teunissen Date: Fri, 17 Aug 2018 00:33:33 +0200 Subject: [PATCH 1/2] Fix error not being returned in LoadConfig() When there was an error with the os.Stat call, we would return nil. This is because a new variable was created instead of the named return value assigned and returning nil explicitly instead of relying on the named returns. --- engine/engine.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/engine/engine.go b/engine/engine.go index 8fa2bab..ccc2542 100644 --- a/engine/engine.go +++ b/engine/engine.go @@ -62,7 +62,7 @@ func GoFileWalk(rootPath string, includePaths []string) (fileList []string, err func LoadConfig() (config map[string]interface{}, err error) { var parsedConfig map[string]interface{} - if _, err := os.Stat("/config.json"); err == nil { + if _, err = os.Stat("/config.json"); err == nil { data, err := ioutil.ReadFile("/config.json") if err != nil { return nil, err @@ -74,7 +74,7 @@ func LoadConfig() (config map[string]interface{}, err error) { } } - return parsedConfig, nil + return parsedConfig, err } func IncludePaths(rootPath string, config map[string]interface{}) []string { From 660acc6c8d5f282012c01c05bdbd1eabb3ccf8ea Mon Sep 17 00:00:00 2001 From: Jasper Teunissen Date: Fri, 17 Aug 2018 23:02:32 +0200 Subject: [PATCH 2/2] Rewrote LoadConfig() to have less nested code I think the method looks cleaner when you have less nested code blocks. This way your brain has to do less thinking, because when no errors occur you just have to follow the leftmost indented code to see how it works. Only in unusual situations where errors occur you have to start reading nested code. --- engine/engine.go | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/engine/engine.go b/engine/engine.go index ccc2542..a60ff66 100644 --- a/engine/engine.go +++ b/engine/engine.go @@ -62,19 +62,21 @@ func GoFileWalk(rootPath string, includePaths []string) (fileList []string, err func LoadConfig() (config map[string]interface{}, err error) { var parsedConfig map[string]interface{} - if _, err = os.Stat("/config.json"); err == nil { - data, err := ioutil.ReadFile("/config.json") - if err != nil { - return nil, err - } - err = json.Unmarshal(data, &parsedConfig) + if _, err := os.Stat("/config.json"); err != nil { + return nil, err + } - if err != nil { - return nil, err - } + data, err := ioutil.ReadFile("/config.json") + if err != nil { + return nil, err + } + err = json.Unmarshal(data, &parsedConfig) + + if err != nil { + return nil, err } - return parsedConfig, err + return parsedConfig, nil } func IncludePaths(rootPath string, config map[string]interface{}) []string {