From 8c2a6819373a16b9cdd6f5b99eaad059c4c5ee50 Mon Sep 17 00:00:00 2001 From: "A.Unger" Date: Mon, 8 Nov 2021 12:32:52 +0100 Subject: [PATCH] normalize webdav --- accounts/pkg/config/config.go | 39 -------------- webdav/pkg/command/root.go | 23 ++------ webdav/pkg/command/server.go | 8 +-- webdav/pkg/config/config.go | 65 ++++------------------- webdav/pkg/config/{env.go => mappings.go} | 14 +++-- 5 files changed, 23 insertions(+), 126 deletions(-) rename webdav/pkg/config/{env.go => mappings.go} (81%) diff --git a/accounts/pkg/config/config.go b/accounts/pkg/config/config.go index 329a29048..e382cc965 100644 --- a/accounts/pkg/config/config.go +++ b/accounts/pkg/config/config.go @@ -3,14 +3,10 @@ package config import ( "context" - "fmt" "path" - "reflect" "github.com/owncloud/ocis/ocis-pkg/shared" - gofig "github.com/gookit/config/v2" - "github.com/owncloud/ocis/ocis-pkg/config/defaults" ) @@ -224,38 +220,3 @@ func GetEnv() []string { return r } - -// UnmapEnv loads values from the gooconf.Config argument and sets them in the expected destination. -func (c *Config) UnmapEnv(gooconf *gofig.Config) error { - vals := structMappings(c) - for i := range vals { - for j := range vals[i].EnvVars { - // we need to guard against v != "" because this is the condition that checks that the value is set from the environment. - // the `ok` guard is not enough, apparently. - if v, ok := gooconf.GetValue(vals[i].EnvVars[j]); ok && v != "" { - - // get the destination type from destination - switch reflect.ValueOf(vals[i].Destination).Type().String() { - case "*bool": - r := gooconf.Bool(vals[i].EnvVars[j]) - *vals[i].Destination.(*bool) = r - case "*string": - r := gooconf.String(vals[i].EnvVars[j]) - *vals[i].Destination.(*string) = r - case "*int": - r := gooconf.Int(vals[i].EnvVars[j]) - *vals[i].Destination.(*int) = r - case "*float64": - // defaults to float64 - r := gooconf.Float(vals[i].EnvVars[j]) - *vals[i].Destination.(*float64) = r - default: - // it is unlikely we will ever get here. Let this serve more as a runtime check for when debugging. - return fmt.Errorf("invalid type for env var: `%v`", vals[i].EnvVars[j]) - } - } - } - } - - return nil -} diff --git a/webdav/pkg/command/root.go b/webdav/pkg/command/root.go index 1eff029c5..f6ee762a1 100644 --- a/webdav/pkg/command/root.go +++ b/webdav/pkg/command/root.go @@ -71,11 +71,8 @@ func ParseConfig(c *cli.Context, cfg *config.Config) error { // load all env variables relevant to the config in the current context. conf.LoadOSEnv(config.GetEnv(), false) - if err = cfg.UnmapEnv(conf); err != nil { - return err - } - - return nil + bindings := config.StructMappings(cfg) + return ociscfg.BindEnv(conf, bindings) } // SutureService allows for the webdav command to be embedded and supervised by a suture supervisor tree. @@ -85,11 +82,7 @@ type SutureService struct { // NewSutureService creates a new webdav.SutureService func NewSutureService(cfg *ociscfg.Config) suture.Service { - inheritLogging(cfg) - if cfg.Mode == 0 { - cfg.WebDAV.Supervised = true - } - cfg.WebDAV.Log.File = cfg.Log.File + cfg.WebDAV.Log = cfg.Log return SutureService{ cfg: cfg.WebDAV, } @@ -103,13 +96,3 @@ func (s SutureService) Serve(ctx context.Context) error { return nil } - -// inheritLogging is a poor man's global logging state tip-toeing around circular dependencies. It sets the logging -// of the service to whatever is in the higher config (in this case coming from ocis.yaml) and sets them as defaults, -// being overwritten when the extension parses its config file / env variables. -func inheritLogging(cfg *ociscfg.Config) { - cfg.WebDAV.Log.File = cfg.Log.File - cfg.WebDAV.Log.Color = cfg.Log.Color - cfg.WebDAV.Log.Pretty = cfg.Log.Pretty - cfg.WebDAV.Log.Level = cfg.Log.Level -} diff --git a/webdav/pkg/command/server.go b/webdav/pkg/command/server.go index fdcbbb993..009497d29 100644 --- a/webdav/pkg/command/server.go +++ b/webdav/pkg/command/server.go @@ -24,13 +24,7 @@ func Server(cfg *config.Config) *cli.Command { cfg.HTTP.Root = strings.TrimSuffix(cfg.HTTP.Root, "/") } - if err := ParseConfig(ctx, cfg); err != nil { - return err - } - - logger := NewLogger(cfg) - logger.Debug().Str("service", "webdav").Msg("ignoring config file parsing when running supervised") - return nil + return ParseConfig(ctx, cfg) }, Action: func(c *cli.Context) error { logger := NewLogger(cfg) diff --git a/webdav/pkg/config/config.go b/webdav/pkg/config/config.go index e24b86617..801de7213 100644 --- a/webdav/pkg/config/config.go +++ b/webdav/pkg/config/config.go @@ -2,20 +2,10 @@ package config import ( "context" - "fmt" - "reflect" - gofig "github.com/gookit/config/v2" + "github.com/owncloud/ocis/ocis-pkg/shared" ) -// Log defines the available logging configuration. -type Log struct { - Level string `mapstructure:"level"` - Pretty bool `mapstructure:"pretty"` - Color bool `mapstructure:"color"` - File string `mapstructure:"file"` -} - // Debug defines the available debug configuration. type Debug struct { Addr string `mapstructure:"addr"` @@ -57,14 +47,14 @@ type Tracing struct { // Config combines all available configuration parts. type Config struct { - File string `mapstructure:"file"` - Log Log `mapstructure:"log"` - Debug Debug `mapstructure:"debug"` - HTTP HTTP `mapstructure:"http"` - Tracing Tracing `mapstructure:"tracing"` - Service Service `mapstructure:"service"` - OcisPublicURL string `mapstructure:"ocis_public_url"` - WebdavNamespace string `mapstructure:"webdav_namespace"` + File string `mapstructure:"file"` + Log shared.Log `mapstructure:"log"` + Debug Debug `mapstructure:"debug"` + HTTP HTTP `mapstructure:"http"` + Tracing Tracing `mapstructure:"tracing"` + Service Service `mapstructure:"service"` + OcisPublicURL string `mapstructure:"ocis_public_url"` + WebdavNamespace string `mapstructure:"webdav_namespace"` Context context.Context Supervised bool @@ -77,7 +67,7 @@ func New() *Config { func DefaultConfig() *Config { return &Config{ - Log: Log{}, + Log: shared.Log{}, Debug: Debug{ Addr: "", Token: "", @@ -120,38 +110,3 @@ func GetEnv() []string { return r } - -// UnmapEnv loads values from the gooconf.Config argument and sets them in the expected destination. -func (c *Config) UnmapEnv(gooconf *gofig.Config) error { - vals := structMappings(c) - for i := range vals { - for j := range vals[i].EnvVars { - // we need to guard against v != "" because this is the condition that checks that the value is set from the environment. - // the `ok` guard is not enough, apparently. - if v, ok := gooconf.GetValue(vals[i].EnvVars[j]); ok && v != "" { - - // get the destination type from destination - switch reflect.ValueOf(vals[i].Destination).Type().String() { - case "*bool": - r := gooconf.Bool(vals[i].EnvVars[j]) - *vals[i].Destination.(*bool) = r - case "*string": - r := gooconf.String(vals[i].EnvVars[j]) - *vals[i].Destination.(*string) = r - case "*int": - r := gooconf.Int(vals[i].EnvVars[j]) - *vals[i].Destination.(*int) = r - case "*float64": - // defaults to float64 - r := gooconf.Float(vals[i].EnvVars[j]) - *vals[i].Destination.(*float64) = r - default: - // it is unlikely we will ever get here. Let this serve more as a runtime check for when debugging. - return fmt.Errorf("invalid type for env var: `%v`", vals[i].EnvVars[j]) - } - } - } - } - - return nil -} diff --git a/webdav/pkg/config/env.go b/webdav/pkg/config/mappings.go similarity index 81% rename from webdav/pkg/config/env.go rename to webdav/pkg/config/mappings.go index e0392c64f..0a118c24a 100644 --- a/webdav/pkg/config/env.go +++ b/webdav/pkg/config/mappings.go @@ -1,13 +1,17 @@ package config -type mapping struct { - EnvVars []string // name of the EnvVars var. - Destination interface{} // memory address of the original config value to modify. +import "github.com/owncloud/ocis/ocis-pkg/shared" + +// StructMappings binds a set of environment variables to a destination on cfg. Iterating over this set and editing the +// Destination value of a binding will alter the original value, as it is a pointer to its memory address. This lets +// us propagate changes easier. +func StructMappings(cfg *Config) []shared.EnvBinding { + return structMappings(cfg) } // structMappings binds a set of environment variables to a destination on cfg. -func structMappings(cfg *Config) []mapping { - return []mapping{ +func structMappings(cfg *Config) []shared.EnvBinding { + return []shared.EnvBinding{ { EnvVars: []string{"WEBDAV_LOG_FILE", "OCIS_LOG_FILE"}, Destination: &cfg.Log.File,