diff options
-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) |