X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;f=main.go;h=090144d9bab0e96ae23c0ad9ca758dae73930eb1;hb=1f754944c23df02b64aeb591d502b36ad314c5eb;hp=c8d20958894964fc6c0421dae4ced0ceb6c95c6c;hpb=c35d7a0fc8bbb1f9123bb41a7b66e45ea2564dd2;p=src%2Fxds%2Fxds-cli.git diff --git a/main.go b/main.go index c8d2095..090144d 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 == "" { @@ -71,16 +76,25 @@ func main() { } appUsage := fmt.Sprintf("command line tool for X(cross) Development System.") appDescription := fmt.Sprintf("%s utility for X(cross) Development System\n", AppName) - /* SEB UPDATE DOC - appDescription += ` - xds-cli configuration is driven either by environment variables or by command line - options or using a config file knowning that the following priority order is used: - 1. use option value (for example use project ID set by --id option), - 2. else use variable 'XDS_xxx' (for example 'XDS_PROJECT_ID' variable) when a - config file is specified with '--config|-c' option, - 3. else use 'XDS_xxx' (for example 'XDS_PROJECT_ID') environment variable. - ` - */ + appDescription += ` + Setting of global options is driven either by environment variables or by command + line options or using a config file knowning that the following priority order is used: + 1. use option value (for example --url option), + 2. else use variable 'XDS_xxx' (for example 'XDS_SERVER_URL' variable) when a + config file is specified with '--config|-c' option, + 3. else use 'XDS_xxx' (for example 'XDS_SERVER_URL') environment variable. + + Examples: + # Get help of 'projects' sub-command + ` + AppName + ` projects --help + + # List all SDKs + ` + AppName + ` sdks ls + + # Add a new project + ` + AppName + ` prj add --label="myProject" --type=cs --path=$HOME/xds-workspace/myProject +` + // Create a new App instance app := cli.NewApp() app.Name = AppName @@ -154,6 +168,36 @@ func main() { app.Before = func(ctx *cli.Context) error { var err error + + // Don't init anything when no argument or help option is set + if ctx.NArg() == 0 { + return nil + } + for _, a := range ctx.Args() { + switch a { + case "-h", "--h", "-help", "--help": + return nil + } + } + + // 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 +207,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) @@ -213,6 +257,7 @@ func XdsConnInit(ctx *cli.Context) error { return cli.NewExitError(errmsg, 1) } HTTPCli.SetLogLevel(ctx.String("loglevel")) + Log.Infoln("HTTP session ID : ", HTTPCli.GetClientID()) // Create io Websocket client Log.Debugln("Connecting IO.socket client on ", baseURL)