diff --git a/ocis-pkg/config/config.go b/ocis-pkg/config/config.go index bdd6cd549..d0cd0e1d2 100644 --- a/ocis-pkg/config/config.go +++ b/ocis-pkg/config/config.go @@ -18,17 +18,9 @@ import ( webdav "github.com/owncloud/ocis/webdav/pkg/config" ) -// Tracing defines the available tracing configuration. -type Tracing struct { - Enabled bool `ocisConfig:"enabled"` - Type string `ocisConfig:"type"` - Endpoint string `ocisConfig:"endpoint"` - Collector string `ocisConfig:"collector"` -} - // TokenManager is the config for using the reva token manager type TokenManager struct { - JWTSecret string `ocisConfig:"jwt_secret"` + JWTSecret string `ocisConfig:"jwt_secret" env:"OCIS_JWT_SECRET"` } const ( @@ -43,9 +35,9 @@ type Mode int // Runtime configures the oCIS runtime when running in supervised mode. type Runtime struct { - Port string `ocisConfig:"port"` - Host string `ocisConfig:"host"` - Extensions string `ocisConfig:"extensions"` + Port string `ocisConfig:"port" env:"OCIS_RUNTIME_PORT"` + Host string `ocisConfig:"host" env:"OCIS_RUNTIME_HOST"` + Extensions string `ocisConfig:"extensions" env:"OCIS_RUN_EXTENSIONS"` } // Config combines all available configuration parts. @@ -77,100 +69,3 @@ type Config struct { Thumbnails *thumbnails.Config `ocisConfig:"thumbnails"` WebDAV *webdav.Config `ocisConfig:"webdav"` } - -func DefaultConfig() *Config { - return &Config{ - TokenManager: TokenManager{ - JWTSecret: "Pive-Fumkiu4", - }, - Runtime: Runtime{ - Port: "9250", - Host: "localhost", - }, - Accounts: accounts.DefaultConfig(), - GLAuth: glauth.DefaultConfig(), - Graph: graph.DefaultConfig(), - IDP: idp.DefaultConfig(), - Proxy: proxy.DefaultConfig(), - GraphExplorer: graphExplorer.DefaultConfig(), - OCS: ocs.DefaultConfig(), - Settings: settings.DefaultConfig(), - Web: web.DefaultConfig(), - Store: store.DefaultConfig(), - Thumbnails: thumbnails.DefaultConfig(), - WebDAV: webdav.DefaultConfig(), - Storage: storage.DefaultConfig(), - } -} - -// 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 -} - -// 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) -} - -func structMappings(cfg *Config) []shared.EnvBinding { - return []shared.EnvBinding{ - // TODO: transform this too - { - 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, - }, - { - EnvVars: []string{"OCIS_LOG_FILE"}, - Destination: &cfg.Log.File, - }, - { - EnvVars: []string{"OCIS_TRACING_ENABLED"}, - Destination: &cfg.Tracing.Enabled, - }, - { - EnvVars: []string{"OCIS_TRACING_TYPE"}, - Destination: &cfg.Tracing.Type, - }, - { - EnvVars: []string{"OCIS_TRACING_ENDPOINT"}, - Destination: &cfg.Tracing.Endpoint, - }, - { - EnvVars: []string{"OCIS_TRACING_COLLECTOR"}, - Destination: &cfg.Tracing.Collector, - }, - { - EnvVars: []string{"OCIS_JWT_SECRET"}, - Destination: &cfg.TokenManager.JWTSecret, - }, - { - EnvVars: []string{"OCIS_RUNTIME_PORT"}, - Destination: &cfg.Runtime.Port, - }, - { - EnvVars: []string{"OCIS_RUNTIME_HOST"}, - Destination: &cfg.Runtime.Host, - }, - { - EnvVars: []string{"OCIS_RUN_EXTENSIONS"}, - Destination: &cfg.Runtime.Extensions, - }, - } -} diff --git a/ocis-pkg/config/defaultconfig.go b/ocis-pkg/config/defaultconfig.go new file mode 100644 index 000000000..21cf14ba0 --- /dev/null +++ b/ocis-pkg/config/defaultconfig.go @@ -0,0 +1,42 @@ +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" + 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" +) + +func DefaultConfig() *Config { + return &Config{ + TokenManager: TokenManager{ + JWTSecret: "Pive-Fumkiu4", + }, + Runtime: Runtime{ + Port: "9250", + Host: "localhost", + }, + Accounts: accounts.DefaultConfig(), + GLAuth: glauth.DefaultConfig(), + Graph: graph.DefaultConfig(), + IDP: idp.DefaultConfig(), + Proxy: proxy.DefaultConfig(), + GraphExplorer: graphExplorer.DefaultConfig(), + OCS: ocs.DefaultConfig(), + Settings: settings.DefaultConfig(), + Web: web.DefaultConfig(), + Store: store.DefaultConfig(), + Thumbnails: thumbnails.DefaultConfig(), + WebDAV: webdav.DefaultConfig(), + Storage: storage.DefaultConfig(), + } +} diff --git a/ocis-pkg/shared/shared_types.go b/ocis-pkg/shared/shared_types.go index 25aa96269..268c596b0 100644 --- a/ocis-pkg/shared/shared_types.go +++ b/ocis-pkg/shared/shared_types.go @@ -10,24 +10,24 @@ type EnvBinding struct { // 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"` + Level string `ocisConfig:"level" env:"OCIS_LOG_LEVEL"` + Pretty bool `ocisConfig:"pretty" env:"OCIS_LOG_PRETTY"` + Color bool `ocisConfig:"color" env:"OCIS_LOG_COLOR"` + File string `ocisConfig:"file" env:"OCIS_LOG_FILE"` } // Tracing defines the available tracing configuration. type Tracing struct { - Enabled bool `ocisConfig:"enabled"` - Type string `ocisConfig:"type"` - Endpoint string `ocisConfig:"endpoint"` - Collector string `ocisConfig:"collector"` + Enabled bool `ocisConfig:"enabled" env:"OCIS_TRACING_ENABLED"` + Type string `ocisConfig:"type" env:"OCIS_TRACING_TYPE"` + Endpoint string `ocisConfig:"endpoint" env:"OCIS_TRACING_ENDPOINT"` + Collector string `ocisConfig:"collector" env:"OCIS_TRACING_COLLECTOR"` } // Commons holds configuration that are common to all extensions. Each extension can then decide whether // to overwrite its values. type Commons struct { - *Log `mapstructure:"log"` - Tracing `mapstrucuture:"log"` - OcisURL string `mapstructure:"ocis_url"` + Log *Log `ocisConfig:"log"` + Tracing *Tracing `ocisConfig:"tracing"` + OcisURL string `ocisConfig:"ocis_url" env:"OCIS_URL"` } diff --git a/ocis/pkg/command/root.go b/ocis/pkg/command/root.go index ffcbba1b6..09d7e63fb 100644 --- a/ocis/pkg/command/root.go +++ b/ocis/pkg/command/root.go @@ -8,6 +8,7 @@ import ( "github.com/owncloud/ocis/ocis-pkg/version" "github.com/owncloud/ocis/ocis/pkg/register" "github.com/urfave/cli/v2" + "github.com/wkloucek/envdecode" ) // Execute is the entry point for the ocis command. @@ -55,15 +56,15 @@ func Execute() error { // ParseConfig loads ocis configuration. func ParseConfig(c *cli.Context, cfg *config.Config) error { - conf, err := ociscfg.BindSourcesToStructs("ocis", cfg) + _, err := ociscfg.BindSourcesToStructs("ocis", cfg) if err != nil { return err } - // TODO: use envconfig here too + // load all env variables relevant to the config in the current context. + if err := envdecode.Decode(cfg); err != nil && err.Error() != "none of the target fields were set from environment variables" { + return err + } - conf.LoadOSEnv(config.GetEnv(), false) - - bindings := config.StructMappings(cfg) - return ociscfg.BindEnv(conf, bindings) + return nil } diff --git a/ocis/pkg/runtime/service/service.go b/ocis/pkg/runtime/service/service.go index c66b07c3f..fa7c68586 100644 --- a/ocis/pkg/runtime/service/service.go +++ b/ocis/pkg/runtime/service/service.go @@ -168,10 +168,10 @@ func Start(o ...Option) error { s.cfg.Storage.Log = &shared.Log{} } - s.cfg.Storage.Log.Color = s.cfg.Commons.Color - s.cfg.Storage.Log.Level = s.cfg.Commons.Level - s.cfg.Storage.Log.Pretty = s.cfg.Commons.Pretty - s.cfg.Storage.Log.File = s.cfg.Commons.File + s.cfg.Storage.Log.Color = s.cfg.Commons.Log.Color + s.cfg.Storage.Log.Level = s.cfg.Commons.Log.Level + s.cfg.Storage.Log.Pretty = s.cfg.Commons.Log.Pretty + s.cfg.Storage.Log.File = s.cfg.Commons.Log.File if err = rpc.Register(s); err != nil { if s != nil {