From f7b7b63e3f30b4d855a26d550ddf726116c65846 Mon Sep 17 00:00:00 2001 From: Frédéric Guillot Date: Sun, 2 Jun 2019 18:20:59 -0700 Subject: Add optional config file parser in addition to environment variables --- cli/cli.go | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) (limited to 'cli') diff --git a/cli/cli.go b/cli/cli.go index ae94aba..eba1ee3 100644 --- a/cli/cli.go +++ b/cli/cli.go @@ -24,11 +24,14 @@ const ( flagResetPasswordHelp = "Reset user password" flagResetFeedErrorsHelp = "Clear all feed errors for all users" flagDebugModeHelp = "Show debug logs" + flagConfigFileHelp = "Load configuration file" + flagConfigDumpHelp = "Print parsed configuration values" ) // Parse parses command line arguments. func Parse() { var ( + err error flagInfo bool flagVersion bool flagMigrate bool @@ -37,6 +40,8 @@ func Parse() { flagResetPassword bool flagResetFeedErrors bool flagDebugMode bool + flagConfigFile string + flagConfigDump bool ) flag.BoolVar(&flagInfo, "info", false, flagInfoHelp) @@ -49,12 +54,30 @@ func Parse() { flag.BoolVar(&flagResetPassword, "reset-password", false, flagResetPasswordHelp) flag.BoolVar(&flagResetFeedErrors, "reset-feed-errors", false, flagResetFeedErrorsHelp) flag.BoolVar(&flagDebugMode, "debug", false, flagDebugModeHelp) + flag.StringVar(&flagConfigFile, "config-file", "", flagConfigFileHelp) + flag.StringVar(&flagConfigFile, "c", "", flagConfigFileHelp) + flag.BoolVar(&flagConfigDump, "config-dump", false, flagConfigDumpHelp) flag.Parse() - if err := config.ParseConfig(); err != nil { + cfg := config.NewParser() + + if flagConfigFile != "" { + config.Opts, err = cfg.ParseFile(flagConfigFile) + if err != nil { + logger.Fatal("%v", err) + } + } + + config.Opts, err = cfg.ParseEnvironmentVariables() + if err != nil { logger.Fatal("%v", err) } + if flagConfigDump { + fmt.Print(config.Opts) + return + } + if flagDebugMode || config.Opts.HasDebugMode() { logger.EnableDebug() } -- cgit v1.2.3