about summary refs log tree commit diff
path: root/tools/govanity
diff options
context:
space:
mode:
authorFranck Cuny <franck@fcuny.net>2022-06-15 17:44:33 -0700
committerFranck Cuny <franck@fcuny.net>2022-06-15 17:47:11 -0700
commit17f3ff9e062edcc85be9abb532e0b9f0f5c3f96a (patch)
tree3f450836024a671064a687c57e6a36ca227575fa /tools/govanity
parentfix(tools/govanity): add a test for the repo URL (diff)
downloadworld-17f3ff9e062edcc85be9abb532e0b9f0f5c3f96a.tar.gz
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 <franck@fcuny.net>
Diffstat (limited to 'tools/govanity')
-rw-r--r--tools/govanity/e2e_test.go13
-rw-r--r--tools/govanity/main.go27
2 files changed, 22 insertions, 18 deletions
diff --git a/tools/govanity/e2e_test.go b/tools/govanity/e2e_test.go
index 4ec299a..157bcc2 100644
--- a/tools/govanity/e2e_test.go
+++ b/tools/govanity/e2e_test.go
@@ -1,12 +1,9 @@
 package main
 
 import (
-	"io/ioutil"
 	"net"
 	"net/http"
 	"testing"
-
-	"gopkg.in/yaml.v3"
 )
 
 func TestConfigurationURL(t *testing.T) {
@@ -17,15 +14,9 @@ func TestConfigurationURL(t *testing.T) {
 		t.Skipf("no network connectivity: %v", err)
 	}
 
-	buf, err := ioutil.ReadFile("vanity.yaml")
-	if err != nil {
-		t.Fatalf("failed to read the configuration file: %v", err)
-	}
-
-	cfg := &config{}
-	err = yaml.Unmarshal(buf, cfg)
+	cfg, err := loadConfig()
 	if err != nil {
-		t.Fatalf("failed to parse the YAML configuration: %v", err)
+		t.Fatal(err)
 	}
 
 	for _, r := range cfg.Repositories {
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 {