Do a large chunk of the move to go

This commit moves a huge chunk of the code to go. The TODO.md document
indicates what is left to be done.
This commit is contained in:
Tom Wiesing 2022-08-14 10:57:59 +02:00
parent db2ad9b4bd
commit 7b38fdd801
No known key found for this signature in database
93 changed files with 4689 additions and 645 deletions

View file

@ -0,0 +1,105 @@
package config
import (
"net/url"
"regexp"
"strconv"
"strings"
"github.com/FAU-CDI/wisski-distillery/internal/fsx"
"github.com/pkg/errors"
)
// Validator reads from the configuration file
type Validator func(s string) (interface{}, error)
var knownValidators map[string]Validator = map[string]Validator{
"is_valid_abspath": IsValidAbspath,
"is_valid_domain": IsValidDomain,
"is_valid_domains": IsValidDomains,
"is_valid_number": IsValidNumber,
"is_valid_https_url": IsValidHttpsURL,
"is_valid_slug": IsValidSlug,
"is_valid_file": IsValidFile,
"is_valid_email": IsValidEmail,
"is_nonempty": IsNonEmpty,
}
func IsValidAbspath(s string) (interface{}, error) {
if !fsx.IsDirectory(s) {
return nil, errors.Errorf("%q does not exist or is not a directory", s)
}
return s, nil
}
func IsValidFile(s string) (interface{}, error) {
if !fsx.IsFile(s) {
return nil, errors.Errorf("%q does not exist or is not a regular file", s)
}
return s, nil
}
func IsNonEmpty(s string) (interface{}, error) {
if s == "" {
return nil, errors.New("value is empty")
}
return s, nil
}
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 IsValidDomain(s string) (interface{}, error) {
if !regexpDomain.MatchString(s) {
return nil, errors.Errorf("%q is not a valid domain", s)
}
return s, nil
}
func IsValidDomains(s string) (interface{}, error) {
if len(s) == 0 {
return []string{}, nil
}
domains := strings.Split(s, ",")
for _, d := range domains {
if !regexpDomain.MatchString(d) {
return nil, errors.Errorf("%q is not a valid domain", d)
}
}
return domains, nil
}
func IsValidNumber(s string) (interface{}, error) {
value, err := strconv.ParseInt(s, 10, 64)
return int(value), err
}
func IsValidHttpsURL(s string) (interface{}, error) {
url, err := url.Parse(s)
if err != nil {
return nil, errors.Wrapf(err, "%q is not a valid URL", s)
}
if url.Scheme != "https" {
return nil, errors.Errorf("%q is not a valid https URL (%q)", s, url.Scheme)
}
return url, nil
}
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!
func IsValidEmail(s string) (interface{}, error) {
if s == "" { // no email provided
return "", nil
}
if !regexpEmail.MatchString(s) {
return nil, errors.Errorf("%q is not a valid email", s)
}
return s, nil
}
var regexpSlug = regexp.MustCompile(`^[a-zA-Z0-9][-a-zA-Z0-9]*$`) // TODO: Make this regexp nicer!
func IsValidSlug(s string) (interface{}, error) {
if !regexpSlug.MatchString(s) {
return nil, errors.Errorf("%q is not a valid slug", s)
}
return s, nil
}