From: Sebastien Douheret Date: Mon, 6 Nov 2017 15:51:23 +0000 (+0100) Subject: Fixed env vars setting from config file. X-Git-Tag: v0.1.0~12 X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?p=src%2Fxds%2Fxds-cli.git;a=commitdiff_plain;h=0df439bb10fe1f178b32aff8467e9671888eac4b Fixed env vars setting from config file. --- diff --git a/cmd-exec.go b/cmd-exec.go index 612851f..d5b6466 100644 --- a/cmd-exec.go +++ b/cmd-exec.go @@ -6,8 +6,6 @@ import ( "strings" "github.com/iotbzh/xds-agent/lib/apiv1" - common "github.com/iotbzh/xds-common/golib" - "github.com/joho/godotenv" "github.com/urfave/cli" ) @@ -38,7 +36,6 @@ func initCmdExec(cmdDef *[]cli.Command) { func exec(ctx *cli.Context) error { prjID := ctx.String("id") - confFile := ctx.String("config") rPath := ctx.String("rPath") sdkid := ctx.String("sdkid") @@ -47,23 +44,6 @@ func exec(ctx *cli.Context) error { return cli.NewExitError("project id must be set (see --id option)", 1) } - // Load config file if requested - envMap := make(map[string]string) - 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) - } - envMap, err = godotenv.Read(confFile) - if err != nil { - exitError(1, "Error reading env config file "+confFile) - } - } - argsCommand := make([]string, len(ctx.Args())) copy(argsCommand, ctx.Args()) Log.Infof("Execute: /exec %v", argsCommand) @@ -128,9 +108,9 @@ func exec(ctx *cli.Context) error { } // Build env - Log.Debugf("Command env: %v", envMap) + Log.Debugf("Command env: %v", EnvConfFileMap) env := []string{} - for k, v := range envMap { + for k, v := range EnvConfFileMap { env = append(env, k+"="+v) } diff --git a/main.go b/main.go index c8d2095..508bbdd 100644 --- a/main.go +++ b/main.go @@ -11,6 +11,7 @@ import ( "github.com/Sirupsen/logrus" common "github.com/iotbzh/xds-common/golib" + "github.com/joho/godotenv" socketio_client "github.com/sebd71/go-socket.io-client" "github.com/urfave/cli" ) @@ -43,6 +44,9 @@ const ( // Log Global variable that hold logger var Log = logrus.New() +// EnvConfFileMap Global variable that hold environment vars loaded from config file +var EnvConfFileMap map[string]string + // HTTPCli Global variable that hold HTTP Client var HTTPCli *common.HTTPClient @@ -58,6 +62,7 @@ func exitError(code int, f string, a ...interface{}) { // main func main() { + EnvConfFileMap := make(map[string]string) // Allow to set app name from cli (useful for debugging) if AppName == "" { @@ -154,6 +159,25 @@ func main() { app.Before = func(ctx *cli.Context) error { var err error + + // 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 { @@ -163,7 +187,7 @@ func main() { Log.Formatter = &logrus.TextFormatter{} Log.Infof("%s version: %s", AppName, app.Version) - // SEB Add again Log.Debugf("Environment: %v", os.Environ()) + Log.Debugf("Environment: %v", os.Environ()) if err = XdsConnInit(ctx); err != nil { // Directly call HandleExitCoder to avoid to print help (ShowAppHelp)