diff --git a/changelog/unreleased/config-management.md b/changelog/unreleased/config-management.md new file mode 100644 index 0000000..ff7f35b --- /dev/null +++ b/changelog/unreleased/config-management.md @@ -0,0 +1,5 @@ +Enhancement: Configuration + +Extensions should be responsible of configuring themselves. We use Viper for config loading from default paths. Environment variables **WILL** take precedence over config files. + +https://github.com/owncloud/ocis-webdav/pull/14 diff --git a/pkg/command/root.go b/pkg/command/root.go index 585add3..128e05d 100644 --- a/pkg/command/root.go +++ b/pkg/command/root.go @@ -32,45 +32,7 @@ func Execute() error { Flags: flagset.RootWithConfig(cfg), Before: func(c *cli.Context) error { - logger := NewLogger(cfg) - - viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_")) - viper.SetEnvPrefix("WEBDAV") - viper.AutomaticEnv() - - if c.IsSet("config-file") { - viper.SetConfigFile(c.String("config-file")) - } else { - viper.SetConfigName("webdav") - - 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("Continue without config") - 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 + return ParseConfig(c, cfg) }, Commands: []*cli.Command{ @@ -101,3 +63,46 @@ func NewLogger(cfg *config.Config) log.Logger { log.Color(cfg.Log.Color), ) } + +// ParseConfig loads webdav configuration from Viper known paths. +func ParseConfig(c *cli.Context, cfg *config.Config) error { + logger := NewLogger(cfg) + + viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_")) + viper.SetEnvPrefix("WEBDAV") + viper.AutomaticEnv() + + if c.IsSet("config-file") { + viper.SetConfigFile(c.String("config-file")) + } else { + viper.SetConfigName("webdav") + + 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("Continue without config") + 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 +} diff --git a/pkg/command/server.go b/pkg/command/server.go index 8a57fab..a898275 100644 --- a/pkg/command/server.go +++ b/pkg/command/server.go @@ -34,7 +34,7 @@ func Server(cfg *config.Config) *cli.Command { cfg.HTTP.Root = strings.TrimSuffix(cfg.HTTP.Root, "/") } - return nil + return ParseConfig(c, cfg) }, Action: func(c *cli.Context) error { logger := NewLogger(cfg)