Remove environment.Environment struct

This commit completely removes the environment struct as it is no longer
used.
This commit is contained in:
Tom Wiesing 2023-03-02 12:52:51 +01:00
parent 3263920d6b
commit 473040a69f
No known key found for this signature in database
40 changed files with 91 additions and 146 deletions

View file

@ -12,14 +12,13 @@ import (
"github.com/FAU-CDI/wisski-distillery/internal/config/legacy/envreader"
"github.com/FAU-CDI/wisski-distillery/internal/config/legacy/stringparser"
"github.com/FAU-CDI/wisski-distillery/internal/config/validators"
"github.com/FAU-CDI/wisski-distillery/pkg/environment"
"github.com/pkg/errors"
)
// Migrate parses a configuration from an old configuration.
func Migrate(config *config.Config, env environment.Environment, src io.Reader) error {
func Migrate(config *config.Config, src io.Reader) error {
var legacy Legacy
if err := legacy.Unmarshal(env, src); err != nil {
if err := legacy.Unmarshal(src); err != nil {
return nil
}
return legacy.Migrate(config)
@ -104,7 +103,7 @@ func (legacy *Legacy) Migrate(cfg *config.Config) error {
// When a key is missing, it is set to the default value.
//
// See also [stringparser.Parse].
func (config *Legacy) Unmarshal(env environment.Environment, src io.Reader) error {
func (config *Legacy) Unmarshal(src io.Reader) error {
// read all the values!
values, err := envreader.ReadAll(src)
if err != nil {
@ -138,7 +137,7 @@ func (config *Legacy) Unmarshal(env environment.Environment, src io.Reader) erro
}
// parse the value!
if err := stringparser.Parse(env, tParser, value, vField); err != nil {
if err := stringparser.Parse(tParser, value, vField); err != nil {
return errors.Errorf("Legacy.Unmarshal: Setting %q, Parser %q: %s", tEnv, tParser, err)
}
}

View file

@ -4,14 +4,13 @@ import (
"reflect"
"strings"
"github.com/FAU-CDI/wisski-distillery/pkg/environment"
"github.com/pkg/errors"
)
var errUnknownParser = errors.New("unknown parser")
// Parse parses the provided value with the parser.
func Parse(env environment.Environment, name, value string, vField reflect.Value) error {
func Parse(name, value string, vField reflect.Value) error {
// use the validator
parser, ok := knownParsers[strings.ToLower(name)]
@ -20,7 +19,7 @@ func Parse(env environment.Environment, name, value string, vField reflect.Value
}
// get the parsed value
checked, err := parser(env, value)
checked, err := parser(value)
if err != nil {
return err
}
@ -58,8 +57,8 @@ var knownParsers map[string]Parser[any] = map[string]Parser[any]{
}
func asGenericParser[T any](parser Parser[T]) Parser[any] {
return func(env environment.Environment, s string) (value any, err error) {
value, err = parser(env, s)
return func(s string) (value any, err error) {
value, err = parser(s)
return
}
}

View file

@ -9,7 +9,6 @@ import (
"strings"
"time"
"github.com/FAU-CDI/wisski-distillery/pkg/environment"
"github.com/FAU-CDI/wisski-distillery/pkg/fsx"
"github.com/pkg/errors"
)
@ -20,10 +19,10 @@ import (
// 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(env environment.Environment, s string) (T, error)
type Parser[T any] func(s string) (T, error)
// ParseAbspath checks that s is an absolute path and returns it as-is
func ParseAbspath(env environment.Environment, s string) (string, error) {
func ParseAbspath(s string) (string, error) {
if !fsx.IsDirectory(s) {
return "", errors.Errorf("%q does not exist or is not a directory", s)
}
@ -31,7 +30,7 @@ func ParseAbspath(env environment.Environment, s string) (string, error) {
}
// ParseFile checks that s is a valid file and returns it as-is
func ParseFile(env environment.Environment, s string) (string, error) {
func ParseFile(s string) (string, error) {
if !fsx.IsFile(s) {
return "", errors.Errorf("%q does not exist or is not a regular file", s)
}
@ -41,7 +40,7 @@ func ParseFile(env environment.Environment, s string) (string, error) {
var errEmptyString = errors.New("value is empty")
// ParseNonEmpty checks that s is a non-empty string and returns it as-is
func ParseNonEmpty(env environment.Environment, s string) (string, error) {
func ParseNonEmpty(s string) (string, error) {
if s == "" {
return "", errEmptyString
}
@ -51,7 +50,7 @@ func ParseNonEmpty(env environment.Environment, s string) (string, error) {
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 in lowercase
func ParseValidDomain(env environment.Environment, s string) (string, error) {
func ParseValidDomain(s string) (string, error) {
if !regexpDomain.MatchString(s) {
return "", errors.Errorf("%q is not a valid domain", s)
}
@ -59,7 +58,7 @@ func ParseValidDomain(env environment.Environment, s string) (string, error) {
}
// ParseValidDomains checks that s is a comma-seperated list of valid domains and returns them in lower case
func ParseValidDomains(env environment.Environment, s string) ([]string, error) {
func ParseValidDomains(s string) ([]string, error) {
if len(s) == 0 {
return []string{}, nil
}
@ -73,19 +72,19 @@ func ParseValidDomains(env environment.Environment, s string) ([]string, error)
}
// ParseNumber parses s as a decimal integer
func ParseNumber(env environment.Environment, s string) (int, error) {
func ParseNumber(s string) (int, error) {
value, err := strconv.ParseInt(s, 10, 64)
return int(value), err
}
// ParsePort parses s as a port
func ParsePort(env environment.Environment, s string) (uint16, error) {
func ParsePort(s string) (uint16, error) {
value, err := strconv.ParseUint(s, 10, 16)
return uint16(value), err
}
// ParseHttpsURL parses a string into a url that starts with 'https://'
func ParseHttpsURL(env environment.Environment, s string) (*url.URL, error) {
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)
@ -99,7 +98,7 @@ func ParseHttpsURL(env environment.Environment, s string) (*url.URL, error) {
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(env environment.Environment, s string) (string, error) {
func ParseEmail(s string) (string, error) {
if s == "" { // no email provided
return "", nil
}
@ -112,7 +111,7 @@ func ParseEmail(env environment.Environment, s string) (string, error) {
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(env environment.Environment, s string) (string, error) {
func ParseSlug(s string) (string, error) {
if !regexpSlug.MatchString(s) {
return "", errors.Errorf("%q is not a valid slug", s)
}
@ -120,6 +119,6 @@ func ParseSlug(env environment.Environment, s string) (string, error) {
}
// ParseDuration parses a time.Duration
func ParseDuration(env environment.Environment, s string) (time.Duration, error) {
func ParseDuration(s string) (time.Duration, error) {
return time.ParseDuration(s)
}

View file

@ -5,7 +5,6 @@ import (
"path/filepath"
"github.com/FAU-CDI/wisski-distillery/internal/bootstrap"
"github.com/FAU-CDI/wisski-distillery/pkg/environment"
"github.com/FAU-CDI/wisski-distillery/pkg/fsx"
)
@ -34,7 +33,7 @@ func (pcfg PathsConfig) ExecutablePath() string {
}
// UsingDistilleryExecutable checks if the current process is using the distillery executable
func (pcfg PathsConfig) UsingDistilleryExecutable(env environment.Environment) bool {
func (pcfg PathsConfig) UsingDistilleryExecutable() bool {
exe, err := os.Executable()
if err != nil {
return false
@ -44,7 +43,7 @@ func (pcfg PathsConfig) UsingDistilleryExecutable(env environment.Environment) b
// CurrentExecutable returns the path to the current executable being used.
// When it does not exist, falls back to the default executable.
func (pcfg PathsConfig) CurrentExecutable(env environment.Environment) string {
func (pcfg PathsConfig) CurrentExecutable() string {
exe, err := os.Executable()
if err != nil || !fsx.IsFile(exe) {
return pcfg.ExecutablePath()

View file

@ -4,14 +4,13 @@ import (
"io"
"github.com/FAU-CDI/wisski-distillery/internal/config/validators"
"github.com/FAU-CDI/wisski-distillery/pkg/environment"
"github.com/tkw1536/pkglib/validator"
"gopkg.in/yaml.v3"
)
// Unmarshal reads configuration from the provided io.Reader, and then validates it.
// Configuration is read in yaml format.
func (config *Config) Unmarshal(env environment.Environment, src io.Reader) error {
func (config *Config) Unmarshal(src io.Reader) error {
// read yaml!
{
decoder := yaml.NewDecoder(src)
@ -22,12 +21,12 @@ func (config *Config) Unmarshal(env environment.Environment, src io.Reader) erro
}
// TODO: should this be done seperatly?
return config.Validate(env)
return config.Validate()
}
// Validate validates this configuration file and sets appropriate defaults
func (config *Config) Validate(env environment.Environment) error {
return validator.Validate(config, validators.New(env))
func (config *Config) Validate() error {
return validator.Validate(config, validators.New())
}
func (config *Config) Marshal(dest io.Writer) error {

View file

@ -7,7 +7,6 @@ import (
"github.com/FAU-CDI/wisski-distillery/internal/bootstrap"
"github.com/FAU-CDI/wisski-distillery/internal/passwordx"
"github.com/FAU-CDI/wisski-distillery/pkg/environment"
"github.com/tkw1536/pkglib/hostname"
"github.com/tkw1536/pkglib/password"
)
@ -28,13 +27,13 @@ type Template struct {
}
// SetDefaults sets defaults on the template
func (tpl *Template) SetDefaults(env environment.Environment) (err error) {
func (tpl *Template) SetDefaults() (err error) {
if tpl.RootPath == "" {
tpl.RootPath = bootstrap.BaseDirectoryDefault
}
if tpl.DefaultDomain == "" {
tpl.DefaultDomain = hostname.FQDN() // TODO: Make this environment specific
tpl.DefaultDomain = hostname.FQDN()
}
if tpl.TSAdminUser == "" {

View file

@ -1,22 +1,17 @@
package validators
import (
"github.com/FAU-CDI/wisski-distillery/pkg/environment"
"github.com/tkw1536/pkglib/validator"
)
// New creates a new set of standard validators for the configuration
func New(env environment.Environment) validator.Collection {
func New() 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, "directory", ValidateDirectory)
validator.Add(coll, "file", ValidateFile)
validator.Add(coll, "domain", ValidateDomain)
validator.AddSlice(coll, "domains", ",", ValidateDomain)

View file

@ -1,12 +1,11 @@
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 {
func ValidateFile(path *string, dflt string) error {
if *path == "" {
*path = dflt
}
@ -16,7 +15,7 @@ func ValidateFile(env environment.Environment, path *string, dflt string) error
return nil
}
func ValidateDirectory(env environment.Environment, path *string, dflt string) error {
func ValidateDirectory(path *string, dflt string) error {
if *path == "" {
*path = dflt
}