From 55d5c9c529ebdc095e7dd33366c5c48396cc3ec1 Mon Sep 17 00:00:00 2001 From: Tom Wiesing Date: Wed, 23 Nov 2022 15:27:58 +0100 Subject: [PATCH] config: Handle errors better --- internal/config/config_template | 4 ++++ internal/config/read.go | 8 ++++---- internal/dis/component/component.go | 18 +++++++++++++++--- pkg/stringparser/parse.go | 8 +++++--- 4 files changed, 28 insertions(+), 10 deletions(-) diff --git a/internal/config/config_template b/internal/config/config_template index bc8b2bc..4f24138 100644 --- a/internal/config/config_template +++ b/internal/config/config_template @@ -69,6 +69,10 @@ GRAPHDB_ADMIN_PASSWORD=${GRAPHDB_ADMIN_PASSWORD} MYSQL_ADMIN_USER=${MYSQL_ADMIN_USER} MYSQL_ADMIN_PASSWORD=${MYSQL_ADMIN_PASSWORD} +# The admin user and password required to access the keycloak server and api +KEYCLOAK_ADMIN_USER=${KEYCLOAK_ADMIN_USER} +KEYCLOAK_ADMIN_PASSWORD=${KEYCLOAK_ADMIN_PASSWORD} + # The admin user and password required to access the /dis/ server and api DIS_ADMIN_USER=${DIS_ADMIN_USER} DIS_ADMIN_PASSWORD=${DIS_ADMIN_PASSWORD} diff --git a/internal/config/read.go b/internal/config/read.go index bddb774..649f31c 100644 --- a/internal/config/read.go +++ b/internal/config/read.go @@ -7,6 +7,7 @@ import ( "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" ) // Unmarshal updates this configuration from the provided [io.Reader]. @@ -45,15 +46,14 @@ func (config *Config) Unmarshal(env environment.Environment, src io.Reader) erro // read the value with a default value, ok := values[tEnv] if !ok || value == "" { - if tDefault == "" { - continue + if tDefault != "" { + value = tDefault } - value = tDefault } // parse the value! if err := stringparser.Parse(env, tParser, value, vField); err != nil { - return err + return errors.Errorf("Config.Unmarshal: Setting %q, Parser %q: %s", tEnv, tParser, err) } } diff --git a/internal/dis/component/component.go b/internal/dis/component/component.go index 5010d91..476a200 100644 --- a/internal/dis/component/component.go +++ b/internal/dis/component/component.go @@ -20,6 +20,10 @@ type Component interface { // Name should be implemented by the [ComponentBase] struct. Name() string + // ID returns a unique id of this component + // ID should be implemented by the [ComponentBase] struct. + ID() string + // getBase returns the underlying ComponentBase object of this Component. // It is used internally during initialization getBase() *Base @@ -27,8 +31,8 @@ type Component interface { // Base is embedded into every Component type Base struct { - name string // name is the name of this component - Still // the underlying still of the distillery + name, id string // name and id of this component + Still // the underlying still of the distillery } //lint:ignore U1000 used to implement the private methods of [Component] @@ -41,7 +45,11 @@ func (cb *Base) getBase() *Base { func Init(component Component, core Still) Component { base := component.getBase() // pointer to a struct base.Still = core - base.name = strings.ToLower(reflect.TypeOf(component).Elem().Name()) + + tp := reflect.TypeOf(component).Elem() + base.name = strings.ToLower(tp.Name()) + base.id = tp.PkgPath() + "." + tp.Name() + return component } @@ -49,6 +57,10 @@ func (cb Base) Name() string { return cb.name } +func (cb Base) ID() string { + return cb.id +} + // Still represents the central part of a distillery. // It is used inside the main distillery struct, as well as every component via [ComponentBase]. type Still struct { diff --git a/pkg/stringparser/parse.go b/pkg/stringparser/parse.go index 777d929..91c5741 100644 --- a/pkg/stringparser/parse.go +++ b/pkg/stringparser/parse.go @@ -8,19 +8,21 @@ import ( "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 { // use the validator parser, ok := knownParsers[strings.ToLower(name)] if parser == nil || !ok { - return errors.Errorf("unknown parser %q", name) + return errUnknownParser } // get the parsed value checked, err := parser(env, value) if err != nil { - return errors.Wrapf(err, "parser %s returned error", name) + return err } // set the value of the field @@ -34,7 +36,7 @@ func Parse(env environment.Environment, name, value string, vField reflect.Value // capture any error if errSet != nil { - return errors.Errorf("parser %s: set returned %v", name, errSet) + return errors.Errorf("set returned %v", name, errSet) } return nil