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:
parent
568c005d15
commit
945329a080
70 changed files with 1150 additions and 350 deletions
|
|
@ -2,60 +2,31 @@ package config
|
|||
|
||||
import (
|
||||
"io"
|
||||
"reflect"
|
||||
|
||||
"github.com/FAU-CDI/wisski-distillery/internal/config/validators"
|
||||
"github.com/FAU-CDI/wisski-distillery/pkg/environment"
|
||||
"github.com/FAU-CDI/wisski-distillery/pkg/envreader"
|
||||
"github.com/FAU-CDI/wisski-distillery/pkg/stringparser"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/FAU-CDI/wisski-distillery/pkg/validator"
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
// Unmarshal updates this configuration from the provided [io.Reader].
|
||||
//
|
||||
// Data is read using the [envreader.ReadAll] method, see the appropriate documentation for the file format.
|
||||
//
|
||||
// The `env` and `parser` reflect tags of the [Config] struct determine the keys to read from, and the types to expect.
|
||||
// When a key is missing, it is set to the default value.
|
||||
//
|
||||
// See also [stringparser.Parse].
|
||||
// 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 {
|
||||
// read all the values!
|
||||
values, err := envreader.ReadAll(src)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
vConfig := reflect.ValueOf(config).Elem()
|
||||
tConfig := vConfig.Type()
|
||||
|
||||
// iterate over the types
|
||||
numValues := tConfig.NumField()
|
||||
for i := 0; i < numValues; i++ {
|
||||
tField := tConfig.Field(i)
|
||||
vField := vConfig.FieldByName(tField.Name)
|
||||
|
||||
tEnv := tField.Tag.Get("env")
|
||||
tDefault := tField.Tag.Get("default")
|
||||
tParser := tField.Tag.Get("parser")
|
||||
|
||||
// skip it if it isn't loaded!
|
||||
if tEnv == "" {
|
||||
continue
|
||||
}
|
||||
|
||||
// read the value with a default
|
||||
value, ok := values[tEnv]
|
||||
if !ok || value == "" {
|
||||
if tDefault != "" {
|
||||
value = tDefault
|
||||
}
|
||||
}
|
||||
|
||||
// parse the value!
|
||||
if err := stringparser.Parse(env, tParser, value, vField); err != nil {
|
||||
return errors.Errorf("Config.Unmarshal: Setting %q, Parser %q: %s", tEnv, tParser, err)
|
||||
// read yaml!
|
||||
{
|
||||
decoder := yaml.NewDecoder(src)
|
||||
decoder.KnownFields(true)
|
||||
if err := decoder.Decode(config); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
// do the validator
|
||||
return validator.Validate(config, validators.New(env))
|
||||
}
|
||||
|
||||
func (config *Config) Marshal(dest io.Writer) error {
|
||||
encoder := yaml.NewEncoder(dest)
|
||||
encoder.SetIndent(4)
|
||||
return encoder.Encode(config)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue