@webos-tools/cli 3.1.2 → 3.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,525 +1,573 @@
1
- #!/usr/bin/env node
2
-
3
- /*
4
- * Copyright (c) 2020-2024 LG Electronics Inc.
5
- *
6
- * SPDX-License-Identifier: Apache-2.0
7
- */
8
-
9
- const async = require('async'),
10
- inquirer = require('inquirer'),
11
- nopt = require('nopt'),
12
- log = require('npmlog'),
13
- path = require('path'),
14
- Ssdp = require('ssdp-js'),
15
- commonTools = require('./../lib/base/common-tools'),
16
- novacom = require('./../lib/base/novacom');
17
-
18
- const version = commonTools.version,
19
- cliControl = commonTools.cliControl,
20
- help = commonTools.help,
21
- appdata = commonTools.appdata,
22
- errHndl = commonTools.errMsg,
23
- setupDevice = commonTools.setupDevice;
24
-
25
- const processName = path.basename(process.argv[1]).replace(/.js/, '');
26
-
27
- process.on('uncaughtException', function(err) {
28
- log.error('uncaughtException', err.toString());
29
- log.verbose('uncaughtException', err.stack);
30
- cliControl.end(-1);
31
- });
32
-
33
- const knownOpts = {
34
- // generic options
35
- "help": Boolean,
36
- "level": ['silly', 'verbose', 'info', 'http', 'warn', 'error'],
37
- "version": Boolean,
38
- // command-specific options
39
- "list": Boolean,
40
- "listfull": Boolean,
41
- "add": [String, null],
42
- "remove": [String, null],
43
- "modify": [String, null],
44
- "default": [String, null],
45
- "search": Boolean,
46
- "timeout": [String, null],
47
- "info": [String, Array],
48
- "reset": Boolean
49
- };
50
-
51
- const shortHands = {
52
- // generic aliases
53
- "h": ["--help"],
54
- "v": ["--level", "verbose"],
55
- "V": ["--version"],
56
- // command-specific aliases
57
- "l": ["--list"],
58
- "F": ["--listfull"],
59
- "i": ["--info"],
60
- "a": ["--add"],
61
- "r": ["--remove"],
62
- "m": ["--modify"],
63
- "f": ["--default"],
64
- "s": ["--search"],
65
- "t": ["--timeout"],
66
- "R": ["--reset"]
67
- };
68
-
69
- const argv = nopt(knownOpts, shortHands, process.argv, 2 /* drop 'node' & 'ares-*.js'*/);
70
-
71
- log.heading = processName;
72
- log.level = argv.level || 'warn';
73
- log.verbose("argv", argv);
74
-
75
- const inqChoices = ["add", "modify"],
76
- dfChoices = ["set default"],
77
- rmChoices = ["remove"],
78
- totChoices = inqChoices.concat(rmChoices, dfChoices);
79
-
80
- let questions = [],
81
- op;
82
-
83
- if (argv.list) {
84
- op = deviceList;
85
- } else if (argv.listfull) {
86
- op = deviceListFull;
87
- } else if (argv.reset) {
88
- op = reset;
89
- } else if (argv.search || argv.timeout) {
90
- op = search;
91
- } else if (argv.add || argv.modify || argv.info) {
92
- op = modifyDeviceInfo;
93
- } else if (argv.remove) {
94
- op = removeDeviceInfo;
95
- } else if (argv.default) {
96
- op = setDefaultDeviceInfo;
97
- } else if (argv.version) {
98
- version.showVersionAndExit();
99
- } else if (argv.help) {
100
- help.display(processName, appdata.getConfig(true).profile);
101
- cliControl.end();
102
- } else {
103
- op = interactiveInput;
104
- }
105
-
106
- if (op) {
107
- version.checkNodeVersion(function() {
108
- async.series([
109
- op.bind(this)
110
- ],finish);
111
- });
112
- }
113
-
114
- const _needInq = function(choice) {
115
- return function(choices) {
116
- return (choices.indexOf(choice) !== -1);
117
- };
118
- };
119
-
120
- function deviceList() {
121
- setupDevice.showDeviceList(finish);
122
- }
123
-
124
- function deviceListFull() {
125
- setupDevice.showDeviceListFull(finish);
126
- }
127
-
128
- function reset() {
129
- setupDevice.resetDeviceList(finish);
130
- }
131
-
132
- function _queryAddRemove(ssdpDevices, next) {
133
- let selDevice = {};
134
- const resolver = this.resolver || (this.resolver = new novacom.Resolver());
135
- if (typeof ssdpDevices === 'function') {
136
- next = ssdpDevices;
137
- ssdpDevices = null;
138
- }
139
- async.waterfall([
140
- resolver.load.bind(resolver),
141
- resolver.list.bind(resolver),
142
- function(devices, next) {
143
- if (!ssdpDevices) return next(null, devices, null);
144
- const ssdpDevMap = {};
145
- ssdpDevices.forEach(function(sd) {
146
- const key = sd.name + ' # '+sd.address;
147
- for (const idx in devices) {
148
- if (devices[idx].name === sd.name)
149
- {
150
- ssdpDevMap[key] = devices[idx];
151
- ssdpDevMap[key].op = 'modify';
152
- ssdpDevMap[key].host = sd.address;
153
- break;
154
- }
155
- }
156
- if (!ssdpDevMap[key]) {
157
- ssdpDevMap[key] = {
158
- name: sd.name,
159
- host: sd.address,
160
- op: 'add'
161
- };
162
- }
163
- });
164
-
165
- questions = [{
166
- type: "list",
167
- name: "discovered",
168
- message: "Select",
169
- choices: Object.keys(ssdpDevMap)
170
- }];
171
- inquirer.prompt(questions).then(function(answers) {
172
- next(null, devices, ssdpDevMap[answers.discovered]);
173
- });
174
- },
175
- function(devices, ssdpDevice, next) {
176
- const deviceNames = devices.filter(function(device) {
177
- return (device.conn.indexOf('novacom') === -1);
178
- }).map(function(device) {
179
- return (device.name);
180
- });
181
- questions = [{
182
- type: "list",
183
- name: "op",
184
- message: "Select",
185
- choices: function() {
186
- if (ssdpDevice) {
187
- if (ssdpDevice.op === 'modify') return inqChoices;
188
- else return ['add'];
189
- }
190
- if (deviceNames.length > 1) return totChoices;
191
- // deveice list has emulator only > unsupported remove option
192
- return inqChoices.concat(dfChoices);
193
- },
194
- filter: function(val) {
195
- return val.toLowerCase();
196
- },
197
- default: function() {
198
- if (ssdpDevice && ssdpDevice.op) return ssdpDevice.op;
199
- else return null;
200
- }
201
- }, {
202
- type: "input",
203
- name: "device_name",
204
- message: "Enter Device Name:",
205
- when: function(answers) {
206
- return (answers.op === "add");
207
- },
208
- default: function() {
209
- if (ssdpDevice && ssdpDevice.name) return ssdpDevice.name;
210
- else return null;
211
- },
212
- validate: function(input) {
213
- if (input.length < 1) {
214
- return errHndl.getErrStr("EMPTY_VALUE");
215
- }
216
- if (deviceNames.indexOf(input) !== -1) {
217
- return errHndl.getErrStr("EXISTING_VALUE");
218
- }
219
- if (!setupDevice.isValidDeviceName(input)) {
220
- return errHndl.getErrStr("INVALID_DEVICENAME");
221
- }
222
- return true;
223
- }
224
- }, {
225
- type: "list",
226
- name: "device_name",
227
- message: "Select a device",
228
- choices: deviceNames.filter(dv => dv !== "emulator"),
229
- when: function(answers) {
230
- return (["remove"].indexOf(answers.op) !== -1 && !ssdpDevice);
231
- }
232
- }, {
233
- type: "list",
234
- name: "device_name",
235
- message: "Select a device",
236
- choices: deviceNames,
237
- when: function(answers) {
238
- return (["modify", "set default"].indexOf(answers.op) !== -1 && !ssdpDevice);
239
- }
240
- }];
241
- inquirer.prompt(questions)
242
- .then(function(answers) {
243
- devices.forEach(function(device) {
244
- if (answers.device_name === device.name) {
245
- selDevice = device;
246
- }
247
- });
248
- selDevice.name = answers.device_name || ((ssdpDevice) ? ssdpDevice.name : null);
249
- selDevice.mode = answers.op || ((ssdpDevice) ? ssdpDevice.op : null);
250
- selDevice.host = (ssdpDevice) ? ssdpDevice.host : (selDevice.host || null);
251
- next(null, selDevice, null);
252
- });
253
- }
254
- ], function(err, result) {
255
- next(err, result);
256
- });
257
- }
258
-
259
- function _queryDeviceInfo(selDevice, next) {
260
- let mode = selDevice.mode;
261
- const deviceName = selDevice.name,
262
- resolver = this.resolver || (this.resolver = new novacom.Resolver()),
263
- defaultDeviceInfo = appdata.getConfig(true).defaultDeviceInfo;
264
-
265
- questions = [{
266
- type: "input",
267
- name: "ip",
268
- message: "Enter Device IP address:",
269
- default: function() {
270
- return selDevice.host || defaultDeviceInfo.ipAddress;
271
- },
272
- validate: function(answers) {
273
- if (!setupDevice.isValidIpv4(answers)) {
274
- return errHndl.getErrStr("INVALID_VALUE");
275
- }
276
- return true;
277
- },
278
- when: function() {
279
- return _needInq(mode)(inqChoices);
280
- }
281
- }, {
282
- type: "input",
283
- name: "port",
284
- message: "Enter Device Port:",
285
- default: function() {
286
- return selDevice.port || defaultDeviceInfo.port;
287
- },
288
- validate: function(answers) {
289
- if (!setupDevice.isValidPort(answers)) {
290
- return errHndl.getErrStr("INVALID_VALUE");
291
- }
292
- return true;
293
- },
294
- when: function() {
295
- return _needInq(mode)(inqChoices);
296
- }
297
- }, {
298
- type: "input",
299
- name: "user",
300
- message: "Enter ssh user:",
301
- default: function() {
302
- return selDevice.username || defaultDeviceInfo.user;
303
- },
304
- when: function() {
305
- return _needInq(mode)(inqChoices);
306
- }
307
- }, {
308
- type: "input",
309
- name: "description",
310
- message: "Enter description:",
311
- default: function() {
312
- return selDevice.description || defaultDeviceInfo.description;
313
- },
314
- when: function() {
315
- return _needInq(mode)(inqChoices);
316
- }
317
- }, {
318
- type: "list",
319
- name: "auth_type",
320
- message: "Select authentication",
321
- choices: ["password", "ssh key"],
322
- default: function() {
323
- let idx = 0;
324
- if (selDevice.privateKeyName) {
325
- idx = 1;
326
- }
327
- return idx;
328
- },
329
- when: function(answers) {
330
- return _needInq(mode)(inqChoices) && answers.user === "root";
331
- }
332
- }, {
333
- type: "password",
334
- name: "password",
335
- message: "Enter password:",
336
- when: function(answers) {
337
- return _needInq(mode)(inqChoices) && (answers.auth_type === "password");
338
- }
339
- }, {
340
- type: "input",
341
- name: "ssh_key",
342
- message: "Enter ssh private key file name:",
343
- default: function() {
344
- return selDevice.privateKeyName || "webos_emul";
345
- },
346
- when: function(answers) {
347
- return _needInq(mode)(inqChoices) && (answers.auth_type === "ssh key");
348
- }
349
- }, {
350
- type: "confirm",
351
- name: "default",
352
- message: "Set default ?",
353
- default: false,
354
- when: function() {
355
- return (mode === "add");
356
- }
357
- }, {
358
- type: "confirm",
359
- name: "confirm",
360
- message: "Save ?",
361
- default: true
362
- }];
363
-
364
- inquirer.prompt(questions).then(function(answers) {
365
- if (answers.confirm) {
366
- log.info("interactiveInput()#_queryDeviceInfo()", "saved");
367
- } else {
368
- log.info("interactiveInput()#_queryDeviceInfo()", "canceled");
369
- return next(null, {
370
- "msg": "Canceled"
371
- });
372
- }
373
- const inDevice = {
374
- profile: appdata.getConfig(true).profile,
375
- name: deviceName,
376
- host: answers.ip,
377
- port: answers.port,
378
- description: answers.description,
379
- username: answers.user,
380
- default: answers.default
381
- };
382
-
383
- if (answers.user !== 'prisoner' && ["add", "modify"].includes(mode)) {
384
- if (answers.auth_type && answers.auth_type === "password") {
385
- inDevice.password = answers.password;
386
- inDevice.privateKey = "@DELETE@";
387
- inDevice.passphrase = "@DELETE@";
388
- inDevice.privateKeyName = "@DELETE@";
389
- } else if ((answers.auth_type && answers.auth_type === "ssh key") || answers.user === "developer") {
390
- inDevice.password = "@DELETE@";
391
- inDevice.privateKey = {
392
- "openSsh": answers.ssh_key || "webos_emul"
393
- };
394
- inDevice.passphrase = answers.ssh_passphrase || "@DELETE@";
395
- inDevice.privateKeyName = "@DELETE@";
396
- } else {
397
- return next(errHndl.getErrMsg("NOT_SUPPORT_AUTHTYPE", answers.auth_type));
398
- }
399
- }
400
-
401
- if (mode === 'set default') {
402
- inDevice.default = true;
403
- mode = 'default';
404
- }
405
-
406
- setupDevice.replaceDefaultDeviceInfo(inDevice);
407
- if (inDevice.port) {
408
- inDevice.port = Number(inDevice.port);
409
- }
410
- async.series([
411
- resolver.load.bind(resolver),
412
- resolver.modifyDeviceFile.bind(resolver, mode, inDevice),
413
- setupDevice.showDeviceList.bind(this),
414
- ], function(err, results) {
415
- if (err) {
416
- return next(err);
417
- }
418
- if(results[2] && results[2].msg){
419
- console.log(results[2].msg);
420
- }
421
- if(inDevice.username === 'prisoner' && mode === 'add'){
422
- setupDevice.displayGetKeyGuide(inDevice.name);
423
- }
424
- finish();
425
- });
426
- });
427
- }
428
-
429
- function interactiveInput() {
430
- async.waterfall([
431
- setupDevice.showDeviceList.bind(this),
432
- function(data, next) {
433
- console.log(data.msg);
434
- console.log("** You can modify the device info in the above list, or add new device.");
435
- next();
436
- },
437
- _queryAddRemove,
438
- _queryDeviceInfo
439
- ], function(err, result) {
440
- finish(err, result);
441
- });
442
- }
443
-
444
- function search(next) {
445
- const TIMEOUT = 5000,
446
- ssdp = new Ssdp(),
447
- timeout = Number(argv.timeout) * 1000 || TIMEOUT,
448
- outterNext = next,
449
- self = this;
450
- let discovered = [],
451
- end = false;
452
-
453
- console.log("Searching...");
454
- log.verbose("search()", "timeout:", timeout);
455
- ssdp.start();
456
- ssdp.onDevice(function(device) {
457
- if (!device.headers || !device.headers.SERVER ||
458
- device.headers.SERVER.indexOf('WebOS') < 0 || end) {
459
- return finish(null, {msg: "No devices is discovered."});
460
- }
461
- log.verbose("search()# %s:%s (%s)", '[Discovered]', device.name, device.address);
462
- });
463
- async.waterfall([
464
- function(next) {
465
- setTimeout(function() {
466
- discovered = ssdp.getList().map(function(device) {
467
- end = true;
468
- return {
469
- "uuid": device.headers.USN.split(':')[1],
470
- "name": device.name.replace(/\s/g, '_'),
471
- "address": device.address,
472
- "registered": false
473
- };
474
- });
475
- // ssdp.destroy();
476
- if (discovered.length === 0) {
477
- console.log("No devices is discovered.");
478
- return outterNext();
479
- }
480
- log.verbose("search()", "discovered:", discovered.length);
481
- next(null, discovered);
482
- }, timeout);
483
- },
484
- _queryAddRemove.bind(self),
485
- _queryDeviceInfo.bind(self)
486
- ], function(err) {
487
- next(err);
488
- });
489
- }
490
-
491
- function modifyDeviceInfo() {
492
- setupDevice.modifyDeviceInfo(argv, finish);
493
- }
494
-
495
- function setDefaultDeviceInfo() {
496
- setupDevice.setDefaultDevice(argv.default, finish);
497
- }
498
-
499
- function removeDeviceInfo() {
500
- setupDevice.removeDeviceInfo(argv, finish);
501
- }
502
-
503
- function finish(err, value) {
504
- log.info("finish()");
505
- if (err) {
506
- // handle err from getErrMsg()
507
- if (Array.isArray(err) && err.length > 0) {
508
- for (const index in err) {
509
- log.error(err[index].heading, err[index].message);
510
- }
511
- log.verbose(err[0].stack);
512
- } else {
513
- // handle general err (string & object)
514
- log.error(err.toString());
515
- log.verbose(err.stack);
516
- }
517
- cliControl.end(-1);
518
- } else {
519
- log.verbose("finish()", "value:", value);
520
- if (value && value.msg) {
521
- console.log(value.msg);
522
- }
523
- cliControl.end();
524
- }
525
- }
1
+ #!/usr/bin/env node
2
+
3
+ /*
4
+ * Copyright (c) 2020-2024 LG Electronics Inc.
5
+ *
6
+ * SPDX-License-Identifier: Apache-2.0
7
+ */
8
+
9
+ const async = require('async'),
10
+ inquirer = require('inquirer'),
11
+ nopt = require('nopt'),
12
+ abbrev = require("abbrev"),
13
+ log = require('npmlog'),
14
+ path = require('path'),
15
+ Ssdp = require('ssdp-js'),
16
+ commonTools = require('./../lib/base/common-tools'),
17
+ novacom = require('./../lib/base/novacom');
18
+
19
+ const version = commonTools.version,
20
+ cliControl = commonTools.cliControl,
21
+ help = commonTools.help,
22
+ appdata = commonTools.appdata,
23
+ errHndl = commonTools.errMsg,
24
+ setupDevice = commonTools.setupDevice;
25
+
26
+ const processName = path.basename(process.argv[1]).replace(/.js/, '');
27
+
28
+ process.on('uncaughtException', function(err) {
29
+ log.error('uncaughtException', err.toString());
30
+ log.verbose('uncaughtException', err.stack);
31
+ cliControl.end(-1);
32
+ });
33
+
34
+ const knownOpts = {
35
+ // generic options
36
+ "help": Boolean,
37
+ "level": ['silly', 'verbose', 'info', 'http', 'warn', 'error'],
38
+ "version": Boolean,
39
+ // command-specific options
40
+ "list": Boolean,
41
+ "listfull": Boolean,
42
+ "add": [String, null],
43
+ "remove": [String, null],
44
+ "modify": [String, null],
45
+ "default": [String, null],
46
+ "search": Boolean,
47
+ "timeout": [String, null],
48
+ "info": [String, Array],
49
+ "reset": Boolean
50
+ };
51
+
52
+ const shortHands = {
53
+ // generic aliases
54
+ "h": ["--help"],
55
+ "v": ["--level", "verbose"],
56
+ "V": ["--version"],
57
+ // command-specific aliases
58
+ "l": ["--list"],
59
+ "F": ["--listfull"],
60
+ "i": ["--info"],
61
+ "a": ["--add"],
62
+ "r": ["--remove"],
63
+ "m": ["--modify"],
64
+ "f": ["--default"],
65
+ "s": ["--search"],
66
+ "t": ["--timeout"],
67
+ "R": ["--reset"]
68
+ };
69
+
70
+ const argv = getArgv();
71
+
72
+ log.heading = processName;
73
+ log.level = argv.level || 'warn';
74
+ log.verbose("argv", argv);
75
+
76
+ const inqChoices = ["add", "modify"],
77
+ dfChoices = ["set default"],
78
+ rmChoices = ["remove"],
79
+ totChoices = inqChoices.concat(rmChoices, dfChoices);
80
+
81
+ let questions = [],
82
+ op;
83
+
84
+ if (argv.list) {
85
+ op = deviceList;
86
+ } else if (argv.listfull) {
87
+ op = deviceListFull;
88
+ } else if (argv.reset) {
89
+ op = reset;
90
+ } else if (argv.search || argv.timeout) {
91
+ op = search;
92
+ } else if (argv.add || argv.modify || argv.info) {
93
+ op = modifyDeviceInfo;
94
+ } else if (argv.remove) {
95
+ op = removeDeviceInfo;
96
+ } else if (argv.default) {
97
+ op = setDefaultDeviceInfo;
98
+ } else if (argv.version) {
99
+ version.showVersionAndExit();
100
+ } else if (argv.help) {
101
+ help.display(processName, appdata.getConfig(true).profile);
102
+ cliControl.end();
103
+ } else {
104
+ op = interactiveInput;
105
+ }
106
+
107
+ if (op) {
108
+ version.checkNodeVersion(function() {
109
+ async.series([
110
+ op.bind(this)
111
+ ],finish);
112
+ });
113
+ }
114
+
115
+ const _needInq = function(choice) {
116
+ return function(choices) {
117
+ return (choices.indexOf(choice) !== -1);
118
+ };
119
+ };
120
+
121
+ function deviceList() {
122
+ setupDevice.showDeviceList(finish);
123
+ }
124
+
125
+ function deviceListFull() {
126
+ setupDevice.showDeviceListFull(finish);
127
+ }
128
+
129
+ function reset() {
130
+ setupDevice.resetDeviceList(finish);
131
+ }
132
+
133
+ function _queryAddRemove(ssdpDevices, next) {
134
+ let selDevice = {};
135
+ const resolver = this.resolver || (this.resolver = new novacom.Resolver());
136
+ if (typeof ssdpDevices === 'function') {
137
+ next = ssdpDevices;
138
+ ssdpDevices = null;
139
+ }
140
+ async.waterfall([
141
+ resolver.load.bind(resolver),
142
+ resolver.list.bind(resolver),
143
+ function(devices, next) {
144
+ if (!ssdpDevices) return next(null, devices, null);
145
+ const ssdpDevMap = {};
146
+ ssdpDevices.forEach(function(sd) {
147
+ const key = sd.name + ' # '+sd.address;
148
+ for (const idx in devices) {
149
+ if (devices[idx].name === sd.name)
150
+ {
151
+ ssdpDevMap[key] = devices[idx];
152
+ ssdpDevMap[key].op = 'modify';
153
+ ssdpDevMap[key].host = sd.address;
154
+ break;
155
+ }
156
+ }
157
+ if (!ssdpDevMap[key]) {
158
+ ssdpDevMap[key] = {
159
+ name: sd.name,
160
+ host: sd.address,
161
+ op: 'add'
162
+ };
163
+ }
164
+ });
165
+
166
+ questions = [{
167
+ type: "list",
168
+ name: "discovered",
169
+ message: "Select",
170
+ choices: Object.keys(ssdpDevMap)
171
+ }];
172
+ inquirer.prompt(questions).then(function(answers) {
173
+ next(null, devices, ssdpDevMap[answers.discovered]);
174
+ });
175
+ },
176
+ function(devices, ssdpDevice, next) {
177
+ const deviceNames = devices.filter(function(device) {
178
+ return (device.conn.indexOf('novacom') === -1);
179
+ }).map(function(device) {
180
+ return (device.name);
181
+ });
182
+ questions = [{
183
+ type: "list",
184
+ name: "op",
185
+ message: "Select",
186
+ choices: function() {
187
+ if (ssdpDevice) {
188
+ if (ssdpDevice.op === 'modify') return inqChoices;
189
+ else return ['add'];
190
+ }
191
+ if (deviceNames.length > 1) return totChoices;
192
+ // deveice list has emulator only > unsupported remove option
193
+ return inqChoices.concat(dfChoices);
194
+ },
195
+ filter: function(val) {
196
+ return val.toLowerCase();
197
+ },
198
+ default: function() {
199
+ if (ssdpDevice && ssdpDevice.op) return ssdpDevice.op;
200
+ else return null;
201
+ }
202
+ }, {
203
+ type: "input",
204
+ name: "device_name",
205
+ message: "Enter Device Name:",
206
+ when: function(answers) {
207
+ return (answers.op === "add");
208
+ },
209
+ default: function() {
210
+ if (ssdpDevice && ssdpDevice.name) return ssdpDevice.name;
211
+ else return null;
212
+ },
213
+ validate: function(input) {
214
+ if (input.length < 1) {
215
+ return errHndl.getErrStr("EMPTY_VALUE");
216
+ }
217
+ if (deviceNames.indexOf(input) !== -1) {
218
+ return errHndl.getErrStr("EXISTING_VALUE");
219
+ }
220
+ if (!setupDevice.isValidDeviceName(input)) {
221
+ return errHndl.getErrStr("INVALID_DEVICENAME");
222
+ }
223
+ return true;
224
+ }
225
+ }, {
226
+ type: "list",
227
+ name: "device_name",
228
+ message: "Select a device",
229
+ choices: deviceNames.filter(dv => dv !== "emulator"),
230
+ when: function(answers) {
231
+ return (["remove"].indexOf(answers.op) !== -1 && !ssdpDevice);
232
+ }
233
+ }, {
234
+ type: "list",
235
+ name: "device_name",
236
+ message: "Select a device",
237
+ choices: deviceNames,
238
+ when: function(answers) {
239
+ return (["modify", "set default"].indexOf(answers.op) !== -1 && !ssdpDevice);
240
+ }
241
+ }];
242
+ inquirer.prompt(questions)
243
+ .then(function(answers) {
244
+ devices.forEach(function(device) {
245
+ if (answers.device_name === device.name) {
246
+ selDevice = device;
247
+ }
248
+ });
249
+ selDevice.name = answers.device_name || ((ssdpDevice) ? ssdpDevice.name : null);
250
+ selDevice.mode = answers.op || ((ssdpDevice) ? ssdpDevice.op : null);
251
+ selDevice.host = (ssdpDevice) ? ssdpDevice.host : (selDevice.host || null);
252
+ next(null, selDevice, null);
253
+ });
254
+ }
255
+ ], function(err, result) {
256
+ next(err, result);
257
+ });
258
+ }
259
+
260
+ function _queryDeviceInfo(selDevice, next) {
261
+ let mode = selDevice.mode;
262
+ const deviceName = selDevice.name,
263
+ resolver = this.resolver || (this.resolver = new novacom.Resolver()),
264
+ defaultDeviceInfo = appdata.getConfig(true).defaultDeviceInfo;
265
+
266
+ questions = [{
267
+ type: "input",
268
+ name: "ip",
269
+ message: "Enter Device IP address:",
270
+ default: function() {
271
+ return selDevice.host || defaultDeviceInfo.ipAddress;
272
+ },
273
+ validate: function(answers) {
274
+ if (!setupDevice.isValidIpv4(answers)) {
275
+ return errHndl.getErrStr("INVALID_VALUE");
276
+ }
277
+ return true;
278
+ },
279
+ when: function() {
280
+ return _needInq(mode)(inqChoices);
281
+ }
282
+ }, {
283
+ type: "input",
284
+ name: "port",
285
+ message: "Enter Device Port:",
286
+ default: function() {
287
+ return selDevice.port || defaultDeviceInfo.port;
288
+ },
289
+ validate: function(answers) {
290
+ if (!setupDevice.isValidPort(answers)) {
291
+ return errHndl.getErrStr("INVALID_VALUE");
292
+ }
293
+ return true;
294
+ },
295
+ when: function() {
296
+ return _needInq(mode)(inqChoices);
297
+ }
298
+ }, {
299
+ type: "input",
300
+ name: "user",
301
+ message: "Enter ssh user:",
302
+ default: function() {
303
+ return selDevice.username || defaultDeviceInfo.user;
304
+ },
305
+ when: function() {
306
+ return _needInq(mode)(inqChoices);
307
+ }
308
+ }, {
309
+ type: "input",
310
+ name: "description",
311
+ message: "Enter description:",
312
+ default: function() {
313
+ return selDevice.description || defaultDeviceInfo.description;
314
+ },
315
+ when: function() {
316
+ return _needInq(mode)(inqChoices);
317
+ }
318
+ }, {
319
+ type: "list",
320
+ name: "auth_type",
321
+ message: "Select authentication",
322
+ choices: ["password", "ssh key"],
323
+ default: function() {
324
+ let idx = 0;
325
+ if (selDevice.privateKeyName) {
326
+ idx = 1;
327
+ }
328
+ return idx;
329
+ },
330
+ when: function(answers) {
331
+ return _needInq(mode)(inqChoices) && answers.user === "root";
332
+ }
333
+ }, {
334
+ type: "password",
335
+ name: "password",
336
+ message: "Enter password:",
337
+ when: function(answers) {
338
+ return _needInq(mode)(inqChoices) && (answers.auth_type === "password");
339
+ }
340
+ }, {
341
+ type: "input",
342
+ name: "ssh_key",
343
+ message: "Enter ssh private key file name:",
344
+ default: function() {
345
+ return selDevice.privateKeyName || "webos_emul";
346
+ },
347
+ when: function(answers) {
348
+ return _needInq(mode)(inqChoices) && (answers.auth_type === "ssh key");
349
+ }
350
+ }, {
351
+ type: "confirm",
352
+ name: "default",
353
+ message: "Set default ?",
354
+ default: false,
355
+ when: function() {
356
+ return (mode === "add");
357
+ }
358
+ }, {
359
+ type: "confirm",
360
+ name: "confirm",
361
+ message: "Save ?",
362
+ default: true
363
+ }];
364
+
365
+ inquirer.prompt(questions).then(function(answers) {
366
+ if (answers.confirm) {
367
+ log.info("interactiveInput()#_queryDeviceInfo()", "saved");
368
+ } else {
369
+ log.info("interactiveInput()#_queryDeviceInfo()", "canceled");
370
+ return next(null, {
371
+ "msg": "Canceled"
372
+ });
373
+ }
374
+ const inDevice = {
375
+ profile: appdata.getConfig(true).profile,
376
+ name: deviceName,
377
+ host: answers.ip,
378
+ port: answers.port,
379
+ description: answers.description,
380
+ username: answers.user,
381
+ default: answers.default
382
+ };
383
+
384
+ if (answers.user !== 'prisoner' && ["add", "modify"].includes(mode)) {
385
+ if (answers.auth_type && answers.auth_type === "password") {
386
+ inDevice.password = answers.password;
387
+ inDevice.privateKey = "@DELETE@";
388
+ inDevice.passphrase = "@DELETE@";
389
+ inDevice.privateKeyName = "@DELETE@";
390
+ } else if ((answers.auth_type && answers.auth_type === "ssh key") || answers.user === "developer") {
391
+ inDevice.password = "@DELETE@";
392
+ inDevice.privateKey = {
393
+ "openSsh": answers.ssh_key || "webos_emul"
394
+ };
395
+ inDevice.passphrase = answers.ssh_passphrase || "@DELETE@";
396
+ inDevice.privateKeyName = "@DELETE@";
397
+ } else {
398
+ return next(errHndl.getErrMsg("NOT_SUPPORT_AUTHTYPE", answers.auth_type));
399
+ }
400
+ }
401
+
402
+ if (mode === 'set default') {
403
+ inDevice.default = true;
404
+ mode = 'default';
405
+ }
406
+
407
+ setupDevice.replaceDefaultDeviceInfo(inDevice);
408
+ if (inDevice.port) {
409
+ inDevice.port = Number(inDevice.port);
410
+ }
411
+ async.series([
412
+ resolver.load.bind(resolver),
413
+ resolver.modifyDeviceFile.bind(resolver, mode, inDevice),
414
+ setupDevice.showDeviceList.bind(this),
415
+ ], function(err, results) {
416
+ if (err) {
417
+ return next(err);
418
+ }
419
+ if(results[2] && results[2].msg){
420
+ console.log(results[2].msg);
421
+ }
422
+ if(inDevice.username === 'prisoner' && mode === 'add'){
423
+ setupDevice.displayGetKeyGuide(inDevice.name);
424
+ }
425
+ finish();
426
+ });
427
+ });
428
+ }
429
+
430
+ function interactiveInput() {
431
+ async.waterfall([
432
+ setupDevice.showDeviceList.bind(this),
433
+ function(data, next) {
434
+ console.log(data.msg);
435
+ console.log("** You can modify the device info in the above list, or add new device.");
436
+ next();
437
+ },
438
+ _queryAddRemove,
439
+ _queryDeviceInfo
440
+ ], function(err, result) {
441
+ finish(err, result);
442
+ });
443
+ }
444
+
445
+ function search(next) {
446
+ const TIMEOUT = 5000,
447
+ ssdp = new Ssdp(),
448
+ timeout = Number(argv.timeout) * 1000 || TIMEOUT,
449
+ outterNext = next,
450
+ self = this;
451
+ let discovered = [],
452
+ end = false;
453
+
454
+ console.log("Searching...");
455
+ log.verbose("search()", "timeout:", timeout);
456
+ ssdp.start();
457
+ ssdp.onDevice(function(device) {
458
+ if (!device.headers || !device.headers.SERVER ||
459
+ device.headers.SERVER.indexOf('WebOS') < 0 || end) {
460
+ return finish(null, {msg: "No devices is discovered."});
461
+ }
462
+ log.verbose("search()# %s:%s (%s)", '[Discovered]', device.name, device.address);
463
+ });
464
+ async.waterfall([
465
+ function(next) {
466
+ setTimeout(function() {
467
+ discovered = ssdp.getList().map(function(device) {
468
+ end = true;
469
+ return {
470
+ "uuid": device.headers.USN.split(':')[1],
471
+ "name": device.name.replace(/\s/g, '_'),
472
+ "address": device.address,
473
+ "registered": false
474
+ };
475
+ });
476
+ // ssdp.destroy();
477
+ if (discovered.length === 0) {
478
+ console.log("No devices is discovered.");
479
+ return outterNext();
480
+ }
481
+ log.verbose("search()", "discovered:", discovered.length);
482
+ next(null, discovered);
483
+ }, timeout);
484
+ },
485
+ _queryAddRemove.bind(self),
486
+ _queryDeviceInfo.bind(self)
487
+ ], function(err) {
488
+ next(err);
489
+ });
490
+ }
491
+
492
+ function modifyDeviceInfo() {
493
+ setupDevice.modifyDeviceInfo(argv, finish);
494
+ }
495
+
496
+ function setDefaultDeviceInfo() {
497
+ setupDevice.setDefaultDevice(argv.default, finish);
498
+ }
499
+
500
+ function removeDeviceInfo() {
501
+ setupDevice.removeDeviceInfo(argv, finish);
502
+ }
503
+
504
+ function finish(err, value) {
505
+ log.info("finish()");
506
+ if (err) {
507
+ // handle err from getErrMsg()
508
+ if (Array.isArray(err) && err.length > 0) {
509
+ for (const index in err) {
510
+ log.error(err[index].heading, err[index].message);
511
+ }
512
+ log.verbose(err[0].stack);
513
+ } else {
514
+ // handle general err (string & object)
515
+ log.error(err.toString());
516
+ log.verbose(err.stack);
517
+ }
518
+ cliControl.end(-1);
519
+ } else {
520
+ log.verbose("finish()", "value:", value);
521
+ if (value && value.msg) {
522
+ console.log(value.msg);
523
+ }
524
+ cliControl.end();
525
+ }
526
+ }
527
+
528
+ function getArgv() {
529
+ let argv = nopt(knownOpts, shortHands, process.argv, 2 /* drop 'node' & 'ares-*.js'*/);
530
+ let mode = checkMode(argv);
531
+ return (mode && argv[mode] === 'true') ? reParse(argv, mode) : argv;
532
+ }
533
+
534
+ function checkMode(argv) {
535
+ if (argv.add) return 'add';
536
+ if (argv.modify) return 'modify';
537
+ if (argv.remove) return 'remove';
538
+ return (argv.default) ? 'default' : null;
539
+ }
540
+
541
+ function reParse(argv, mode) {
542
+ let nameIndex = -1;
543
+ let cookedNameIndex = -1;
544
+ let abbrevOpts = abbrev(Object.keys(knownOpts))
545
+
546
+ for (let i = 0; i < argv.argv.original.length; i++) {
547
+ if (argv.argv.original[i].match(/^-{2,}$/)) {
548
+ nameIndex = i;
549
+ break;
550
+ }
551
+ }
552
+ cookedNameIndex = argv.argv.cooked.indexOf('--');
553
+ if (cookedNameIndex <= 0 || !argv.argv.cooked[cookedNameIndex - 1].match(/^-/) || abbrevOpts[argv.argv.cooked[cookedNameIndex - 1].replace(/^-+/, "")] != mode) {
554
+ return argv;
555
+ }
556
+
557
+ argv[mode] = argv.argv.original[nameIndex];
558
+ if (nameIndex != -1 && cookedNameIndex != -1 && nameIndex + 1 < argv.argv.original.length) {
559
+ const remainArgv = nopt(knownOpts, shortHands, argv.argv.original, nameIndex + 1);
560
+ for (const option in remainArgv) {
561
+ if (remainArgv.hasOwnProperty(option)) {
562
+ if (!argv.hasOwnProperty(option)) {
563
+ argv[option] = remainArgv[option];
564
+ } else if (Array.isArray(argv[option])) {
565
+ argv[option] = argv[option].concat(remainArgv[option]);
566
+ }
567
+ }
568
+ }
569
+ argv.argv.remain = remainArgv.argv.remain;
570
+ argv.argv.cooked = argv.argv.cooked.splice(0, cookedNameIndex + 1).concat(remainArgv.argv.cooked);
571
+ }
572
+ return argv
573
+ }