X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;f=main.go;h=7d1f5e19762fa8c593a384269538ee0b0366e89e;hb=2231e3eae86c5e3ae05e30f667d331f5875c7884;hp=ad237af8f3478631c2d1822edd0856712734899a;hpb=f888a1c70aaa168e5956f911fd34b7a0a9bc8614;p=src%2Fxds%2Fxds-cli.git diff --git a/main.go b/main.go index ad237af..7d1f5e1 100644 --- a/main.go +++ b/main.go @@ -1,5 +1,5 @@ /* - * Copyright (C) 2017 "IoT.bzh" + * Copyright (C) 2017-2018 "IoT.bzh" * Author Sebastien Douheret * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -28,9 +28,10 @@ import ( "strings" "text/tabwriter" + "gerrit.automotivelinux.org/gerrit/src/xds/xds-agent.git/lib/xaapiv1" + common "gerrit.automotivelinux.org/gerrit/src/xds/xds-common.git/golib" "github.com/Sirupsen/logrus" - "github.com/iotbzh/xds-agent/lib/xaapiv1" - common "github.com/iotbzh/xds-common/golib" + "github.com/joho/godotenv" socketio_client "github.com/sebd71/go-socket.io-client" "github.com/urfave/cli" @@ -57,7 +58,7 @@ var AppSubVersion = "unknown-dev" // Application details const ( - appCopyright = "Copyright (C) 2017 IoT.bzh - Apache-2.0" + appCopyright = "Copyright (C) 2017-2018 IoT.bzh - Apache-2.0" defaultLogLevel = "error" ) @@ -176,6 +177,12 @@ func main() { Usage: "logging level (supported levels: panic, fatal, error, warn, info, debug)", Value: defaultLogLevel, }, + cli.StringFlag{ + Name: "logfile", + Value: "stderr", + Usage: "filename where logs will be redirected (default stderr)\n\t", + EnvVar: "XDS_LOGFILENAME", + }, cli.StringFlag{ Name: "url, u", EnvVar: "XDS_AGENT_URL", @@ -201,12 +208,13 @@ func main() { initCmdProjects(&app.Commands) initCmdSdks(&app.Commands) initCmdExec(&app.Commands) + initCmdTargets(&app.Commands) initCmdMisc(&app.Commands) // Add --config option to all commands to support --config option either before or after command verb // IOW support following both syntaxes: - // xds-cli exec --config myprj.conf ... - // xds-cli --config myprj.conf exec ... + // xds-cli exec --config myPrj.conf ... + // xds-cli --config myPrj.conf exec ... for i, cmd := range app.Commands { if len(cmd.Flags) > 0 { app.Commands[i].Flags = append(cmd.Flags, cli.StringFlag{Hidden: true, Name: "config, c"}) @@ -276,6 +284,17 @@ func main() { } Log.Formatter = &logrus.TextFormatter{} + if ctx.String("logfile") != "stderr" { + logFile, _ := common.ResolveEnvVar(ctx.String("logfile")) + fdL, err := os.OpenFile(logFile, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0666) + if err != nil { + msgErr := fmt.Sprintf("Cannot create log file %s", logFile) + return cli.NewExitError(msgErr, 1) + } + Log.Infof("Logging to file: %s", logFile) + Log.Out = fdL + } + Log.Infof("%s version: %s", AppName, app.Version) earlyDisplay() Log.Debugf("\nEnvironment: %v\n", os.Environ()) @@ -294,6 +313,10 @@ func main() { XdsConnClose() }() + // Start signals monitoring routine + MonitorSignals() + + // Run the cli app app.Run(os.Args) } @@ -320,6 +343,11 @@ func XdsConnInit(ctx *cli.Context) error { serverURL = "http://" + serverURL } + lvl := common.HTTPLogLevelWarning + if Log.Level == logrus.DebugLevel { + lvl = common.HTTPLogLevelDebug + } + // Create HTTP client Log.Debugln("Connect HTTP client on ", agentURL) conf := common.HTTPClientConfig{ @@ -328,15 +356,22 @@ func XdsConnInit(ctx *cli.Context) error { CsrfDisable: true, LogOut: Log.Out, LogPrefix: "XDSAGENT: ", - LogLevel: common.HTTPLogLevelWarning, + LogLevel: lvl, } HTTPCli, err = common.HTTPNewClient(agentURL, conf) if err != nil { errmsg := err.Error() - if m, err := regexp.MatchString("Get http.?://", errmsg); m && err == nil { + m, err := regexp.MatchString("Get http.?://", errmsg) + if (m && err == nil) || strings.Contains(errmsg, "Failed to get device ID") { i := strings.LastIndex(errmsg, ":") - errmsg = "Cannot connection to " + agentURL + errmsg[i:] + newErr := "Cannot connection to " + agentURL + if i > 0 { + newErr += " (" + strings.TrimSpace(errmsg[i+1:]) + ")" + } else { + newErr += " (" + strings.TrimSpace(errmsg) + ")" + } + errmsg = newErr } return cli.NewExitError(errmsg, 1) } @@ -376,6 +411,9 @@ func XdsConnInit(ctx *cli.Context) error { if err := XdsConfigGet(&xdsConf); err != nil { return cli.NewExitError("ERROR while getting XDS config: "+err.Error(), 1) } + if len(xdsConf.Servers) < 1 { + return cli.NewExitError("No XDS Server connected", 1) + } svrCfg := xdsConf.Servers[XdsServerIndexGet()] if (serverURL != "" && svrCfg.URL != serverURL) || !svrCfg.Connected { Log.Infof("Update XDS Server config: serverURL=%v, svrCfg=%v", serverURL, svrCfg)