Merge branch 'master' into thumbnail-api

This commit is contained in:
David Christofas
2020-03-19 10:03:09 +01:00
committed by GitHub
4 changed files with 58 additions and 40 deletions

View File

@@ -5,6 +5,7 @@ The following sections list the changes for unreleased.
## Summary
* Chg #1: Initial release of basic version
* Enh #14: Configuration
## Details
@@ -15,4 +16,11 @@ The following sections list the changes for unreleased.
https://github.com/owncloud/ocis-webdav/issues/1
* Enhancement #14: 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

View File

@@ -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

View File

@@ -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
}

View File

@@ -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)