From 17f3ff9e062edcc85be9abb532e0b9f0f5c3f96a Mon Sep 17 00:00:00 2001 From: Franck Cuny Date: Wed, 15 Jun 2022 17:44:33 -0700 Subject: ref(tools/govanity): add a function to load the configuration Change-Id: I36c6da7eba64e6f877d1a17c700c56a70434625a Reviewed-on: https://cl.fcuny.net/c/world/+/422 Tested-by: CI Reviewed-by: Franck Cuny --- tools/govanity/main.go | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) (limited to 'tools/govanity/main.go') diff --git a/tools/govanity/main.go b/tools/govanity/main.go index dd6d653..d74db9b 100644 --- a/tools/govanity/main.go +++ b/tools/govanity/main.go @@ -4,6 +4,7 @@ import ( "bytes" "embed" "flag" + "fmt" "html/template" "io/ioutil" "log" @@ -36,15 +37,10 @@ type moduleTmpl struct { func main() { flag.Parse() - buf, err := ioutil.ReadFile("vanity.yaml") - if err != nil { - log.Fatalf("failed to read the configuration: %+v", err) - } - cfg := &config{} - err = yaml.Unmarshal(buf, cfg) + cfg, err := loadConfig() if err != nil { - log.Fatalf("failed to parse the YAML configuration: %+v", err) + log.Fatal(err) } http.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) { @@ -58,6 +54,23 @@ func main() { log.Fatal(http.ListenAndServe(":8080", nil)) } +func loadConfig() (*config, error) { + configPath := "./vanity.yaml" + + configYaml, err := ioutil.ReadFile(configPath) + if err != nil { + return nil, fmt.Errorf("failed to read configuration file %s: %v", configPath, err) + } + + var cfg config + err = yaml.Unmarshal(configYaml, &cfg) + if err != nil { + return nil, fmt.Errorf("failed to unmarshall configuration: %v", err) + } + + return &cfg, nil +} + func goGet(cfg *config) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { if r.Method != http.MethodGet { -- cgit 1.4.1