Rename packages

This commit is contained in:
Tom Wiesing 2022-09-14 14:17:08 +02:00
parent 49b8760527
commit ef1243ea39
No known key found for this signature in database
47 changed files with 524 additions and 369 deletions

117
pkg/envreader/envreader.go Normal file
View file

@ -0,0 +1,117 @@
// Package envreader
package envreader
import (
"bufio"
"io"
"strings"
)
// Scanner is a scanner for environment files.
// To create a new scanner use [NewScanner].
//
// It scans through a reader and reads environment variables from it.
// Reads may be internally buffered.
//
// An environment variable is of the form:
// KEY=VALUE
// on a separate line.
// Keys and values are case-sensitive and may contain anything except for newline characters.
// Spaces around key and value are trimmed using [strings.TrimSpace].
// Keys may not contain an '='.
// Lines not containing a '=' (e.g. blank lines) and those starting with '#' and '//' are ignored.
//
// To advance the scanner to the next key, value pair use [Scan].
// To get the current (key, value) pair, use [Data].
//
// A typical use-case of a scanner is as follows:
//
// scanner := NewScanner(r)
// for scanner.Scan() {
// // process any data ....
// fmt.Println(scanner.Data())
// }
// if err := scanner.Err(); err != nil {
// // handle errors
// }
//
// For the common use case of reading a set of distinct keys from a file see [ReadAll].
type Scanner struct {
s *bufio.Scanner
// current key and value
key string
value string
}
// NewScanner creates a new scanner from the underlying Reader
func NewScanner(r io.Reader) *Scanner {
return &Scanner{
s: bufio.NewScanner(r),
}
}
// Scanner advances the scanner until the next KEY=VALUE pair.
//
// If there are no more values left (e.g. the underlying reader returned io.EOF)
// or when an unexpected error occured, returns false.
//
// A caller should always check Err() to see if there was an error.
func (scanner *Scanner) Scan() bool {
var found bool
for scanner.s.Scan() {
// check that we don't have an empty or comment only line
tokens := strings.TrimSpace(scanner.s.Text())
if len(tokens) == 0 || tokens[0] == '#' || strings.HasPrefix(tokens, "//") {
continue
}
// check that we have a 'key=value' pair
scanner.key, scanner.value, found = strings.Cut(tokens, "=")
if !found {
continue
}
// got a key = value
scanner.key = strings.TrimSpace(scanner.key)
scanner.value = strings.TrimSpace(scanner.value)
return true
}
// nothing found
scanner.key = ""
scanner.value = ""
return false
}
// Data reads the current value from the scanner.
// When Scan() has not been called, or returned false, returns two empty strings.
func (scanner Scanner) Data() (key, value string) {
return scanner.key, scanner.value
}
// Err returns any error that occured on the underlying read.
//
// When no error occured, or the underlying read is io.EOF, returns nil.
func (scanner Scanner) Err() error {
return scanner.s.Err()
}
// ReadAll creates a new [Scanner], and then reads all key/value pairs from r.
// If a key occurs more than once, only the last value is set in the returned map.
func ReadAll(r io.Reader) (values map[string]string, err error) {
scanner := NewScanner(r)
// read and store all values
values = make(map[string]string)
for scanner.Scan() {
key, value := scanner.Data()
values[key] = value
}
// check if there was an error!
if err := scanner.Err(); err != nil {
return nil, err
}
return values, nil
}

View file

@ -0,0 +1,42 @@
// Package envreader
package envreader
import (
"fmt"
"strings"
)
func ExampleNewScanner() {
scanner := NewScanner(strings.NewReader(`
lines without an equal sign are ignored
// this line is a comment, even with an = sign
KEY=VALUE
# this is also a comment =
spaces in keys = spaces in values
multiple=equal=signs
CaSe = SenSitiVe
empty value=
=empty key
`))
for scanner.Scan() {
key, value := scanner.Data()
fmt.Printf("%q %q\n", key, value)
}
if err := scanner.Err(); err != nil {
fmt.Println(scanner.Err())
} else {
fmt.Println("no error")
}
// Output: "KEY" "VALUE"
// "spaces in keys" "spaces in values"
// "multiple" "equal=signs"
// "CaSe" "SenSitiVe"
// "empty value" ""
// "" "empty key"
// no error
}

60
pkg/stringparser/parse.go Normal file
View file

@ -0,0 +1,60 @@
package stringparser
import (
"reflect"
"strings"
"github.com/pkg/errors"
)
// Parse parses the provided value with the parser.
func Parse(name, value string, vField reflect.Value) error {
// use the validator
parser, ok := knownParsers[strings.ToLower(name)]
if parser == nil || !ok {
return errors.Errorf("unknown parser %q", name)
}
// get the parsed value
checked, err := parser(value)
if err != nil {
return errors.Wrapf(err, "parser %s returned error", name)
}
// set the value of the field
var errSet interface{}
func() {
defer func() {
errSet = recover()
}()
vField.Set(reflect.ValueOf(checked))
}()
// capture any error
if errSet != nil {
return errors.Errorf("parser %s: set returned %v", name, errSet)
}
return nil
}
// knownParsers holds the known parsers
var knownParsers map[string]Parser[any] = map[string]Parser[any]{
"abspath": asGenericParser(ParseAbspath),
"domain": asGenericParser(ParseValidDomain),
"domains": asGenericParser(ParseValidDomains),
"number": asGenericParser(ParseNumber),
"https_url": asGenericParser(ParseHttpsURL),
"slug": asGenericParser(ParseSlug),
"file": asGenericParser(ParseFile),
"email": asGenericParser(ParseEmail),
"nonempty": asGenericParser(ParseNonEmpty),
}
func asGenericParser[T any](parser Parser[T]) Parser[any] {
return func(s string) (value any, err error) {
value, err = parser(s)
return
}
}

View file

@ -0,0 +1,111 @@
// Package stringparser provides Parser
package stringparser
import (
"net/url"
"regexp"
"strconv"
"strings"
"github.com/FAU-CDI/wisski-distillery/pkg/fsx"
"github.com/pkg/errors"
)
// Parser is used to read a value from a string and turn it into a golang value.
// It is simultaniously used to validate particular setting.
//
// Parsers can be found in this package as functions called Parse*.
// They are refered to by their name, e.g. ParseNonempty can be refered to by the name 'Nonempty'.
// See [Parse].
type Parser[T any] func(s string) (T, error)
// ParseAbspath checks that s is an absolute path and returns it as-is
func ParseAbspath(s string) (string, error) {
if !fsx.IsDirectory(s) {
return "", errors.Errorf("%q does not exist or is not a directory", s)
}
return s, nil
}
// ParseFile checks that s is a valid file and returns it as-is
func ParseFile(s string) (string, error) {
if !fsx.IsFile(s) {
return "", errors.Errorf("%q does not exist or is not a regular file", s)
}
return s, nil
}
var errEmptyString = errors.New("value is empty")
// ParseNonEmpty checks that s is a non-empty string and returns it as-is
func ParseNonEmpty(s string) (string, error) {
if s == "" {
return "", errEmptyString
}
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!
// ParseValidDomain checks that s is a valid domain and returns it as-is
func ParseValidDomain(s string) (string, error) {
if !regexpDomain.MatchString(s) {
return "", errors.Errorf("%q is not a valid domain", s)
}
return s, nil
}
// ParseValidDomains checks that s is a comma-seperated list of valid domains and returns them as-is
func ParseValidDomains(s string) ([]string, 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
}
// ParseNumber parses s as a decimal integer
func ParseNumber(s string) (int, error) {
value, err := strconv.ParseInt(s, 10, 64)
return int(value), err
}
// ParseHttpsURL parses a string into a url that starts with 'https://'
func ParseHttpsURL(s string) (*url.URL, 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!
// ParseEmail checks that s represents an email, and then returns it as is.
func ParseEmail(s string) (string, error) {
if s == "" { // no email provided
return "", nil
}
if !regexpEmail.MatchString(s) {
return "", 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!
// ParseSlug parses s as a slug and returns it as is.
func ParseSlug(s string) (string, error) {
if !regexpSlug.MatchString(s) {
return "", errors.Errorf("%q is not a valid slug", s)
}
return s, nil
}