diff options
author | Sebastien Douheret <sebastien.douheret@iot.bzh> | 2017-11-24 17:46:24 +0100 |
---|---|---|
committer | Sebastien Douheret <sebastien.douheret@iot.bzh> | 2017-11-24 17:46:24 +0100 |
commit | 5a34a70b861c9d504e9f07d3c8251c36dc2ce76c (patch) | |
tree | 0bb0b82e9652c345ddefcb4c8bbdf35e9f2a346e | |
parent | 3280e5a9412220806d0931c1fbe2515a041603ac (diff) |
Fixed config file option or env var detection.
-rw-r--r-- | glide.yaml | 2 | ||||
-rw-r--r-- | main.go | 55 |
2 files changed, 37 insertions, 20 deletions
@@ -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 @@ -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) |