parse ocis root config down to subcommands

This commit is contained in:
A.Unger
2020-11-05 11:28:42 +01:00
parent 5f1373b9ed
commit fa7f137c04
2 changed files with 50 additions and 0 deletions

View File

@@ -30,6 +30,7 @@ require (
github.com/owncloud/ocis/webdav v0.0.0-00010101000000-000000000000
github.com/refs/pman v0.0.0-20200701173654-f05b8833071a
github.com/restic/calens v0.2.0
github.com/spf13/viper v1.7.1
go.opencensus.io v0.22.5
)

View File

@@ -2,6 +2,7 @@ package command
import (
"os"
"strings"
"github.com/micro/cli/v2"
"github.com/owncloud/ocis/ocis-pkg/log"
@@ -10,6 +11,7 @@ import (
"github.com/owncloud/ocis/ocis/pkg/register"
"github.com/owncloud/ocis/ocis/pkg/runtime"
"github.com/owncloud/ocis/ocis/pkg/version"
"github.com/spf13/viper"
)
// Execute is the entry point for the ocis-ocis command.
@@ -22,6 +24,10 @@ func Execute() error {
Usage: "ownCloud Infinite Scale Stack",
Compiled: version.Compiled(),
Before: func(c *cli.Context) error {
return ParseConfig(c, cfg)
},
Authors: []*cli.Author{
{
Name: "ownCloud GmbH",
@@ -62,3 +68,46 @@ func NewLogger(cfg *config.Config) log.Logger {
log.Color(cfg.Log.Color),
)
}
// ParseConfig load configuration for every extension
func ParseConfig(c *cli.Context, cfg *config.Config) error {
logger := NewLogger(cfg)
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
viper.SetEnvPrefix("OCIS")
viper.AutomaticEnv()
if c.IsSet("config-file") {
viper.SetConfigFile(c.String("config-file"))
} else {
viper.SetConfigName("ocis")
viper.AddConfigPath("/etc/ocis")
viper.AddConfigPath("$HOME/.ocis")
viper.AddConfigPath("./config")
}
if err := viper.ReadInConfig(); err != nil {
switch err.(type) {
case viper.ConfigFileNotFoundError:
logger.Info().
Msg("no config found on preconfigured location")
case viper.UnsupportedConfigError:
logger.Fatal().
Err(err).
Msg("unsupported config type")
default:
logger.Fatal().
Err(err).
Msg("failed to read config")
}
}
if err := viper.Unmarshal(&cfg); err != nil {
logger.Fatal().
Err(err).
Msg("failed to parse config")
}
return nil
}