Move to yaml-based configuration

This commit updates the configuration to be yaml-based and updates the
configuration to read in a yaml file.
This commit is contained in:
Tom Wiesing 2023-02-12 18:13:52 +01:00
parent 568c005d15
commit 945329a080
No known key found for this signature in database
70 changed files with 1150 additions and 350 deletions

View file

@ -0,0 +1,32 @@
package validators
import (
"github.com/FAU-CDI/wisski-distillery/pkg/environment"
"github.com/FAU-CDI/wisski-distillery/pkg/validator"
)
// New creates a new set of standard validators for the configuration
func New(env environment.Environment) validator.Collection {
coll := make(validator.Collection)
validator.Add(coll, "nonempty", ValidateNonempty)
validator.Add(coll, "directory", func(value *string, dflt string) error {
return ValidateDirectory(env, value, dflt)
})
validator.Add(coll, "file", func(value *string, dflt string) error {
return ValidateFile(env, value, dflt)
})
validator.Add(coll, "domain", ValidateDomain)
validator.AddSlice(coll, "domains", ",", ValidateDomain)
validator.Add(coll, "https", ValidateHTTPSURL)
validator.Add(coll, "slug", ValidateSlug)
validator.Add(coll, "email", ValidateEmail)
validator.Add(coll, "positive", ValidatePositive)
validator.Add(coll, "port", ValidatePort)
validator.Add(coll, "duration", ValidateDuration)
return coll
}

View file

@ -0,0 +1,21 @@
package validators
import (
"regexp"
"strings"
"github.com/pkg/errors"
)
var regexpDomain = regexp.MustCompile(`^([a-zA-Z0-9][-a-zA-Z0-9]*\.)*[a-zA-Z0-9][-a-zA-Z0-9]*$`) // TODO: Make this regexp nicer!
func ValidateDomain(domain *string, dflt string) error {
if *domain == "" {
*domain = dflt
}
if !regexpDomain.MatchString(*domain) {
return errors.Errorf("%q is not a valid domain", *domain)
}
*domain = strings.ToLower(*domain)
return nil
}

View file

@ -0,0 +1,14 @@
package validators
import "time"
func ValidateDuration(d *time.Duration, dflt string) error {
if *d == 0 {
var err error
*d, err = time.ParseDuration(dflt)
if err != nil {
return err
}
}
return nil
}

View file

@ -0,0 +1,24 @@
package validators
import (
"regexp"
"github.com/pkg/errors"
)
var regexpEmail = regexp.MustCompile(`^([-a-zA-Z0-9]+)\@([a-zA-Z0-9][-a-zA-Z0-9]*\.)*[a-zA-Z0-9][-a-zA-Z0-9]*$`) // TODO: Make this regexp nicer!
// ValidateEmail checks that s represents an email, and then returns it as is.
func ValidateEmail(email *string, dflt string) error {
if *email == "" {
*email = dflt
}
if *email == "" { // no email provided => ok
return nil
}
if !regexpEmail.MatchString(*email) {
return errors.Errorf("%q is not a valid email", *email)
}
return nil
}

View file

@ -0,0 +1,27 @@
package validators
import (
"github.com/FAU-CDI/wisski-distillery/pkg/environment"
"github.com/FAU-CDI/wisski-distillery/pkg/fsx"
"github.com/pkg/errors"
)
func ValidateFile(env environment.Environment, path *string, dflt string) error {
if *path == "" {
*path = dflt
}
if !fsx.IsFile(env, *path) {
return errors.Errorf("%q does not exist or is not a file", *path)
}
return nil
}
func ValidateDirectory(env environment.Environment, path *string, dflt string) error {
if *path == "" {
*path = dflt
}
if !fsx.IsDirectory(env, *path) {
return errors.Errorf("%q does not exist or is not a directory", *path)
}
return nil
}

View file

@ -0,0 +1,32 @@
package validators
import (
"strconv"
"github.com/pkg/errors"
)
func ValidatePositive(value *int, dflt string) (err error) {
if *value == 0 && dflt != "" {
v, err := strconv.ParseInt(dflt, 10, 64)
if err != nil {
return err
}
*value = int(v)
}
if *value <= 0 {
return errors.Errorf("%d is not a positive value", *value)
}
return nil
}
func ValidatePort(value *uint16, dflt string) (err error) {
if *value == 0 && dflt != "" {
v, err := strconv.ParseUint(dflt, 10, 16)
if err != nil {
return err
}
*value = uint16(v)
}
return nil
}

View file

@ -0,0 +1,24 @@
package validators
import (
"regexp"
"strings"
"github.com/pkg/errors"
)
var regexpSlug = regexp.MustCompile(`^[a-zA-Z0-9][-a-zA-Z0-9]*$`) // TODO: Make this regexp nicer!
var ErrInvalidSlug = errors.New("invalid slug")
// ValidateSlug validates a slug and normalizes it.
func ValidateSlug(s *string, dflt string) error {
if *s == "" {
*s = dflt
}
*s = strings.ToLower(*s)
if !regexpSlug.MatchString(*s) {
return ErrInvalidSlug
}
return nil
}

View file

@ -0,0 +1,16 @@
package validators
import "github.com/pkg/errors"
var errEmpty = errors.New("value is empty")
func ValidateNonempty(value *string, dflt string) error {
if *value == "" {
*value = dflt
}
if *value == "" {
return errEmpty
}
return nil
}

View file

@ -0,0 +1,46 @@
package validators
import (
"net/url"
"github.com/pkg/errors"
)
// URL represents a url.URL that is marshaled as a string representing the url.
type URL url.URL
func (u *URL) MarshalText() (text []byte, err error) {
return []byte(u.String()), nil
}
func (u *URL) String() string {
if u == nil {
return ""
}
return (*url.URL)(u).String()
}
func (u *URL) UnmarshalText(text []byte) error {
if len(text) == 0 {
return nil
}
pu, err := url.Parse(string(text))
if err != nil {
return err
}
*u = URL(*pu)
return nil
}
func ValidateHTTPSURL(url **URL, dflt string) error {
if (*url).String() == "" {
*url = new(URL)
if err := (*url).UnmarshalText([]byte(dflt)); err != nil {
return err
}
}
if (*url).Scheme != "https" {
return errors.Errorf("%q is not a valid https URL (%q)", *url, (*url).Scheme)
}
return nil
}