diff options
author | xengineering <me@xengineering.eu> | 2024-03-25 19:36:44 +0100 |
---|---|---|
committer | xengineering <me@xengineering.eu> | 2024-03-25 19:36:44 +0100 |
commit | 99e0fdd5ad572a243b715260c313abdb33a991b8 (patch) | |
tree | 46962a7afe7ff08050c98597cf39b324990f125f | |
parent | d81a1f40c08acac2f9afe8e80b2c69bbf321d3d6 (diff) | |
download | webiot-99e0fdd5ad572a243b715260c313abdb33a991b8.tar webiot-99e0fdd5ad572a243b715260c313abdb33a991b8.tar.zst webiot-99e0fdd5ad572a243b715260c313abdb33a991b8.zip |
Remove mustRead()
This function just increases function nesting and makes the code less
readable.
-rw-r--r-- | main.go | 21 |
1 files changed, 9 insertions, 12 deletions
@@ -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) |