From 4194da4e884a149ee0e19e8a367916d40dc329c2 Mon Sep 17 00:00:00 2001 From: "A.Unger" Date: Thu, 4 Nov 2021 10:53:40 +0100 Subject: [PATCH] add ocis.yaml --- ocis-pkg/config/config.go | 79 +++++++++++++++++ ocis-pkg/config/helpers.go | 2 +- ocis/pkg/command/ocis_example_config.yaml | 4 - ocis/pkg/command/root.go | 22 ++--- ocis/pkg/config/config.go | 103 ---------------------- proxy/pkg/command/root.go | 2 - 6 files changed, 89 insertions(+), 123 deletions(-) delete mode 100644 ocis/pkg/command/ocis_example_config.yaml diff --git a/ocis-pkg/config/config.go b/ocis-pkg/config/config.go index c69ba7147..d5c0dc634 100644 --- a/ocis-pkg/config/config.go +++ b/ocis-pkg/config/config.go @@ -1,6 +1,10 @@ package config import ( + "fmt" + "reflect" + + gofig "github.com/gookit/config/v2" accounts "github.com/owncloud/ocis/accounts/pkg/config" glauth "github.com/owncloud/ocis/glauth/pkg/config" graphExplorer "github.com/owncloud/ocis/graph-explorer/pkg/config" @@ -124,3 +128,78 @@ func New() *Config { WebDAV: webdav.New(), } } + +// TODO(refs) refactoir refactor this outside +type mapping struct { + EnvVars []string // name of the EnvVars var. + Destination interface{} // memory address of the original config value to modify. +} + +// GetEnv fetches a list of known env variables for this extension. It is to be used by gookit, as it provides a list +// with all the environment variables an extension supports. +func GetEnv() []string { + var r = make([]string, len(structMappings(&Config{}))) + for i := range structMappings(&Config{}) { + r = append(r, structMappings(&Config{})[i].EnvVars...) + } + + return r +} + +// UnmapEnv loads values from the gooconf.Config argument and sets them in the expected destination. +// TODO(refs) can we avoid repetition here? +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 +} + +// structMappings binds a set of environment variables to a destination on cfg. +func structMappings(cfg *Config) []mapping { + return []mapping{ + { + EnvVars: []string{"OCIS_LOG_FILE"}, + Destination: &cfg.Log.Level, + }, + { + EnvVars: []string{"OCIS_LOG_LEVEL"}, + Destination: &cfg.Log.Level, + }, + { + EnvVars: []string{"OCIS_LOG_COLOR"}, + Destination: &cfg.Log.Color, + }, + { + EnvVars: []string{"OCIS_LOG_PRETTY"}, + Destination: &cfg.Log.Pretty, + }, + } +} diff --git a/ocis-pkg/config/helpers.go b/ocis-pkg/config/helpers.go index 660befed6..08dd5ed75 100644 --- a/ocis-pkg/config/helpers.go +++ b/ocis-pkg/config/helpers.go @@ -67,7 +67,7 @@ func sanitizeExtensions(set []string, ext []string, f func(a, b string) bool) [] // is to solely modify `dst`, not dealing with the config structs; and do so in a thread safe manner. func BindSourcesToStructs(extension string, dst interface{}) (*gofig.Config, error) { sources := DefaultConfigSources(extension, supportedExtensions) - cnf := gofig.NewWithOptions("proxy", gofig.ParseEnv) + cnf := gofig.NewWithOptions(extension, gofig.ParseEnv) cnf.AddDriver(gooyaml.Driver) _ = cnf.LoadFiles(sources...) diff --git a/ocis/pkg/command/ocis_example_config.yaml b/ocis/pkg/command/ocis_example_config.yaml deleted file mode 100644 index 280f0627a..000000000 --- a/ocis/pkg/command/ocis_example_config.yaml +++ /dev/null @@ -1,4 +0,0 @@ -log: - pretty: true - color: true - level: info diff --git a/ocis/pkg/command/root.go b/ocis/pkg/command/root.go index 75408d5d7..c37595ff9 100644 --- a/ocis/pkg/command/root.go +++ b/ocis/pkg/command/root.go @@ -3,15 +3,12 @@ package command import ( "os" - gofig "github.com/gookit/config/v2" - gooyaml "github.com/gookit/config/v2/yaml" - - "github.com/owncloud/ocis/ocis-pkg/log" - "github.com/urfave/cli/v2" - "github.com/owncloud/ocis/ocis-pkg/config" + ociscfg "github.com/owncloud/ocis/ocis-pkg/config" + "github.com/owncloud/ocis/ocis-pkg/log" "github.com/owncloud/ocis/ocis-pkg/version" "github.com/owncloud/ocis/ocis/pkg/register" + "github.com/urfave/cli/v2" ) // Execute is the entry point for the ocis command. @@ -24,7 +21,7 @@ func Execute() error { Usage: "ownCloud Infinite Scale Stack", Compiled: version.Compiled(), Before: func(c *cli.Context) error { - return ParseConfig(c, cfg) + return ParseConfig(cfg) }, Authors: []*cli.Author{ { @@ -66,16 +63,15 @@ func NewLogger(cfg *config.Config) log.Logger { } // ParseConfig loads ocis configuration from known paths. -func ParseConfig(c *cli.Context, cfg *config.Config) error { - cnf := gofig.NewWithOptions("ocis", gofig.ParseEnv) - cnf.AddDriver(gooyaml.Driver) - err := cnf.LoadFiles("/Users/aunger/code/owncloud/ocis/ocis/pkg/command/ocis_example_config.yaml") +func ParseConfig(cfg *config.Config) error { + conf, err := ociscfg.BindSourcesToStructs("ocis", cfg) if err != nil { return err } - err = cnf.BindStruct("", cfg) - if err != nil { + conf.LoadOSEnv(config.GetEnv(), false) + + if err = cfg.UnmapEnv(conf); err != nil { return err } diff --git a/ocis/pkg/config/config.go b/ocis/pkg/config/config.go index 76f3a052a..d912156be 100644 --- a/ocis/pkg/config/config.go +++ b/ocis/pkg/config/config.go @@ -1,104 +1 @@ package config - -import ( - accounts "github.com/owncloud/ocis/accounts/pkg/config" - glauth "github.com/owncloud/ocis/glauth/pkg/config" - graphExplorer "github.com/owncloud/ocis/graph-explorer/pkg/config" - graph "github.com/owncloud/ocis/graph/pkg/config" - idp "github.com/owncloud/ocis/idp/pkg/config" - pman "github.com/owncloud/ocis/ocis/pkg/runtime/config" - ocs "github.com/owncloud/ocis/ocs/pkg/config" - proxy "github.com/owncloud/ocis/proxy/pkg/config" - settings "github.com/owncloud/ocis/settings/pkg/config" - storage "github.com/owncloud/ocis/storage/pkg/config" - store "github.com/owncloud/ocis/store/pkg/config" - thumbnails "github.com/owncloud/ocis/thumbnails/pkg/config" - web "github.com/owncloud/ocis/web/pkg/config" - webdav "github.com/owncloud/ocis/webdav/pkg/config" -) - -// Log defines the available logging configuration. -type Log struct { - Level string - Pretty bool - Color bool -} - -// Debug defines the available debug configuration. -type Debug struct { - Addr string - Token string - Pprof bool - Zpages bool -} - -// HTTP defines the available http configuration. -type HTTP struct { - Addr string - Root string -} - -// GRPC defines the available grpc configuration. -type GRPC struct { - Addr string -} - -// Tracing defines the available tracing configuration. -type Tracing struct { - Enabled bool - Type string - Endpoint string - Collector string - Service string -} - -// TokenManager is the config for using the reva token manager -type TokenManager struct { - JWTSecret string -} - -// Config combines all available configuration parts. -type Config struct { - Registry string - Log Log - Debug Debug - HTTP HTTP - GRPC GRPC - Tracing Tracing - TokenManager TokenManager - - Accounts *accounts.Config - GLAuth *glauth.Config - Graph *graph.Config - GraphExplorer *graphExplorer.Config - IDP *idp.Config - OCS *ocs.Config - Web *web.Config - Proxy *proxy.Config - Settings *settings.Config - Storage *storage.Config - Store *store.Config - Thumbnails *thumbnails.Config - WebDAV *webdav.Config - Runtime *pman.Config -} - -// New initializes a new configuration with or without defaults. -func New() *Config { - return &Config{ - Accounts: accounts.DefaultConfig(), - GLAuth: glauth.New(), - Graph: graph.New(), - GraphExplorer: graphExplorer.New(), - IDP: idp.New(), - OCS: ocs.New(), - Web: web.New(), - Proxy: proxy.DefaultConfig(), - Settings: settings.New(), - Storage: storage.New(), - Store: store.New(), - Thumbnails: thumbnails.New(), - WebDAV: webdav.New(), - Runtime: pman.NewConfig(), - } -} diff --git a/proxy/pkg/command/root.go b/proxy/pkg/command/root.go index ae5ce38b3..175399284 100644 --- a/proxy/pkg/command/root.go +++ b/proxy/pkg/command/root.go @@ -27,8 +27,6 @@ func Execute(cfg *config.Config) error { }, }, - //Flags: flagset.RootWithConfig(cfg), - Before: func(c *cli.Context) error { cfg.Service.Version = version.String return nil