From 5a34a70b861c9d504e9f07d3c8251c36dc2ce76c Mon Sep 17 00:00:00 2001 From: Sebastien Douheret Date: Fri, 24 Nov 2017 17:46:24 +0100 Subject: [PATCH] Fixed config file option or env var detection. --- glide.yaml | 2 +- main.go | 55 ++++++++++++++++++++++++++++++++++++------------------- 2 files changed, 37 insertions(+), 20 deletions(-) diff --git a/glide.yaml b/glide.yaml index ff5e391..bc1a3f0 100644 --- a/glide.yaml +++ b/glide.yaml @@ -12,7 +12,7 @@ import: - package: github.com/sebd71/go-socket.io-client version: 46defcb47f - package: github.com/iotbzh/xds-agent - version: c51d5034d527578 + version: 4e9af3723740f16f subpackages: - agent - package: github.com/iotbzh/xds-common diff --git a/main.go b/main.go index 090144d..b7f110e 100644 --- a/main.go +++ b/main.go @@ -62,6 +62,8 @@ func exitError(code int, f string, a ...interface{}) { // main func main() { + var earlyDebug []string + EnvConfFileMap := make(map[string]string) // Allow to set app name from cli (useful for debugging) @@ -166,6 +168,36 @@ func main() { sort.Sort(cli.FlagsByName(app.Flags)) sort.Sort(cli.CommandsByName(app.Commands)) + // Early and manual processing of --config option in order to set XDS_xxx + // variables before parsing of option by app cli + confFile := os.Getenv("XDS_CONFIG") + for idx, a := range os.Args[1:] { + if a == "-c" || a == "--config" || a == "-config" { + confFile = os.Args[idx+2] + break + } + } + + // Load config file if requested + if confFile != "" { + earlyDebug = append(earlyDebug, fmt.Sprintf("confFile detected: %v", confFile)) + if !common.Exists(confFile) { + exitError(1, "Error env config file not found") + } + // Load config file variables that will overwrite env variables + err := godotenv.Overload(confFile) + if err != nil { + exitError(1, "Error loading env config file "+confFile) + } + + // Keep confFile settings in a map + EnvConfFileMap, err = godotenv.Read(confFile) + if err != nil { + exitError(1, "Error reading env config file "+confFile) + } + earlyDebug = append(earlyDebug, fmt.Sprintf("EnvConfFileMap: %v", EnvConfFileMap)) + } + app.Before = func(ctx *cli.Context) error { var err error @@ -180,24 +212,6 @@ func main() { } } - // Load config file if requested - confFile := ctx.String("config") - if confFile != "" { - if !common.Exists(confFile) { - exitError(1, "Error env config file not found") - } - // Load config file variables that will overwrite env variables - err := godotenv.Overload(confFile) - if err != nil { - exitError(1, "Error loading env config file "+confFile) - } - // Keep confFile settings in a map - EnvConfFileMap, err = godotenv.Read(confFile) - if err != nil { - exitError(1, "Error reading env config file "+confFile) - } - } - loglevel := ctx.String("log") // Set logger level and formatter if Log.Level, err = logrus.ParseLevel(loglevel); err != nil { @@ -207,7 +221,10 @@ func main() { Log.Formatter = &logrus.TextFormatter{} Log.Infof("%s version: %s", AppName, app.Version) - Log.Debugf("Environment: %v", os.Environ()) + for _, str := range earlyDebug { + Log.Infof("%s", str) + } + Log.Debugf("\nEnvironment: %v\n", os.Environ()) if err = XdsConnInit(ctx); err != nil { // Directly call HandleExitCoder to avoid to print help (ShowAppHelp) -- 2.16.6