From 22601de35185bfd46ea307631dbe4782607dc1f3 Mon Sep 17 00:00:00 2001 From: "A.Unger" Date: Mon, 8 Nov 2021 12:08:35 +0100 Subject: [PATCH] normalize graph-explorer --- graph-explorer/pkg/command/root.go | 29 ++----------------- graph-explorer/pkg/command/server.go | 7 +---- graph-explorer/pkg/config/config.go | 13 ++------- .../pkg/config/{env.go => mappings.go} | 14 +++++---- graph/pkg/command/root.go | 5 ---- graph/pkg/config/config.go | 8 ----- 6 files changed, 16 insertions(+), 60 deletions(-) rename graph-explorer/pkg/config/{env.go => mappings.go} (82%) diff --git a/graph-explorer/pkg/command/root.go b/graph-explorer/pkg/command/root.go index 3ebaa4422..2f5f679a4 100644 --- a/graph-explorer/pkg/command/root.go +++ b/graph-explorer/pkg/command/root.go @@ -19,7 +19,6 @@ func Execute(cfg *config.Config) error { Version: version.String, Usage: "Serve Graph-Explorer for oCIS", Compiled: version.Compiled(), - Authors: []*cli.Author{ { Name: "ownCloud GmbH", @@ -30,18 +29,15 @@ func Execute(cfg *config.Config) error { cfg.Server.Version = version.String return ParseConfig(c, cfg) }, - Commands: []*cli.Command{ Server(cfg), Health(cfg), }, } - cli.HelpFlag = &cli.BoolFlag{ Name: "help,h", Usage: "Show the help", } - cli.VersionFlag = &cli.BoolFlag{ Name: "version,v", Usage: "Print the version", @@ -68,14 +64,9 @@ func ParseConfig(c *cli.Context, cfg *config.Config) error { return err } - // 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 graph-explorer command to be embedded and supervised by a suture supervisor tree. @@ -85,11 +76,7 @@ type SutureService struct { // NewSutureService creates a new graph-explorer.SutureService func NewSutureService(cfg *ociscfg.Config) suture.Service { - inheritLogging(cfg) - if cfg.Mode == 0 { - cfg.GraphExplorer.Supervised = true - } - cfg.GraphExplorer.Log.File = cfg.Log.File + cfg.GraphExplorer.Log = cfg.Log return SutureService{ cfg: cfg.GraphExplorer, } @@ -103,13 +90,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.GraphExplorer.Log.File = cfg.Log.File - cfg.GraphExplorer.Log.Color = cfg.Log.Color - cfg.GraphExplorer.Log.Pretty = cfg.Log.Pretty - cfg.GraphExplorer.Log.Level = cfg.Log.Level -} diff --git a/graph-explorer/pkg/command/server.go b/graph-explorer/pkg/command/server.go index 773a8b61f..f97daa3b8 100644 --- a/graph-explorer/pkg/command/server.go +++ b/graph-explorer/pkg/command/server.go @@ -24,15 +24,10 @@ func Server(cfg *config.Config) *cli.Command { cfg.HTTP.Root = strings.TrimSuffix(cfg.HTTP.Root, "/") } - if err := ParseConfig(ctx, cfg); err != nil { - return err - } - - return nil + return ParseConfig(ctx, cfg) }, Action: func(c *cli.Context) error { logger := NewLogger(cfg) - tracing.Configure(cfg) var ( diff --git a/graph-explorer/pkg/config/config.go b/graph-explorer/pkg/config/config.go index 3a52e857d..558273a68 100644 --- a/graph-explorer/pkg/config/config.go +++ b/graph-explorer/pkg/config/config.go @@ -6,16 +6,9 @@ import ( "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,7 +50,7 @@ type GraphExplorer struct { // Config combines all available configuration parts. type Config struct { File string `mapstructure:"file"` - Log Log `mapstructure:"log"` + Log shared.Log `mapstructure:"log"` Debug Debug `mapstructure:"debug"` HTTP HTTP `mapstructure:"http"` Server Server `mapstructure:"server"` @@ -76,7 +69,7 @@ func New() *Config { // DefaultConfig provides with a working version of a config. func DefaultConfig() *Config { return &Config{ - Log: Log{}, + Log: shared.Log{}, Debug: Debug{ Addr: "127.0.0.1:9136", Token: "", diff --git a/graph-explorer/pkg/config/env.go b/graph-explorer/pkg/config/mappings.go similarity index 82% rename from graph-explorer/pkg/config/env.go rename to graph-explorer/pkg/config/mappings.go index e5dd435e8..09173763e 100644 --- a/graph-explorer/pkg/config/env.go +++ b/graph-explorer/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{"GRAPH_EXPLORER_LOG_LEVEL", "OCIS_LOG_LEVEL"}, Destination: &cfg.Log.Level, diff --git a/graph/pkg/command/root.go b/graph/pkg/command/root.go index 11cfdabb7..094736e3b 100644 --- a/graph/pkg/command/root.go +++ b/graph/pkg/command/root.go @@ -64,14 +64,9 @@ func ParseConfig(c *cli.Context, cfg *config.Config) error { if err != nil { return err } - - // load all env variables relevant to the config in the current context. conf.LoadOSEnv(config.GetEnv(), false) - bindings := config.StructMappings(cfg) return ociscfg.BindEnv(conf, bindings) - - return nil } // SutureService allows for the graph command to be embedded and supervised by a suture supervisor tree. diff --git a/graph/pkg/config/config.go b/graph/pkg/config/config.go index 3d35692c7..9bfb29167 100644 --- a/graph/pkg/config/config.go +++ b/graph/pkg/config/config.go @@ -9,14 +9,6 @@ import ( "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"`