wisski-cloud-distillery/internal/config/read.go
Tom Wiesing c3ca8e2974
Move to github.com/tkw1536/pkglib package
This commit removes various modules that can be migrated to the
github.com/tkw1536/pkglib package without any code changes (beyond
module renamings).
2023-02-26 09:53:25 +01:00

37 lines
997 B
Go

package config
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 {
// read yaml!
{
decoder := yaml.NewDecoder(src)
decoder.KnownFields(true)
if err := decoder.Decode(config); err != nil {
return err
}
}
// TODO: should this be done seperatly?
return config.Validate(env)
}
// 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) Marshal(dest io.Writer) error {
encoder := yaml.NewEncoder(dest)
encoder.SetIndent(4)
return encoder.Encode(config)
}