Files
ocis/pkg/command/root.go
T

130 lines
2.9 KiB
Go
Raw Normal View History

2020-01-30 15:55:40 +01:00
package command
import (
2020-04-29 13:55:13 +02:00
"github.com/owncloud/ocis-accounts/pkg/flagset"
2020-01-30 15:55:40 +01:00
"os"
"os/user"
"path"
2020-03-18 13:11:04 +01:00
"strings"
2020-01-30 15:55:40 +01:00
"github.com/joho/godotenv"
2020-02-04 10:09:38 +01:00
"github.com/micro/cli/v2"
2020-02-04 10:16:12 +01:00
"github.com/owncloud/ocis-accounts/pkg/config"
2020-04-09 06:48:59 +02:00
"github.com/owncloud/ocis-accounts/pkg/version"
2020-03-18 13:57:30 +01:00
"github.com/owncloud/ocis-pkg/v2/log"
2020-03-18 13:11:04 +01:00
"github.com/spf13/viper"
2020-02-04 10:16:12 +01:00
// init store manager
_ "github.com/owncloud/ocis-accounts/pkg/store"
2020-01-30 15:55:40 +01:00
)
var (
defaultConfigPaths = []string{"/etc/ocis", "$HOME/.ocis", "./config"}
defaultFilename = "accounts"
)
2020-01-30 15:55:40 +01:00
// Execute is the entry point for the ocis-accounts command.
func Execute() error {
2020-04-29 13:55:13 +02:00
rootCfg := config.New()
2020-01-30 15:55:40 +01:00
app := &cli.App{
Name: "ocis-accounts",
Version: version.String,
Usage: "Example service for Reva/oCIS",
2020-04-29 13:55:13 +02:00
Flags: flagset.RootWithConfig(rootCfg),
Before: func(c *cli.Context) error {
2020-04-29 13:55:13 +02:00
logger := NewLogger(config.New())
for _, v := range defaultConfigPaths {
// location is the user's home
if v[0] == '$' || v[0] == '~' {
usr, _ := user.Current()
err := godotenv.Load(path.Join(usr.HomeDir, ".ocis", defaultFilename+".env"))
if err != nil {
2020-04-29 13:55:13 +02:00
logger.Debug().Msgf("ignoring missing env file on dir: %v", v)
}
} else {
err := godotenv.Load(path.Join(v, defaultFilename+".env"))
if err != nil {
2020-04-29 13:55:13 +02:00
logger.Debug().Msgf("ignoring missing env file on dir: %v", v)
}
}
}
return nil
},
2020-02-04 10:09:38 +01:00
Authors: []*cli.Author{
2020-01-30 15:55:40 +01:00
{
Name: "ownCloud GmbH",
Email: "support@owncloud.com",
},
},
2020-02-04 10:09:38 +01:00
Commands: []*cli.Command{
2020-04-29 13:55:13 +02:00
Server(rootCfg),
2020-01-30 15:55:40 +01:00
},
}
cli.HelpFlag = &cli.BoolFlag{
Name: "help,h",
Usage: "Show the help",
}
cli.VersionFlag = &cli.BoolFlag{
Name: "version,v",
Usage: "Print the version",
}
return app.Run(os.Args)
}
2020-03-18 13:11:04 +01:00
// NewLogger initializes a service-specific logger instance.
func NewLogger(cfg *config.Config) log.Logger {
return log.NewLogger(
2020-03-18 14:15:11 +01:00
log.Name("accounts"),
2020-04-29 13:55:13 +02:00
log.Level(cfg.Log.Level),
log.Pretty(cfg.Log.Pretty),
log.Color(cfg.Log.Color),
2020-03-18 13:11:04 +01:00
)
}
2020-03-18 14:29:47 +01:00
// ParseConfig loads accounts configuration from Viper known paths.
2020-03-18 13:11:04 +01:00
func ParseConfig(c *cli.Context, cfg *config.Config) error {
logger := NewLogger(cfg)
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
viper.SetEnvPrefix("ACCOUNTS")
2020-03-18 13:11:04 +01:00
viper.AutomaticEnv()
if c.IsSet("config-file") {
viper.SetConfigFile(c.String("config-file"))
} else {
viper.SetConfigName(defaultFilename)
2020-03-18 13:11:04 +01:00
for _, v := range defaultConfigPaths {
viper.AddConfigPath(v)
}
2020-03-18 13:11:04 +01:00
}
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
}