-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
executable file
·428 lines (340 loc) · 12.1 KB
/
app.js
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
#!/usr/bin/env node
var ScreenManager = require("./bin/ScreenManager.js");
var CommandHandler = require("./bin/CommandHandler.js");
var HDB = require("./lib/HdbHandler.js");
var SettingsHandler = require("./lib/SettingsHandler.js");
var argv = require('optimist').argv;
var mkdirp = require('mkdirp');
var path = require('path');
var async = require("async");
var osHomedir = require('os-homedir');
var fs = require("fs");
var SavedConnectionManager = require("./bin/SavedConnectionManager.js");
var CliTable = require('cli-table3');
var pkg = require("./package.json");
var username = require('username');
var CenSql = function() {
/**
* Bodge to fix hdb module
* @type {Boolean}
*/
process.browser = true;
/**
* get a global object for storing flags in
*/
global.censql = {};
/**
* Dont accept user input until we're ready
* @type {Boolean}
*/
global.censql.RUNNING_PROCESS = true;
/**
* Init hana library
* @type {HDB}
*/
this.hdb = new HDB();
async.series([
this.createFolderIfNeeded.bind(this),
this.loadSettings.bind(this),
this.showHelpTextIfNeeded.bind(this),
this.createScreen.bind(this),
function(callback) {
/**
* Create a new command handler
*/
this.commandHandler = new CommandHandler(this.screen, this.hdb, this.settings);
callback(null, null);
}.bind(this)
], function() {
this.connManager = new SavedConnectionManager();
/**
* Should version
*/
if (argv.version) {
this.showVersion();
return;
}
/**
* Should list connections
*/
if (argv.l || argv.list_connections) {
this.listConfiguredConnectionNames();
return;
}
/**
* Should check all connections
*/
if (argv.t || argv.connection_test) {
this.testSavedConnections();
return;
}
/**
* Should forget a connection
*/
if (argv.forget) {
this.connManager.delete(argv.forget);
console.log(("Forgot " + argv.forget + "!").green);
return;
}
var connDetails = argv;
if (argv.use) {
connDetails = this.connManager.get(argv.use);
}
if (connDetails) {
this.screen.init();
this.connectToHdb(connDetails.host, connDetails.user, connDetails.pass, connDetails.port, connDetails.tenant);
} else {
if (argv.use.length > 0) {
this.screen.print("Connection '".red + argv.use.red.bold + ("' does not exist. Use the \\save command whilst connected to save a connection.").red, function() {
console.log();
process.exit(1)
})
} else {
this.showHelp();
}
}
}.bind(this))
}
CenSql.prototype.loadSettings = function(callback){
/**
* Get settings
* @type {Object}
*/
this.settingsHandler = new SettingsHandler();
this.settingsHandler.getSettings(function(err, settings){
if(err){
callback(err);
return
}
this.settings = settings;
callback(null);
}.bind(this));
}
CenSql.prototype.testSavedConnections = function() {
var contents = this.connManager.getAll();
var keys = Object.keys(contents);
keys = keys.sort(function(a, b) {
if (a.toLowerCase() > b.toLowerCase()) return 1;
if (b.toLowerCase() > a.toLowerCase()) return -1;
return 0;
})
async.mapLimit(keys, 50, function(key, callback) {
var entry = contents[key];
var db = new HDB();
/**
* Connect to HANA with the login info supplied by the user
*/
db.connect(entry["host"], entry["user"], entry["pass"], entry["port"], entry["tenant"], "tmp_" + key, function(err, data) {
if (err) {
callback(null, {
status: 1,
key: key,
message: "Error connecting: " + err
});
return;
}
db.close();
callback(null, {
status: 0,
key: key,
message: "Connected Successfully"
});
}.bind(this))
}, function(err, output) {
if (err) {
console.log(err);
return;
}
output = output.sort(function(a, b) {
return a.key.localeCompare(b.key);
})
var table = new CliTable({
chars: (new(require("./lib/CharacterCodeIndex.js"))).tableChars
});
table.push(["Name".bold, "Status".bold, "Details".bold])
for (var i = 0; i < output.length; i++) {
table.push([output[i].key.bold, (output[i].status == 0 ? "OK".green.bold : "ERROR".red.bold), output[i].message])
}
console.log(table.toString());
process.exit(0);
})
}
CenSql.prototype.showVersion = function() {
console.log(pkg.version)
process.exit(0);
}
CenSql.prototype.listConfiguredConnectionNames = function() {
var contents = this.connManager.getAll();
var names = Object.keys(contents);
names = names.sort(function(a, b) {
if (a.toLowerCase() > b.toLowerCase()) return 1;
if (b.toLowerCase() > a.toLowerCase()) return -1;
return 0;
})
var table = new CliTable({
chars: (new(require("./lib/CharacterCodeIndex.js"))).tableChars
});
table.push(["Name".bold, "User".bold, "Host".bold, "Port".bold, "Tenant".bold]);
for (var i = 0; i < names.length; i++) {
table.push([names[i].bold, contents[names[i]].user, contents[names[i]].host, contents[names[i]].port, contents[names[i]].tenant || "N/A"])
}
console.log(table.toString());
}
CenSql.prototype.setTitle = function(isStudio) {
var title = "CenSQL" + (isStudio ? " Studio" : "");
if (argv.use) title += ' - ' + argv.use;
process.stdout.write(String.fromCharCode(27) + "]0;" + title + String.fromCharCode(7));
process.title = title;
}
CenSql.prototype.connectToHdb = function(host, user, pass, port, tenant) {
/**
* Connect to HANA with the login info supplied by the user
*/
this.hdb.connect(host, user, pass, port, tenant, "conn", function(err, data) {
if (err) {
this.screen.error(err.message + "\n", function() {
process.exit(1);
});
return;
}
this.setConnectionMetaData("conn", function(err) {
if (err) {
this.screen.error(err.message + "\n", function() {
process.exit(1);
});
return;
}
/**
* If the user specified the command run it, otherwise, open an interactive session
*/
if (!argv.command) {
this.startCLI(user);
} else {
this.runBatchCommand();
}
}.bind(this))
}.bind(this))
}
CenSql.prototype.startCLI = function(user) {
this.hdb.exec("conn", "SELECT (SELECT SYSTEM_ID FROM SYS.M_DATABASE) AS SYSTEM_ID, (SELECT DATABASE_NAME FROM SYS.M_DATABASE) AS DATABASE_NAME, (SELECT USAGE FROM SYS.M_DATABASE) AS USAGE, CURRENT_SCHEMA FROM DUMMY", function(err, data) {
/**
* Allow user inpput from now on
* @type {Boolean}
*/
global.censql.RUNNING_PROCESS = false;
if (err) {
this.screen.ready(this.hdb, user, null, null, null);
return;
} else {
this.screen.ready(this.hdb, user, data[0].SYSTEM_ID, data[0].DATABASE_NAME, data[0].USAGE, data[0].CURRENT_SCHEMA);
}
}.bind(this))
}
CenSql.prototype.runBatchCommand = function() {
this.screen.readyBatch();
var command = argv.command;
/**
* Odd input can occur when running from some terminals
*/
if(typeof command !== "string"){
command = "";
}
this.commandHandler.onCommand(command, function(err, output) {
this.screen.printCommandOutput(command, output, function() {
process.exit(0)
});
}.bind(this));
}
CenSql.prototype.setConnectionMetaData = function(connId, callback) {
async.parallel([
function(callback) {
this.hdb.exec("conn", "SET 'APPLICATION' = 'CenSQL'", callback)
}.bind(this),
function(callback) {
this.hdb.exec("conn", "SET 'APPLICATIONVERSION' = '" + pkg.version + "'", callback)
}.bind(this),
function(callback) {
this.hdb.exec("conn", "SET 'APPLICATIONUSER' = '" + username.sync() + "'", callback)
}.bind(this)
], function(err, res) {
callback(err, null);
})
}
CenSql.prototype.createScreen = function(callback) {
/**
* keep the force_nocolour var in settings for easy access
* @type {boolean}
*/
this.settings.force_nocolour = argv['no-colour'] || argv['no-color'] ? true : false;
this.settings.studio = !!argv['studio'] || !!argv['s'];
/**
* Create screen object adn give it the command handler to handle user input
*/
this.screen = new ScreenManager(
// Command if given
!!argv.command,
this.settings,
// Command Handler
{
handleCommand: function(command, callback) {
this.commandHandler.onCommand(command, callback);
}.bind(this),
getActiveSchema: function(callback) {
this.commandHandler.getActiveSchema(callback);
}.bind(this),
getHandlers: function() {
return this.commandHandler.handlers
}.bind(this)
}
)
if (!argv.command) {
this.setTitle(this.settings.studio);
}
callback.call(this, null);
}
CenSql.prototype.showHelpTextIfNeeded = function(callback) {
/**
* Make sure we have the arguments needed to connect to HANA
*/
if (((!argv.host || !argv.user || !argv.pass || !argv.port) && (!argv.use || argv.use.length == 0) && !(argv.l || argv.list_connections) && !(argv.t || argv.connection_test) && !argv.forget && !argv.version) || argv.help) {
this.showHelp();
}
callback.call(this, null);
}
CenSql.prototype.showHelp = function() {
console.log([
"Usage:\t censql --user <USER> --port 3<ID>15 --host <IP OR HOSTNAME> --pass <PASSWORD>",
"\t censql --user <USER> --port 3<ID>15 --host <IP OR HOSTNAME> --pass <PASSWORD> --command '<SQL_STRING>'",
"\t censql --use <ALIAS>",
"Example: censql --user SYSTEM --port 30015 --host 192.168.0.1 --pass Password123",
"Example: censql --user SYSTEM --port 30015 --host 192.168.0.1 --pass Password123 --command 'SELECT * FROM SYS.M_SERVICES'",
"Example: censql --use DEV1",
"",
"CenSQL Help",
"--user\t\tThe username for the user to connect as",
"--pass\t\tThe password for the user connecting with",
"--host\t\tThe host to connect to",
"--port\t\tThe port to connect to the host with (Layout: '3<ID>15', Instance 99 would be 39915)",
"--tenant\t\tName of the tenant database if on a multitenant system",
"--use <ALIAS>\tConnect using a saved connection instead of user,pass,host,port",
"",
"--command\t\tOptionally run a command/sql without entering the interective terminal",
"-s --studio\t\tEnter studio mode",
"",
"-l --list_connections\tList saved connections",
"-t --connection_test\tConnect to every saved connection and check HANA's status",
"--forget <NAME>\t\tForget a saved connection",
"",
"--no-colour\t\tDisable colour output",
"--no-color\t\tAlias of --no-colour",
"",
"--version\tShow CenSQL version",
].join("\n"));
process.exit(0);
}
CenSql.prototype.createFolderIfNeeded = function(callback) {
var location = path.join(osHomedir(), ".censql");
mkdirp.sync(location, 0777);
callback(null);
};
new CenSql();