diff --git a/ocis/go.mod b/ocis/go.mod index d3651143c1..468bb869bc 100644 --- a/ocis/go.mod +++ b/ocis/go.mod @@ -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 ) diff --git a/ocis/pkg/command/root.go b/ocis/pkg/command/root.go index 66b556e68c..480748015f 100644 --- a/ocis/pkg/command/root.go +++ b/ocis/pkg/command/root.go @@ -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 +}