aboutsummaryrefslogtreecommitdiffstats
path: root/main.go
blob: 737c9b2e642f67b608ac1cd49781b581fe51a65b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
/*
 * Copyright (C) 2017-2019 "IoT.bzh"
 * Author Sebastien Douheret <sebastien@iot.bzh>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 *
 * xds-cli: command line tool used to control / interface X(cross) Development System.
 */

package main

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"
	"github.com/Sirupsen/logrus"
	"github.com/joho/godotenv"
	"github.com/urfave/cli"
)

var appAuthors = []cli.Author{
	cli.Author{Name: "Sebastien Douheret", Email: "sebastien@iot.bzh"},
}

// AppName name of this application
var AppName = "xds-cli"

// AppNativeName native command name that this application can overload
var AppNativeName = "cli"

// AppVersion Version of this application
// (set by Makefile)
var AppVersion = "?.?.?"

// AppSubVersion is the git tag id added to version string
// Should be set by compilation -ldflags "-X main.AppSubVersion=xxx"
// (set by Makefile)
var AppSubVersion = "unknown-dev"

// Application details
const (
	appCopyright             = "Copyright (C) 2017-2019 IoT.bzh - Apache-2.0"
	defaultLogLevel          = "error"
	defaultConfigEnvFilename = "cli-config.env"
)

// 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

// 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{}) {
	earlyDisplay()
	err := fmt.Sprintf(f, a...)
	fmt.Fprintf(os.Stderr, err+"\n")
	os.Exit(code)
}

// earlyDebug Used to log info before logger has been initialized
var earlyDebug []string

func earlyPrintf(format string, args ...interface{}) {
	earlyDebug = append(earlyDebug, fmt.Sprintf(format, args...))
}

func earlyDisplay() {
	for _, str := range earlyDebug {
		Log.Infof("%s", str)
	}
	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() {

	// Allow to set app name from cli (useful for debugging)
	if AppName == "" {
		AppName = os.Getenv("XDS_APPNAME")
	}
	if AppName == "" {
		panic("Invalid setup, AppName not define !")
	}
	if AppNativeName == "" {
		AppNativeName = AppName[4:]
	}
	appUsage := fmt.Sprintf("command line tool for X(cross) Development System.")
	appDescription := fmt.Sprintf("%s utility for X(cross) Development System\n", AppName)
	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_AGENT_URL' variable) when a
         config file is specified with '--config|-c' option,
      3. else use 'XDS_xxx' (for example 'XDS_AGENT_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
	app.Usage = appUsage
	app.Version = AppVersion + " (" + AppSubVersion + ")"
	app.Authors = appAuthors
	app.Copyright = appCopyright
	app.Metadata = make(map[string]interface{})
	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:"
	for _, f := range app.Flags {
		var env, usage string
		switch f.(type) {
		case cli.StringFlag:
			fs := f.(cli.StringFlag)
			env = fs.EnvVar
			usage = fs.Usage
		case cli.BoolFlag:
			fb := f.(cli.BoolFlag)
			env = fb.EnvVar
			usage = fb.Usage
		default:
			exitError(1, "Un-implemented option type")
		}
		if env != "" {
			dynDesc += fmt.Sprintf("\n %s \t\t %s", env, usage)
		}
	}
	app.Description = appDescription + dynDesc

	// Declare global flags
	app.Flags = []cli.Flag{
		cli.StringFlag{
			Name:   "config, c",
			EnvVar: "XDS_CONFIG",
			Usage:  "env config file to source on startup",
		},
		cli.StringFlag{
			Name:   "log, l",
			EnvVar: "XDS_LOGLEVEL",
			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",
			Value:  "localhost:8800",
			Usage:  "local XDS agent url",
		},
		cli.StringFlag{
			Name:   "url-server, us",
			EnvVar: "XDS_SERVER_URL",
			Value:  "",
			Usage:  "overwrite remote XDS server url (default value set in xds-agent-config.json file)",
		},
		cli.BoolFlag{
			Name:   "timestamp, ts",
			EnvVar: "XDS_TIMESTAMP",
			Usage:  "prefix output with timestamp",
		},
	}

	// Declare commands
	app.Commands = []cli.Command{}

	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 ...
	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"})
		}
		for j, subCmd := range cmd.Subcommands {
			app.Commands[i].Subcommands[j].Flags = append(subCmd.Flags, cli.StringFlag{Hidden: true, Name: "config, c"})
		}
	}

	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
	//  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" {
			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("Used confFile: %v", confFile)
		// 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)
		}
		earlyPrintf("EnvConfFileMap: %v", EnvConfFileMap)
	}

	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
			}
		}

		loglevel := ctx.String("log")
		// Set logger level and formatter
		if Log.Level, err = logrus.ParseLevel(loglevel); err != nil {
			msg := fmt.Sprintf("Invalid log level : \"%v\"\n", loglevel)
			return cli.NewExitError(msg, 1)
		}
		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())

		if err = XdsConnInit(ctx); err != nil {
			// Directly call HandleExitCoder to avoid to print help (ShowAppHelp)
			// Note that this function wil never return and program will exit
			cli.HandleExitCoder(err)
		}

		return nil
	}

	// Close HTTP client and WS connection on exit
	defer func() {
		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)
}

// XdsConnInit Initialized HTTP and WebSocket connection to XDS agent
func XdsConnInit(ctx *cli.Context) error {
	var err error

	// Define HTTP and WS url
	agentURL := ctx.String("url")
	serverURL := ctx.String("url-server")

	// Allow to only set port number
	if match, _ := regexp.MatchString("^([0-9]+)$", agentURL); match {
		agentURL = "http://localhost:" + ctx.String("url")
	}
	if match, _ := regexp.MatchString("^([0-9]+)$", serverURL); match {
		serverURL = "http://localhost:" + ctx.String("url-server")
	}
	// Add http prefix if missing
	if agentURL != "" && !strings.HasPrefix(agentURL, "http://") {
		agentURL = "http://" + agentURL
	}
	if serverURL != "" && !strings.HasPrefix(serverURL, "http://") {
		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{
		URLPrefix:           "/api/v1",
		HeaderClientKeyName: "Xds-Agent-Sid",
		CsrfDisable:         true,
		LogOut:              Log.Out,
		LogPrefix:           "XDSAGENT: ",
		LogLevel:            lvl,
	}

	HTTPCli, err = common.HTTPNewClient(agentURL, conf)
	if err != nil {
		errmsg := err.Error()
		m, err := regexp.MatchString("Get http.?://", errmsg)
		if (m && err == nil) || strings.Contains(errmsg, "Failed to get device ID") {
			i := strings.LastIndex(errmsg, ":")
			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)
	}
	HTTPCli.SetLogLevel(ctx.String("loglevel"))
	Log.Infoln("HTTP session ID : ", HTTPCli.GetClientID())

	// Create io Websocket client
	Log.Debugln("Connecting IO.socket client on ", agentURL)

	IOSkClient, err = NewIoSocketClient(agentURL, HTTPCli.GetClientID())
	if err != nil {
		return cli.NewExitError(err.Error(), 1)
	}

	IOSkClient.On("error", func(err error) {
		fmt.Println("ERROR Websocket: ", err.Error())
	})

	ctx.App.Metadata["httpCli"] = HTTPCli
	ctx.App.Metadata["ioskCli"] = IOSkClient

	// Display version in logs (debug helpers)
	ver := xaapiv1.XDSVersion{}
	if err := XdsVersionGet(&ver); err != nil {
		return cli.NewExitError("ERROR while retrieving XDS version: "+err.Error(), 1)
	}
	Log.Infof("XDS Agent/Server version: %v", ver)

	// Get current config and update connection to server when needed
	xdsConf := xaapiv1.APIConfig{}
	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)
		if serverURL != "" {
			svrCfg.URL = serverURL
		}
		svrCfg.ConnRetry = 10
		if err := XdsConfigSet(xdsConf); err != nil {
			return cli.NewExitError("ERROR while updating XDS server URL: "+err.Error(), 1)
		}
	}

	return nil
}

// XdsConnClose Terminate connection to XDS agent
func XdsConnClose() {
	Log.Debugf("Closing HTTP client session...")
	/* TODO
	if httpCli, ok := app.Metadata["httpCli"]; ok {
		c := httpCli.(*common.HTTPClient)
	}
	*/

	Log.Debugf("Closing WebSocket connection...")
	/*
		if ioskCli, ok := app.Metadata["ioskCli"]; ok {
			c := ioskCli.(*socketio_client.Client)
		}
	*/
}

// NewTableWriter Create a writer that inserts padding around tab-delimited
func NewTableWriter() *tabwriter.Writer {
	writer := new(tabwriter.Writer)
	writer.Init(os.Stdout, 0, 8, 0, '\t', 0)
	return writer
}