inherit logging config from ocis.yaml, let extensions overwrite their logging. Considering using a package level global logging...

This commit is contained in:
A.Unger
2021-10-31 00:08:46 +02:00
parent 998df71ae3
commit 711acbb354
21 changed files with 180 additions and 126 deletions

View File

@@ -0,0 +1,4 @@
log:
pretty: true
color: true
level: info

View File

@@ -2,18 +2,16 @@ package command
import (
"os"
"strings"
"github.com/owncloud/ocis/ocis-pkg/sync"
gofig "github.com/gookit/config/v2"
gooyaml "github.com/gookit/config/v2/yaml"
"github.com/owncloud/ocis/ocis-pkg/log"
"github.com/urfave/cli/v2"
"github.com/owncloud/ocis/ocis-pkg/config"
"github.com/owncloud/ocis/ocis-pkg/version"
"github.com/owncloud/ocis/ocis/pkg/flagset"
"github.com/owncloud/ocis/ocis/pkg/register"
"github.com/spf13/viper"
)
// Execute is the entry point for the ocis command.
@@ -25,18 +23,15 @@ func Execute() error {
Version: version.String,
Usage: "ownCloud Infinite Scale Stack",
Compiled: version.Compiled(),
Before: func(c *cli.Context) error {
return ParseConfig(c, cfg)
},
Authors: []*cli.Author{
{
Name: "ownCloud GmbH",
Email: "support@owncloud.com",
},
},
Flags: flagset.RootWithConfig(cfg),
}
for _, fn := range register.Commands {
@@ -70,47 +65,16 @@ func NewLogger(cfg *config.Config) log.Logger {
)
}
// ParseConfig load configuration for every extension
// ParseConfig loads ocis 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("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")
cnf := gofig.NewWithOptions("ocis", gofig.ParseEnv)
cnf.AddDriver(gooyaml.Driver)
err := cnf.LoadFiles("/Users/aunger/code/owncloud/ocis/ocis/pkg/command/ocis_example_config.yaml")
if err != nil {
return err
}
if err := viper.ReadInConfig(); err != nil {
switch err.(type) {
case viper.ConfigFileNotFoundError:
logger.Debug().
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")
}
err = cnf.BindStruct("", cfg)
return nil
}