From 99e0fdd5ad572a243b715260c313abdb33a991b8 Mon Sep 17 00:00:00 2001 From: xengineering Date: Mon, 25 Mar 2024 19:36:44 +0100 Subject: Remove mustRead() This function just increases function nesting and makes the code less readable. --- main.go | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/main.go b/main.go index f3ec517..4ac41e8 100644 --- a/main.go +++ b/main.go @@ -55,14 +55,17 @@ func parseFlags() string { // parseConfig() parses and validates the runtime configuration file and // returns it as Go datastructure. func parseConfig(path string) RuntimeConfig { + data, err := os.ReadFile(path) + if err != nil { + log.Fatalf("Could not read '%s'!", path) + } - data := mustRead(path) if !json.Valid(data) { log.Fatalf("%s contains invalid JSON!", path) } config := RuntimeConfig{} - err := json.Unmarshal(data, &config) + err = json.Unmarshal(data, &config) if err != nil { log.Fatalf("Could not parse configuration file:\n%s\n", err) } @@ -86,20 +89,14 @@ func index(devices DevicesConfig) func(http.ResponseWriter, *http.Request) { } } -// mustRead() reads a file and panics if this is not possible. -func mustRead(path string) []byte { - data, err := os.ReadFile(path) - if err != nil { - log.Fatalf("Could not read '%s'!", path) - } - return data -} - // mustRender() renders a template file with the given data and panics if this // is not possible. func mustRender(filepath string, data interface{}) string { + file, err := os.ReadFile(filepath) + if err != nil { + log.Fatalf("Could not read '%s'!", filepath) + } - file := mustRead(filepath) tmpl, err := template.New(filepath).Parse(string(file)) var buffer bytes.Buffer err = tmpl.Execute(&buffer, data) -- cgit v1.2.3-70-g09d2