migrate graph-explorer to the new config scheme

This commit is contained in:
A.Unger
2021-11-05 11:31:35 +01:00
parent ba1635165b
commit 91c766210c
8 changed files with 177 additions and 180 deletions
+8 -42
View File
@@ -3,14 +3,11 @@ package command
import (
"context"
"os"
"strings"
"github.com/owncloud/ocis/graph-explorer/pkg/config"
ociscfg "github.com/owncloud/ocis/ocis-pkg/config"
"github.com/owncloud/ocis/ocis-pkg/log"
"github.com/owncloud/ocis/ocis-pkg/sync"
"github.com/owncloud/ocis/ocis-pkg/version"
"github.com/spf13/viper"
"github.com/thejerf/suture/v4"
"github.com/urfave/cli/v2"
)
@@ -29,9 +26,6 @@ func Execute(cfg *config.Config) error {
Email: "support@owncloud.com",
},
},
//Flags: flagset.RootWithConfig(cfg),
Before: func(c *cli.Context) error {
cfg.Server.Version = version.String
return ParseConfig(c, cfg)
@@ -67,46 +61,18 @@ func NewLogger(cfg *config.Config) log.Logger {
)
}
// ParseConfig loads accounts configuration from Viper known paths.
// ParseConfig loads graph configuration from known paths.
func ParseConfig(c *cli.Context, cfg *config.Config) error {
sync.ParsingViperConfig.Lock()
defer sync.ParsingViperConfig.Unlock()
logger := NewLogger(cfg)
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
viper.SetEnvPrefix("GRAPH_EXPLORER")
viper.AutomaticEnv()
if c.IsSet("config-file") {
viper.SetConfigFile(c.String("config-file"))
} else {
viper.SetConfigName("graph-explorer")
viper.AddConfigPath("/etc/ocis")
viper.AddConfigPath("$HOME/.ocis")
viper.AddConfigPath("./config")
conf, err := ociscfg.BindSourcesToStructs("graph-explorer", cfg)
if err != nil {
return err
}
if err := viper.ReadInConfig(); err != nil {
switch err.(type) {
case viper.ConfigFileNotFoundError:
//logger.Info().
// Msg("Continue without config")
case viper.UnsupportedConfigError:
logger.Fatal().
Err(err).
Msg("Unsupported config type")
default:
logger.Fatal().
Err(err).
Msg("Failed to read config")
}
}
// load all env variables relevant to the config in the current context.
conf.LoadOSEnv(config.GetEnv(), false)
if err := viper.Unmarshal(&cfg); err != nil {
logger.Fatal().
Err(err).
Msg("Failed to parse config")
if err = cfg.UnmapEnv(conf); err != nil {
return err
}
return nil
+3 -8
View File
@@ -6,7 +6,6 @@ import (
"github.com/oklog/run"
"github.com/owncloud/ocis/graph-explorer/pkg/config"
"github.com/owncloud/ocis/graph-explorer/pkg/flagset"
"github.com/owncloud/ocis/graph-explorer/pkg/metrics"
"github.com/owncloud/ocis/graph-explorer/pkg/server/debug"
"github.com/owncloud/ocis/graph-explorer/pkg/server/http"
@@ -20,19 +19,15 @@ func Server(cfg *config.Config) *cli.Command {
return &cli.Command{
Name: "server",
Usage: "Start integrated server",
Flags: flagset.ServerWithConfig(cfg),
Before: func(ctx *cli.Context) error {
logger := NewLogger(cfg)
if cfg.HTTP.Root != "/" {
cfg.HTTP.Root = strings.TrimSuffix(cfg.HTTP.Root, "/")
}
// When running on single binary mode the before hook from the root command won't get called. We manually
// call this before hook from ocis command, so the configuration can be loaded.
if !cfg.Supervised {
return ParseConfig(ctx, cfg)
if err := ParseConfig(ctx, cfg); err != nil {
return err
}
logger.Debug().Str("service", "graph-explorer").Msg("ignoring config file parsing when running supervised")
return nil
},
Action: func(c *cli.Context) error {