switch graph to struct tag based env config

This commit is contained in:
Willy Kloucek
2021-12-16 19:40:23 +01:00
parent 4067ae9493
commit 30656c5a32
8 changed files with 92 additions and 235 deletions
+24 -15
View File
@@ -4,9 +4,9 @@ import (
"context"
"os"
"github.com/owncloud/ocis/ocis-pkg/shared"
"github.com/imdario/mergo"
"github.com/thejerf/suture/v4"
"github.com/wkloucek/envdecode"
"github.com/owncloud/ocis/graph/pkg/config"
ociscfg "github.com/owncloud/ocis/ocis-pkg/config"
@@ -62,26 +62,35 @@ func NewLogger(cfg *config.Config) log.Logger {
// ParseConfig loads graph configuration from known paths.
func ParseConfig(c *cli.Context, cfg *config.Config) error {
conf, err := ociscfg.BindSourcesToStructs("graph", cfg)
_, err := ociscfg.BindSourcesToStructs(cfg.Service.Name, cfg)
if err != nil {
return err
}
// provide with defaults for shared logging, since we need a valid destination address for BindEnv.
if cfg.Log == nil && cfg.Commons != nil && cfg.Commons.Log != nil {
cfg.Log = &shared.Log{
Level: cfg.Commons.Log.Level,
Pretty: cfg.Commons.Log.Pretty,
Color: cfg.Commons.Log.Color,
File: cfg.Commons.Log.File,
}
} else if cfg.Log == nil && cfg.Commons == nil {
cfg.Log = &shared.Log{}
//if cfg.Log == nil && cfg.Commons != nil && cfg.Commons.Log != nil {
// cfg.Log = &shared.Log{
// Level: cfg.Commons.Log.Level,
// Pretty: cfg.Commons.Log.Pretty,
// Color: cfg.Commons.Log.Color,
// File: cfg.Commons.Log.File,
// }
//} else if cfg.Log == nil && cfg.Commons == nil {
// cfg.Log = &shared.Log{}
//}
// load all env variables relevant to the config in the current context.
envCfg := config.Config{}
if err := envdecode.Decode(&envCfg); err != nil && err.Error() != "none of the target fields were set from environment variables" {
return err
}
conf.LoadOSEnv(config.GetEnv(cfg), false)
bindings := config.StructMappings(cfg)
return ociscfg.BindEnv(conf, bindings)
// merge environment variable config on top of the current config
if err := mergo.Merge(cfg, envCfg, mergo.WithOverride); err != nil {
return err
}
return nil
}
// SutureService allows for the graph command to be embedded and supervised by a suture supervisor tree.
+4 -3
View File
@@ -6,6 +6,7 @@ import (
"github.com/oklog/run"
"github.com/owncloud/ocis/graph/pkg/config"
"github.com/owncloud/ocis/graph/pkg/logging"
"github.com/owncloud/ocis/graph/pkg/metrics"
"github.com/owncloud/ocis/graph/pkg/server/debug"
"github.com/owncloud/ocis/graph/pkg/server/http"
@@ -30,9 +31,9 @@ func Server(cfg *config.Config) *cli.Command {
return nil
},
Action: func(c *cli.Context) error {
logger := NewLogger(cfg)
if err := tracing.Configure(cfg); err != nil {
logger := logging.Configure(cfg.Service.Name, cfg.Log)
err := tracing.Configure(cfg)
if err != nil {
return err
}