about summary refs log tree commit diff stats
path: root/internal/config
diff options
context:
space:
mode:
authorAlan Pearce2024-05-22 18:03:22 +0200
committerAlan Pearce2024-05-22 18:03:22 +0200
commitea098ab11740fe4783694ff0122738b30e0c342f (patch)
treebcd50fdb6fc7ce9730c758f709aa6bf29a763ea9 /internal/config
parentaef028263229d8acda28b8e657413f7e9c187833 (diff)
downloadwebsite-ea098ab11740fe4783694ff0122738b30e0c342f.tar.lz
website-ea098ab11740fe4783694ff0122738b30e0c342f.tar.zst
website-ea098ab11740fe4783694ff0122738b30e0c342f.zip
refactor: lint with golangci-lint
Diffstat (limited to 'internal/config')
-rw-r--r--internal/config/config.go19
-rw-r--r--internal/config/cspgenerator.go28
2 files changed, 24 insertions, 23 deletions
diff --git a/internal/config/config.go b/internal/config/config.go
index be7dcb9..df69bce 100644
--- a/internal/config/config.go
+++ b/internal/config/config.go
@@ -25,7 +25,8 @@ type URL struct {
 
 func (u *URL) UnmarshalText(text []byte) (err error) {
 	u.URL, err = url.Parse(string(text))
-	return err
+
+	return errors.Wrapf(err, "could not parse URL %s", string(text))
 }
 
 type Config struct {
@@ -51,15 +52,15 @@ func GetConfig() (*Config, error) {
 	log.Debug("reading config.toml")
 	_, err := toml.DecodeFile("config.toml", &config)
 	if err != nil {
-		var pathError *fs.PathError
-		var tomlError toml.ParseError
-		if errors.As(err, &pathError) {
-			return nil, errors.WithMessage(err, "could not read configuration")
-		} else if errors.As(err, &tomlError) {
-			return nil, errors.WithMessage(err, tomlError.ErrorWithUsage())
-		} else {
-			return nil, errors.Wrap(err, "config error")
+		switch t := err.(type) {
+		case *fs.PathError:
+			return nil, errors.WithMessage(t, "could not read configuration")
+		case *toml.ParseError:
+			return nil, errors.WithMessage(t, t.ErrorWithUsage())
 		}
+
+		return nil, errors.Wrap(err, "config error")
 	}
+
 	return &config, nil
 }
diff --git a/internal/config/cspgenerator.go b/internal/config/cspgenerator.go
index 5ad3ef0..40eca01 100644
--- a/internal/config/cspgenerator.go
+++ b/internal/config/cspgenerator.go
@@ -9,13 +9,15 @@ import (
 
 	"github.com/crewjam/csp"
 	"github.com/fatih/structtag"
+	"github.com/pkg/errors"
 )
 
 func GenerateCSP() error {
 	t := reflect.TypeFor[csp.Header]()
 	file, err := os.OpenFile("./csp.go", os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0)
 	if err != nil {
-		return err
+
+		return errors.Wrap(err, "could not write to output")
 	}
 	defer file.Close()
 
@@ -29,46 +31,43 @@ import (
 
 `)
 	if err != nil {
-		return err
+
+		return errors.Wrap(err, "could not write to output")
 	}
 
 	_, err = fmt.Fprintf(file, "type CSP struct {\n")
 	if err != nil {
-		return err
+		return errors.Wrap(err, "could not write to output")
 	}
 
 	for i := 0; i < t.NumField(); i++ {
 		field := t.Field(i)
 		var t reflect.Type
-		if field.Type.Kind() == reflect.Slice {
-			t = field.Type
-		} else {
-			t = field.Type
-		}
+		t = field.Type
 		tags, err := structtag.Parse(string(field.Tag))
 		if err != nil {
-			return err
+			return errors.Wrap(err, "could not write to output")
 		}
 		cspTag, err := tags.Get("csp")
 		if err != nil {
-			return err
+			return errors.Wrap(err, "could not get csp tag")
 		}
 		err = tags.Set(&structtag.Tag{
 			Key:  "toml",
 			Name: cspTag.Name,
 		})
 		if err != nil {
-			return err
+			return errors.Wrap(err, "could not set toml tag")
 		}
 
 		_, err = fmt.Fprintf(file, "\t%-23s %-28s `%s`\n", field.Name, t, tags.String())
 		if err != nil {
-			return err
+			return errors.Wrap(err, "could not write to output")
 		}
 	}
 	_, err = fmt.Fprintln(file, "}")
 	if err != nil {
-		return err
+		return errors.Wrap(err, "could not write to output")
 	}
 
 	_, err = fmt.Fprintln(file, `
@@ -76,7 +75,8 @@ func (c *CSP) String() string {
 	return csp.Header(*c).String()
 }`)
 	if err != nil {
-		return err
+		return errors.Wrap(err, "could not write to output")
 	}
+
 	return nil
 }