/*
- * Copyright (C) 2017-2018 "IoT.bzh"
+ * Copyright (C) 2017-2019 "IoT.bzh"
* Author Sebastien Douheret <sebastien@iot.bzh>
*
* Licensed under the Apache License, Version 2.0 (the "License");
import (
"fmt"
"os"
+ "path"
"regexp"
"sort"
"strings"
+ "syscall"
"text/tabwriter"
"gerrit.automotivelinux.org/gerrit/src/xds/xds-agent.git/lib/xaapiv1"
- common "gerrit.automotivelinux.org/gerrit/src/xds/xds-common.git/golib"
+ common "gerrit.automotivelinux.org/gerrit/src/xds/xds-common.git"
"github.com/Sirupsen/logrus"
-
"github.com/joho/godotenv"
- socketio_client "github.com/sebd71/go-socket.io-client"
"github.com/urfave/cli"
)
// Application details
const (
- appCopyright = "Copyright (C) 2017-2018 IoT.bzh - Apache-2.0"
- defaultLogLevel = "error"
+ appCopyright = "Copyright (C) 2017-2019 IoT.bzh - Apache-2.0"
+ defaultLogLevel = "error"
+ defaultConfigEnvFilename = "cli-config.env"
)
// Log Global variable that hold logger
// HTTPCli Global variable that hold HTTP Client
var HTTPCli *common.HTTPClient
-// IOsk Global variable that hold SocketIo client
-var IOsk *socketio_client.Client
+// IOSkClient Global variable that hold SocketIo client
+var IOSkClient *IOSockClient
// exitError exists this program with the specified error
func exitError(code int, f string, a ...interface{}) {
earlyDebug = []string{}
}
+// LogSillyf Logging helper used for silly logging (printed on log.debug)
+func LogSillyf(format string, args ...interface{}) {
+ sillyVal, sillyLog := os.LookupEnv("XDS_LOG_SILLY")
+ if sillyLog && sillyVal == "1" {
+ Log.Debugf("SILLY: "+format, args...)
+ }
+}
+
// main
func main() {
app.Metadata["version"] = AppVersion
app.Metadata["git-tag"] = AppSubVersion
app.Metadata["logger"] = Log
+ // FIXME: Disable completion for now, because it's not working with options
+ // (eg. --label) and prevents to complete local path
+ // (IOW current function only completes command and sub-commands)
+ app.EnableBashCompletion = false
// Create env vars help
dynDesc := "\nENVIRONMENT VARIABLES:"
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
// 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")
+ // 1/ from command line option: "--config myConfig.json"
+ // 2/ from environment variable XDS_CONFIG
+ // 3/ $HOME/.xds/cli/cli-config.env file
+ // 4/ /etc/xds/cli/cli-config.env file
+ searchIn := make([]string, 0, 4)
for idx, a := range os.Args[1:] {
if a == "-c" || a == "--config" || a == "-config" {
- confFile = os.Args[idx+2]
+ searchIn = append(searchIn, os.Args[idx+2])
break
}
}
+ searchIn = append(searchIn, os.Getenv("XDS_CONFIG"))
+ if usrHome := common.GetUserHome(); usrHome != "" {
+ searchIn = append(searchIn, path.Join(usrHome, ".xds", "cli", defaultConfigEnvFilename))
+ }
+ searchIn = append(searchIn, path.Join("/etc", "xds", "cli", defaultConfigEnvFilename))
+
+ // Use the first existing env config file
+ confFile := ""
+ for _, p := range searchIn {
+ if pr, err := common.ResolveEnvVar(p); err == nil {
+ earlyPrintf("Check if confFile exists : %v", pr)
+ if common.Exists(pr) {
+ confFile = pr
+ break
+ }
+ }
+ }
// Load config file if requested
if confFile != "" {
- earlyPrintf("confFile detected: %v", confFile)
- confFile, err := common.ResolveEnvVar(confFile)
- if err != nil {
- exitError(1, "Error while resolving confFile: %v", err)
- }
- earlyPrintf("Resolved confFile: %v", confFile)
- if !common.Exists(confFile) {
- exitError(1, "Error env config file not found")
- }
+ earlyPrintf("Used confFile: %v", confFile)
// Load config file variables that will overwrite env variables
- err = godotenv.Overload(confFile)
+ err := godotenv.Overload(confFile)
if err != nil {
exitError(1, "Error loading env config file "+confFile)
}
XdsConnClose()
}()
+ // Start signals monitoring routine
+ MonitorSignals()
+
+ // Default callback to handle interrupt signal
+ // Maybe be overwritten by some subcommand (eg. targets commands)
+ err := OnSignals(func(sig os.Signal) {
+ Log.Debugf("Send signal %v (from main)", sig)
+ if IsInterruptSignal(sig) {
+ err := cli.NewExitError("Interrupted\n", int(syscall.EINTR))
+ cli.HandleExitCoder(err)
+ }
+ })
+ if err != nil {
+ cli.NewExitError(err.Error(), 1)
+ return
+ }
+
+ // Run the cli app
app.Run(os.Args)
}
// Create io Websocket client
Log.Debugln("Connecting IO.socket client on ", agentURL)
- opts := &socketio_client.Options{
- Transport: "websocket",
- Header: make(map[string][]string),
- }
- opts.Header["XDS-AGENT-SID"] = []string{HTTPCli.GetClientID()}
-
- IOsk, err = socketio_client.NewClient(agentURL, opts)
+ IOSkClient, err = NewIoSocketClient(agentURL, HTTPCli.GetClientID())
if err != nil {
- return cli.NewExitError("IO.socket connection error: "+err.Error(), 1)
+ return cli.NewExitError(err.Error(), 1)
}
- IOsk.On("error", func(err error) {
+ IOSkClient.On("error", func(err error) {
fmt.Println("ERROR Websocket: ", err.Error())
})
ctx.App.Metadata["httpCli"] = HTTPCli
- ctx.App.Metadata["ioskCli"] = IOsk
+ ctx.App.Metadata["ioskCli"] = IOSkClient
// Display version in logs (debug helpers)
ver := xaapiv1.XDSVersion{}