@cyklang/cli 0.5.6 → 0.7.0

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,5 +1,14 @@
1
1
  #!/usr/bin/env ts-node
2
+ import { DBManager } from "@cyklang/core";
3
+ import loglevel from 'loglevel';
2
4
  import { Command } from "commander";
5
+ export declare const logger: loglevel.Logger;
3
6
  export declare class TableCommand extends Command {
4
7
  constructor(name: string);
5
8
  }
9
+ /**
10
+ * function dropTable
11
+ * @param table
12
+ * @param dbManager
13
+ */
14
+ export declare function dropTable(table: string, dbManager: DBManager): Promise<void>;
@@ -27,7 +27,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
27
27
  return (mod && mod.__esModule) ? mod : { "default": mod };
28
28
  };
29
29
  Object.defineProperty(exports, "__esModule", { value: true });
30
- exports.TableCommand = void 0;
30
+ exports.dropTable = exports.TableCommand = exports.logger = void 0;
31
31
  const fs = __importStar(require("fs"));
32
32
  const loglevel_1 = __importDefault(require("loglevel"));
33
33
  const commander_1 = require("commander");
@@ -35,8 +35,9 @@ const child_process_1 = require("child_process");
35
35
  const Cmd_1 = require("./Cmd");
36
36
  const DBClient_1 = require("./DBClient");
37
37
  const form_data_1 = __importDefault(require("form-data"));
38
- const logger = loglevel_1.default.getLogger("TableCommand.ts");
39
- logger.setLevel("debug");
38
+ const inquirer_1 = __importDefault(require("inquirer"));
39
+ exports.logger = loglevel_1.default.getLogger("TableCommand.ts");
40
+ exports.logger.setLevel("debug");
40
41
  class TableCommand extends commander_1.Command {
41
42
  constructor(name) {
42
43
  super(name);
@@ -45,8 +46,7 @@ class TableCommand extends commander_1.Command {
45
46
  this.addCommand(new TableImportCmd());
46
47
  this.addCommand(new TableList('list', 'list database tables'));
47
48
  this.addCommand(new TableList('l', '(l)ist database tables'));
48
- this.addCommand(new TableData('data', 'table (d)ata'));
49
- this.addCommand(new TableData('d', 'table (d)ata'));
49
+ this.addCommand(new TableDropCommand('drop', 'drop table'));
50
50
  this.addCommand(new TableUpdate('update', 'update table properties'));
51
51
  this.addCommand(new TableUpdate('u', 'shortcut for (u)pdate table properties'));
52
52
  }
@@ -84,7 +84,7 @@ class TableUpdate extends Cmd_1.Cmd {
84
84
  await this.updateAuthAccess('cyk_table', 'table', dbTable.id || '', options);
85
85
  }
86
86
  catch (err) {
87
- logger.error(err);
87
+ exports.logger.error(err);
88
88
  }
89
89
  }
90
90
  }
@@ -115,7 +115,7 @@ class TableExportCmd extends Cmd_1.Cmd {
115
115
  const tableName = tables[indi];
116
116
  const dbTable = await this.dbManager.dbTableExist(tableName);
117
117
  if (dbTable === undefined) {
118
- logger.info('table ' + tableName + ' not found');
118
+ exports.logger.info('table ' + tableName + ' not found');
119
119
  continue;
120
120
  }
121
121
  const filename = buildFilename(dbTable.name, options);
@@ -123,7 +123,7 @@ class TableExportCmd extends Cmd_1.Cmd {
123
123
  }
124
124
  }
125
125
  catch (err) {
126
- logger.error(err);
126
+ exports.logger.error(err);
127
127
  }
128
128
  }
129
129
  async copyTo(dbTable, filename) {
@@ -134,15 +134,15 @@ class TableExportCmd extends Cmd_1.Cmd {
134
134
  const response = await this.dbRemote.apiServer.get('/admin/export/' + dbTable.name, { responseType: 'stream' });
135
135
  const stream = response;
136
136
  stream.on('data', (data) => {
137
- logger.debug('**** DATA ****', data.length);
137
+ exports.logger.debug('**** DATA ****', data.length);
138
138
  const textDecoder = new TextDecoder();
139
139
  const chunk = (textDecoder.decode(data));
140
140
  writeStream.write(chunk);
141
141
  });
142
- stream.on('end', () => logger.debug('**** DONE ****'));
142
+ stream.on('end', () => exports.logger.debug('**** DONE ****'));
143
143
  }
144
144
  catch (err) {
145
- logger.error(err);
145
+ exports.logger.error(err);
146
146
  }
147
147
  }
148
148
  }
@@ -173,7 +173,7 @@ class TableImportCmd extends Cmd_1.Cmd {
173
173
  const tableName = tables[indi];
174
174
  const dbTable = await this.dbManager.dbTableExist(tableName);
175
175
  if (dbTable === undefined) {
176
- logger.info('table ' + tableName + ' not found');
176
+ exports.logger.info('table ' + tableName + ' not found');
177
177
  continue;
178
178
  }
179
179
  const filename = buildFilename(dbTable.name, options);
@@ -181,7 +181,7 @@ class TableImportCmd extends Cmd_1.Cmd {
181
181
  }
182
182
  }
183
183
  catch (err) {
184
- logger.error(err);
184
+ exports.logger.error(err);
185
185
  }
186
186
  }
187
187
  async copyFrom(dbTable, filename) {
@@ -194,11 +194,11 @@ class TableImportCmd extends Cmd_1.Cmd {
194
194
  const route = '/admin/import/' + dbTable.name;
195
195
  const resp = await this.dbRemote.apiServer.post(route, form);
196
196
  if (resp.status === 200) {
197
- logger.debug(filename + ' uploaded');
197
+ exports.logger.debug(filename + ' uploaded');
198
198
  }
199
199
  }
200
200
  catch (err) {
201
- logger.error(err);
201
+ exports.logger.error(err);
202
202
  }
203
203
  }
204
204
  }
@@ -226,7 +226,7 @@ function buildFilename(tableName, options) {
226
226
  return filename;
227
227
  }
228
228
  const commandImportExport = async (dbManager, direction, tables, options) => {
229
- logger.debug('tables : ', tables, 'options : ', options);
229
+ exports.logger.debug('tables : ', tables, 'options : ', options);
230
230
  try {
231
231
  if (options.file === undefined && options.dir === undefined)
232
232
  throw '--file or --dir mandatory';
@@ -240,14 +240,14 @@ const commandImportExport = async (dbManager, direction, tables, options) => {
240
240
  const tableName = tables[indi];
241
241
  const dbTable = await dbManager.dbTableExist(tableName);
242
242
  if (dbTable === undefined) {
243
- logger.info('table ' + tableName + ' not found');
243
+ exports.logger.info('table ' + tableName + ' not found');
244
244
  continue;
245
245
  }
246
246
  psqlCmd(dbTable, direction, options);
247
247
  }
248
248
  }
249
249
  catch (err) {
250
- logger.error(err);
250
+ exports.logger.error(err);
251
251
  }
252
252
  };
253
253
  //--------------------------------------------------------------------------------------------------------------------
@@ -282,7 +282,7 @@ function psqlCmd(dbTable, direction, options) {
282
282
  else if (fs.existsSync(options.dir) === false)
283
283
  throw 'directory ' + options.dir + ' does not exist';
284
284
  else if (fs.lstatSync(options.dir).isFile()) {
285
- logger.debug("--dir " + options.dir + " is a file ");
285
+ exports.logger.debug("--dir " + options.dir + " is a file ");
286
286
  }
287
287
  else {
288
288
  let directory = options.dir.trim();
@@ -293,7 +293,7 @@ function psqlCmd(dbTable, direction, options) {
293
293
  }
294
294
  let copy_cmd = "\\copy " + dbTable.name + lcols + direction + " '" + filename + "' csv header";
295
295
  const shell_cmd = psql_cmd + '"' + copy_cmd + '"';
296
- logger.debug(shell_cmd);
296
+ exports.logger.debug(shell_cmd);
297
297
  const child = (0, child_process_1.spawn)(shell_cmd.trim(), { stdio: 'inherit', shell: true });
298
298
  }
299
299
  //--------------------------------------------------------------------------------------------------------------------
@@ -316,57 +316,43 @@ class TableList extends Cmd_1.Cmd {
316
316
  dbClient.selectFromTable('List of Tables', 'cyk_table', { fields: 'table_id,table_name,table_auth,table_access,table_description', sort: options.sort || '1' });
317
317
  }
318
318
  }
319
- //--------------------------------------------------------------------------------------------------------------------
320
- // class TableData
321
- //--------------------------------------------------------------------------------------------------------------------
322
- class TableData extends Cmd_1.Cmd {
319
+ class TableDropCommand extends Cmd_1.Cmd {
323
320
  constructor(name, description) {
324
321
  super(name);
325
322
  this.description(description)
326
323
  .argument('<table>', 'table name')
327
- .option('--where <clause_where_sql>', 'criteria in SQL syntax')
328
- .option('-s --sort <columns>', 'sort by columns positions (begins by 0) separated by comma')
329
- .option('-w --width <colwidth>', 'columns widths separated by comma')
330
324
  .action(async (table, options) => {
331
- this.commandData(table, options);
325
+ await this.commandDrop(table, options);
332
326
  });
333
327
  }
334
- async commandData(table, options) {
328
+ async commandDrop(table, options) {
335
329
  try {
336
330
  await this.prologue(options);
337
- if (this.dbManager === undefined)
331
+ if (!this.dbManager)
338
332
  throw 'dbManager undefined';
339
333
  const dbTable = await this.dbManager.dbTableExist(table);
340
- if (dbTable === undefined)
341
- throw 'table ' + table + ' not found';
342
- let fields = '';
343
- for (let ind = 0; ind < dbTable.columns.length; ind++) {
344
- const dbColumn = dbTable.columns[ind];
345
- let ok = false;
346
- let fieldName = dbColumn.name;
347
- if (dbColumn.dbType === 'text' || dbColumn.dbType === 'bytea') {
348
- ok = false;
349
- fieldName = 'length(' + dbColumn.name + ')';
350
- }
351
- else {
352
- ok = true;
353
- }
354
- if (ok === true) {
355
- if (fields !== '')
356
- fields += ',';
357
- fields += fieldName;
358
- }
334
+ if (!dbTable) {
335
+ console.log('table ' + table + ' does not exist');
336
+ return;
359
337
  }
360
- logger.debug('commandQuery', table);
361
- const dbClient = new DBClient_1.DBClient(this.dbManager);
362
- let title = 'Table ' + table;
363
- if (options.where !== undefined) {
364
- title += ' where ' + options.where;
338
+ const reply = await inquirer_1.default.prompt({ type: 'confirm', name: 'confirm', message: 'Drop table ' + table + '?' });
339
+ if (reply.confirm && this.dbManager) {
340
+ await dropTable(table, this.dbManager);
365
341
  }
366
- dbClient.selectFromTable(title, table, { fields: fields, width: options.width, sort: options.sort || '1', where: options.where });
367
342
  }
368
343
  catch (err) {
369
- logger.error(err);
344
+ exports.logger.error(err);
370
345
  }
371
346
  }
372
347
  }
348
+ /**
349
+ * function dropTable
350
+ * @param table
351
+ * @param dbManager
352
+ */
353
+ async function dropTable(table, dbManager) {
354
+ const dbTable = await dbManager.dbTableExist(table);
355
+ if (dbTable)
356
+ await dbManager.dbTableDelete(dbTable);
357
+ }
358
+ exports.dropTable = dropTable;
@@ -0,0 +1,5 @@
1
+ import { Cmd } from "./Cmd";
2
+ export declare class TableDataCommand extends Cmd {
3
+ constructor(name: string, description: string);
4
+ commandData(table: string, options: any): Promise<void>;
5
+ }
@@ -0,0 +1,61 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.TableDataCommand = void 0;
4
+ const Cmd_1 = require("./Cmd");
5
+ const DBClient_1 = require("./DBClient");
6
+ const TableCommand_1 = require("./TableCommand");
7
+ //--------------------------------------------------------------------------------------------------------------------
8
+ // class TableData
9
+ //--------------------------------------------------------------------------------------------------------------------
10
+ class TableDataCommand extends Cmd_1.Cmd {
11
+ constructor(name, description) {
12
+ super(name);
13
+ this.description(description)
14
+ .argument('<table>', 'table name')
15
+ .option('--where <clause_where_sql>', 'criteria in SQL syntax')
16
+ .option('-s --sort <columns>', 'sort by columns positions (begins by 0) separated by comma')
17
+ .option('-w --width <colwidth>', 'columns widths separated by comma')
18
+ .action(async (table, options) => {
19
+ this.commandData(table, options);
20
+ });
21
+ }
22
+ async commandData(table, options) {
23
+ try {
24
+ await this.prologue(options);
25
+ if (this.dbManager === undefined)
26
+ throw 'dbManager undefined';
27
+ const dbTable = await this.dbManager.dbTableExist(table);
28
+ if (dbTable === undefined)
29
+ throw 'table ' + table + ' not found';
30
+ let fields = '';
31
+ for (let ind = 0; ind < dbTable.columns.length; ind++) {
32
+ const dbColumn = dbTable.columns[ind];
33
+ let ok = false;
34
+ let fieldName = dbColumn.name;
35
+ if (dbColumn.dbType === 'text' || dbColumn.dbType === 'bytea') {
36
+ ok = false;
37
+ fieldName = 'length(' + dbColumn.name + ')';
38
+ }
39
+ else {
40
+ ok = true;
41
+ }
42
+ if (ok === true) {
43
+ if (fields !== '')
44
+ fields += ',';
45
+ fields += fieldName;
46
+ }
47
+ }
48
+ TableCommand_1.logger.debug('commandQuery', table);
49
+ const dbClient = new DBClient_1.DBClient(this.dbManager);
50
+ let title = 'Table ' + table;
51
+ if (options.where !== undefined) {
52
+ title += ' where ' + options.where;
53
+ }
54
+ dbClient.selectFromTable(title, table, { fields: fields, width: options.width, sort: options.sort || '1', where: options.where });
55
+ }
56
+ catch (err) {
57
+ TableCommand_1.logger.error(err);
58
+ }
59
+ }
60
+ }
61
+ exports.TableDataCommand = TableDataCommand;
@@ -0,0 +1,5 @@
1
+ import { Cmd } from "./Cmd";
2
+ export declare class TableDataCommand extends Cmd {
3
+ constructor(name: string, description: string);
4
+ commandData(table: string, options: any): Promise<void>;
5
+ }
@@ -0,0 +1,61 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.TableDataCommand = void 0;
4
+ const Cmd_1 = require("./Cmd");
5
+ const DBClient_1 = require("./DBClient");
6
+ const TableCommand_1 = require("./TableCommand");
7
+ //--------------------------------------------------------------------------------------------------------------------
8
+ // class TableData
9
+ //--------------------------------------------------------------------------------------------------------------------
10
+ class TableDataCommand extends Cmd_1.Cmd {
11
+ constructor(name, description) {
12
+ super(name);
13
+ this.description(description)
14
+ .argument('<table>', 'table name')
15
+ .option('--where <clause_where_sql>', 'criteria in SQL syntax')
16
+ .option('-s --sort <columns>', 'sort by columns positions (begins by 0) separated by comma')
17
+ .option('-w --width <colwidth>', 'columns widths separated by comma')
18
+ .action(async (table, options) => {
19
+ this.commandData(table, options);
20
+ });
21
+ }
22
+ async commandData(table, options) {
23
+ try {
24
+ await this.prologue(options);
25
+ if (this.dbManager === undefined)
26
+ throw 'dbManager undefined';
27
+ const dbTable = await this.dbManager.dbTableExist(table);
28
+ if (dbTable === undefined)
29
+ throw 'table ' + table + ' not found';
30
+ let fields = '';
31
+ for (let ind = 0; ind < dbTable.columns.length; ind++) {
32
+ const dbColumn = dbTable.columns[ind];
33
+ let ok = false;
34
+ let fieldName = dbColumn.name;
35
+ if (dbColumn.dbType === 'text' || dbColumn.dbType === 'bytea') {
36
+ ok = false;
37
+ fieldName = 'length(' + dbColumn.name + ')';
38
+ }
39
+ else {
40
+ ok = true;
41
+ }
42
+ if (ok === true) {
43
+ if (fields !== '')
44
+ fields += ',';
45
+ fields += fieldName;
46
+ }
47
+ }
48
+ TableCommand_1.logger.debug('commandQuery', table);
49
+ const dbClient = new DBClient_1.DBClient(this.dbManager);
50
+ let title = 'Table ' + table;
51
+ if (options.where !== undefined) {
52
+ title += ' where ' + options.where;
53
+ }
54
+ dbClient.selectFromTable(title, table, { fields: fields, width: options.width, sort: options.sort || '1', where: options.where });
55
+ }
56
+ catch (err) {
57
+ TableCommand_1.logger.error(err);
58
+ }
59
+ }
60
+ }
61
+ exports.TableDataCommand = TableDataCommand;
package/build/index.js CHANGED
@@ -1,26 +1,148 @@
1
1
  #!/usr/bin/env node
2
2
  "use strict";
3
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
4
+ if (k2 === undefined) k2 = k;
5
+ var desc = Object.getOwnPropertyDescriptor(m, k);
6
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
7
+ desc = { enumerable: true, get: function() { return m[k]; } };
8
+ }
9
+ Object.defineProperty(o, k2, desc);
10
+ }) : (function(o, m, k, k2) {
11
+ if (k2 === undefined) k2 = k;
12
+ o[k2] = m[k];
13
+ }));
14
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
15
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
16
+ }) : function(o, v) {
17
+ o["default"] = v;
18
+ });
19
+ var __importStar = (this && this.__importStar) || function (mod) {
20
+ if (mod && mod.__esModule) return mod;
21
+ var result = {};
22
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
23
+ __setModuleDefault(result, mod);
24
+ return result;
25
+ };
3
26
  var __importDefault = (this && this.__importDefault) || function (mod) {
4
27
  return (mod && mod.__esModule) ? mod : { "default": mod };
5
28
  };
6
29
  Object.defineProperty(exports, "__esModule", { value: true });
7
- const loglevel_1 = __importDefault(require("loglevel"));
8
30
  const commander_1 = require("commander");
31
+ const fs = __importStar(require("fs"));
32
+ const loglevel_1 = __importDefault(require("loglevel"));
33
+ const path_1 = __importDefault(require("path"));
34
+ const AssetCommand_1 = require("./AssetCommand");
35
+ const Cmd_1 = require("./Cmd");
36
+ const InstallCommand_1 = require("./InstallCommand");
9
37
  const ModuleCommand_1 = require("./ModuleCommand");
10
- const UserCommand_1 = require("./UserCommand");
38
+ const OpenCommand_1 = require("./OpenCommand");
11
39
  const QueryCommand_1 = require("./QueryCommand");
40
+ const RunCommand_1 = require("./RunCommand");
12
41
  const TableCommand_1 = require("./TableCommand");
42
+ const TableDataCommand_1 = require("./TableDataCommand");
13
43
  const TestCommand_1 = require("./TestCommand");
14
- const RunCommand_1 = require("./RunCommand");
15
- const InstallCommand_1 = require("./InstallCommand");
16
- const AssetCommand_1 = require("./AssetCommand");
17
- const OpenCommand_1 = require("./OpenCommand");
18
44
  const UninstallCommand_1 = require("./UninstallCommand");
45
+ const UserCommand_1 = require("./UserCommand");
19
46
  const logger = loglevel_1.default.getLogger("index.ts");
20
47
  logger.setLevel("debug");
48
+ /**
49
+ * function getOrchestrator
50
+ */
51
+ function getOrchestrator() {
52
+ let result;
53
+ const cwd = process.cwd();
54
+ const dockerComposePath = path_1.default.join(cwd, 'docker-compose.yml');
55
+ const k8sPath = path_1.default.join(cwd, 'k8s');
56
+ if (fs.existsSync(dockerComposePath) && fs.statSync(dockerComposePath).isFile()) {
57
+ result = 'docker';
58
+ }
59
+ else if (fs.existsSync(k8sPath) && fs.statSync(k8sPath).isDirectory()) {
60
+ result = 'kubernetes';
61
+ }
62
+ return result;
63
+ }
64
+ /**
65
+ * function startServer
66
+ */
67
+ async function startServer() {
68
+ try {
69
+ const orchestrator = getOrchestrator();
70
+ if (orchestrator === 'docker') {
71
+ await (0, Cmd_1.spawnCommand)('docker', ['compose', 'up', '-d']);
72
+ }
73
+ if (orchestrator === 'kubernetes') {
74
+ const portForwardPath = path_1.default.join(process.cwd(), 'k8s', 'port-forward');
75
+ if (!fs.existsSync(portForwardPath) || !fs.statSync(portForwardPath).isFile())
76
+ throw portForwardPath + ' file not found';
77
+ const config = JSON.parse(fs.readFileSync(portForwardPath).toString());
78
+ if (!config.postgres || !config.nodejs)
79
+ throw portForwardPath + ' : file format incorrect';
80
+ (0, Cmd_1.spawnCommand)('kubectl', ['port-forward', 'svc/postgres', config.postgres + ':5432']);
81
+ logger.info('Postgresql proxy at localhost:' + config.postgres);
82
+ (0, Cmd_1.spawnCommand)('kubectl', ['port-forward', 'svc/nodejs', config.nodejs + ':3000']);
83
+ logger.info('Nodejs proxy at localhost:' + config.nodejs);
84
+ }
85
+ if (!orchestrator) {
86
+ logger.error('No cyk server is defined in current folder ' + process.cwd());
87
+ }
88
+ }
89
+ catch (err) {
90
+ console.error(err);
91
+ }
92
+ }
93
+ /**
94
+ * function stopServer
95
+ */
96
+ async function stopServer() {
97
+ try {
98
+ const orchestrator = getOrchestrator();
99
+ if (orchestrator === 'docker') {
100
+ await (0, Cmd_1.spawnCommand)('docker', ['compose', 'down']);
101
+ }
102
+ if (orchestrator === 'kubernetes') {
103
+ await (0, Cmd_1.spawnCommand)('kubectl', ['delete', '-f', path_1.default.join(process.cwd(), 'k8s')]);
104
+ logger.info('Kubernetes deployments and services deleted');
105
+ }
106
+ if (!orchestrator) {
107
+ logger.error('No cyk server is defined in current folder ' + process.cwd());
108
+ }
109
+ }
110
+ catch (err) {
111
+ console.error(err);
112
+ }
113
+ }
114
+ /**
115
+ * class StartCommand
116
+ */
117
+ class StartCommand extends commander_1.Command {
118
+ constructor(name) {
119
+ super(name);
120
+ this.description('start cyk server defined in current project folder')
121
+ .action(async (options) => {
122
+ await startServer();
123
+ });
124
+ }
125
+ }
126
+ /**
127
+ * class StopCommand
128
+ */
129
+ class StopCommand extends commander_1.Command {
130
+ constructor(name) {
131
+ super(name);
132
+ this.description('stop cyk server defined in current project folder')
133
+ .action(async (options) => {
134
+ await stopServer();
135
+ });
136
+ }
137
+ }
138
+ /**
139
+ * Main program
140
+ */
21
141
  const program = new commander_1.Command();
22
142
  program.name('cyk').description('cyklang CLI')
23
- .version('0.5.6');
143
+ .version('0.6.0');
144
+ program.addCommand(new StartCommand('start'));
145
+ program.addCommand(new StopCommand('stop'));
24
146
  program.addCommand(new InstallCommand_1.InstallCommand('install'));
25
147
  program.addCommand(new InstallCommand_1.InstallCommand('i'));
26
148
  program.addCommand(new UninstallCommand_1.UninstallCommand('uninstall'));
@@ -34,6 +156,8 @@ program.addCommand(new QueryCommand_1.QueryCommand('query'));
34
156
  program.addCommand(new QueryCommand_1.QueryCommand('q'));
35
157
  program.addCommand(new TableCommand_1.TableCommand('table'));
36
158
  program.addCommand(new TableCommand_1.TableCommand('t'));
159
+ program.addCommand(new TableDataCommand_1.TableDataCommand('data', 'show table data'));
160
+ program.addCommand(new TableDataCommand_1.TableDataCommand('d', 'show table data'));
37
161
  program.addCommand(new TestCommand_1.TestCommand());
38
162
  program.addCommand(new RunCommand_1.RunCommand('run'));
39
163
  program.addCommand(new RunCommand_1.RunCommand('r'));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cyklang/cli",
3
- "version": "0.5.6",
3
+ "version": "0.7.0",
4
4
  "description": "cyklang CLI",
5
5
  "main": "build/index.js",
6
6
  "scripts": {
@@ -17,7 +17,7 @@
17
17
  "author": "",
18
18
  "license": "ISC",
19
19
  "dependencies": {
20
- "@cyklang/core": "^0.5.3",
20
+ "@cyklang/core": "^0.5.4",
21
21
  "commander": "^9.4.0",
22
22
  "dotenv": "^16.0.1",
23
23
  "form-data": "^4.0.0",
@@ -8,8 +8,10 @@ import { Cmd } from "./Cmd"
8
8
  import { DBClient } from "./DBClient"
9
9
  import { Stream } from "form-data"
10
10
  import FormData from 'form-data'
11
+ import inquirer from 'inquirer'
12
+ import { TableDataCommand } from "./TableDataCommand"
11
13
 
12
- const logger = loglevel.getLogger("TableCommand.ts")
14
+ export const logger = loglevel.getLogger("TableCommand.ts")
13
15
  logger.setLevel("debug")
14
16
 
15
17
  export class TableCommand extends Command {
@@ -20,8 +22,7 @@ export class TableCommand extends Command {
20
22
  this.addCommand(new TableImportCmd())
21
23
  this.addCommand(new TableList('list', 'list database tables'))
22
24
  this.addCommand(new TableList('l', '(l)ist database tables'))
23
- this.addCommand(new TableData('data', 'table (d)ata'))
24
- this.addCommand(new TableData('d', 'table (d)ata'))
25
+ this.addCommand(new TableDropCommand('drop', 'drop table'))
25
26
  this.addCommand(new TableUpdate('update', 'update table properties'))
26
27
  this.addCommand(new TableUpdate('u', 'shortcut for (u)pdate table properties'))
27
28
  }
@@ -40,12 +41,12 @@ class TableUpdate extends Cmd {
40
41
  constructor(name: string, description: string) {
41
42
  super(name)
42
43
  this.description(description)
43
- .option('-n --name <table_name>', 'name of the table whose properties are to be changed')
44
- .option('--auth <auth_schema>', 'authentication schema : basic | token | cookie | any | none, default is none')
45
- .option('--access <access>', 'access rights required')
46
- .action(async (options: any) => {
47
- await this.commandUpdate(options)
48
- })
44
+ .option('-n --name <table_name>', 'name of the table whose properties are to be changed')
45
+ .option('--auth <auth_schema>', 'authentication schema : basic | token | cookie | any | none, default is none')
46
+ .option('--access <access>', 'access rights required')
47
+ .action(async (options: any) => {
48
+ await this.commandUpdate(options)
49
+ })
49
50
  }
50
51
 
51
52
  async commandUpdate(options: any) {
@@ -53,10 +54,10 @@ class TableUpdate extends Cmd {
53
54
  await this.prologue(options)
54
55
  if (this.dbManager === undefined) throw 'dbManager undefined'
55
56
 
56
- if ( ! options.name ) throw '<table_name> is missing'
57
+ if (!options.name) throw '<table_name> is missing'
57
58
 
58
59
  const dbTable = await this.dbManager.dbTableExist(options.name)
59
- if ( ! dbTable ) throw 'Table ' + options.name + ' not found'
60
+ if (!dbTable) throw 'Table ' + options.name + ' not found'
60
61
 
61
62
  await this.updateAuthAccess('cyk_table', 'table', dbTable.id || '', options)
62
63
  }
@@ -319,63 +320,46 @@ class TableList extends Cmd {
319
320
  }
320
321
  }
321
322
 
322
- //--------------------------------------------------------------------------------------------------------------------
323
- // class TableData
324
- //--------------------------------------------------------------------------------------------------------------------
325
-
326
- class TableData extends Cmd {
327
-
323
+ class TableDropCommand extends Cmd {
328
324
  constructor(name: string, description: string) {
329
325
  super(name)
330
326
  this.description(description)
331
327
  .argument('<table>', 'table name')
332
- .option('--where <clause_where_sql>', 'criteria in SQL syntax')
333
- .option('-s --sort <columns>', 'sort by columns positions (begins by 0) separated by comma')
334
- .option('-w --width <colwidth>', 'columns widths separated by comma')
335
328
  .action(async (table, options) => {
336
- this.commandData(table, options)
329
+ await this.commandDrop(table, options)
337
330
  })
338
331
  }
339
332
 
340
- async commandData(table: string, options: any) {
333
+ async commandDrop(table: string, options: any) {
341
334
  try {
342
335
  await this.prologue(options)
343
- if (this.dbManager === undefined) throw 'dbManager undefined'
344
-
336
+ if (!this.dbManager) throw 'dbManager undefined'
337
+
345
338
  const dbTable = await this.dbManager.dbTableExist(table)
346
- if (dbTable === undefined) throw 'table ' + table + ' not found'
347
-
348
- let fields = ''
349
-
350
- for (let ind = 0; ind < dbTable.columns.length; ind++) {
351
- const dbColumn = dbTable.columns[ind]
352
- let ok = false
353
- let fieldName = dbColumn.name
354
- if (dbColumn.dbType === 'text' || dbColumn.dbType === 'bytea') {
355
- ok = false
356
- fieldName = 'length(' + dbColumn.name + ')'
357
- }
358
- else {
359
- ok = true
360
- }
361
- if (ok === true) {
362
- if (fields !== '') fields += ','
363
- fields += fieldName
364
- }
339
+ if (!dbTable) {
340
+ console.log('table ' + table + ' does not exist')
341
+ return
365
342
  }
366
-
367
- logger.debug('commandQuery', table)
368
- const dbClient = new DBClient(this.dbManager)
369
- let title = 'Table ' + table
370
- if (options.where !== undefined) {
371
- title += ' where ' + options.where
343
+ const reply = await inquirer.prompt({ type: 'confirm', name: 'confirm', message: 'Drop table ' + table + '?' })
344
+ if (reply.confirm && this.dbManager) {
345
+ await dropTable(table, this.dbManager)
372
346
  }
373
- dbClient.selectFromTable(title, table,
374
- { fields: fields, width: options.width, sort: options.sort || '1', where: options.where })
375
347
  }
376
- catch (err) {
348
+ catch(err) {
377
349
  logger.error(err)
378
350
  }
379
351
  }
380
352
  }
381
353
 
354
+ /**
355
+ * function dropTable
356
+ * @param table
357
+ * @param dbManager
358
+ */
359
+ export async function dropTable(table: string, dbManager: DBManager) {
360
+ const dbTable = await dbManager.dbTableExist(table)
361
+ if (dbTable)
362
+ await dbManager.dbTableDelete(dbTable)
363
+ }
364
+
365
+
@@ -0,0 +1,62 @@
1
+ import { Cmd } from "./Cmd";
2
+ import { DBClient } from "./DBClient";
3
+ import { logger } from "./TableCommand";
4
+
5
+ //--------------------------------------------------------------------------------------------------------------------
6
+ // class TableData
7
+ //--------------------------------------------------------------------------------------------------------------------
8
+ export class TableDataCommand extends Cmd {
9
+
10
+ constructor(name: string, description: string) {
11
+ super(name);
12
+ this.description(description)
13
+ .argument('<table>', 'table name')
14
+ .option('--where <clause_where_sql>', 'criteria in SQL syntax')
15
+ .option('-s --sort <columns>', 'sort by columns positions (begins by 0) separated by comma')
16
+ .option('-w --width <colwidth>', 'columns widths separated by comma')
17
+ .action(async (table, options) => {
18
+ this.commandData(table, options);
19
+ });
20
+ }
21
+
22
+ async commandData(table: string, options: any) {
23
+ try {
24
+ await this.prologue(options);
25
+ if (this.dbManager === undefined) throw 'dbManager undefined';
26
+
27
+ const dbTable = await this.dbManager.dbTableExist(table);
28
+ if (dbTable === undefined) throw 'table ' + table + ' not found';
29
+
30
+ let fields = '';
31
+
32
+ for (let ind = 0; ind < dbTable.columns.length; ind++) {
33
+ const dbColumn = dbTable.columns[ind];
34
+ let ok = false;
35
+ let fieldName = dbColumn.name;
36
+ if (dbColumn.dbType === 'text' || dbColumn.dbType === 'bytea') {
37
+ ok = false;
38
+ fieldName = 'length(' + dbColumn.name + ')';
39
+ }
40
+ else {
41
+ ok = true;
42
+ }
43
+ if (ok === true) {
44
+ if (fields !== '') fields += ',';
45
+ fields += fieldName;
46
+ }
47
+ }
48
+
49
+ logger.debug('commandQuery', table);
50
+ const dbClient = new DBClient(this.dbManager);
51
+ let title = 'Table ' + table;
52
+ if (options.where !== undefined) {
53
+ title += ' where ' + options.where;
54
+ }
55
+ dbClient.selectFromTable(title, table,
56
+ { fields: fields, width: options.width, sort: options.sort || '1', where: options.where });
57
+ }
58
+ catch (err) {
59
+ logger.error(err);
60
+ }
61
+ }
62
+ }
package/src/index.ts CHANGED
@@ -1,23 +1,129 @@
1
1
  #!/usr/bin/env node
2
- import loglevel from 'loglevel'
3
2
  import { Command } from 'commander'
3
+ import * as fs from 'fs'
4
+ import loglevel from 'loglevel'
5
+ import path from 'path'
6
+ import { AssetCommand } from './AssetCommand'
7
+ import { Cmd, spawnCommand } from './Cmd'
8
+ import { InstallCommand } from './InstallCommand'
4
9
  import { ModuleCommand } from "./ModuleCommand"
5
- import { UserCommand } from './UserCommand'
10
+ import { OpenCommand } from './OpenCommand'
6
11
  import { QueryCommand } from './QueryCommand'
12
+ import { RunCommand } from './RunCommand'
7
13
  import { TableCommand } from './TableCommand'
14
+ import { TableDataCommand } from './TableDataCommand'
8
15
  import { TestCommand } from './TestCommand'
9
- import { RunCommand } from './RunCommand'
10
- import { InstallCommand } from './InstallCommand'
11
- import { AssetCommand } from './AssetCommand'
12
- import { OpenCommand } from './OpenCommand'
13
16
  import { UninstallCommand } from './UninstallCommand'
17
+ import { UserCommand } from './UserCommand'
18
+
14
19
  const logger = loglevel.getLogger("index.ts")
15
20
  logger.setLevel("debug")
16
21
 
22
+ type Orchestrator = 'kubernetes' | 'docker' | undefined
23
+
24
+ /**
25
+ * function getOrchestrator
26
+ */
27
+ function getOrchestrator(): Orchestrator {
28
+ let result: Orchestrator
29
+ const cwd = process.cwd()
30
+ const dockerComposePath = path.join(cwd, 'docker-compose.yml')
31
+ const k8sPath = path.join(cwd, 'k8s')
32
+ if (fs.existsSync(dockerComposePath) && fs.statSync(dockerComposePath).isFile()) {
33
+ result = 'docker'
34
+ }
35
+ else if (fs.existsSync(k8sPath) && fs.statSync(k8sPath).isDirectory()) {
36
+ result = 'kubernetes'
37
+ }
38
+ return result
39
+ }
40
+
41
+ /**
42
+ * function startServer
43
+ */
44
+ async function startServer() {
45
+ try {
46
+ const orchestrator = getOrchestrator()
47
+ if (orchestrator === 'docker') {
48
+ await spawnCommand('docker', ['compose', 'up', '-d'])
49
+ }
50
+ if (orchestrator === 'kubernetes') {
51
+ const portForwardPath = path.join(process.cwd(), 'k8s', 'port-forward')
52
+ if (!fs.existsSync(portForwardPath) || !fs.statSync(portForwardPath).isFile())
53
+ throw portForwardPath + ' file not found'
54
+ const config = JSON.parse(fs.readFileSync(portForwardPath).toString())
55
+ if (! config.postgres || ! config.nodejs)
56
+ throw portForwardPath + ' : file format incorrect'
57
+ spawnCommand('kubectl', ['port-forward', 'svc/postgres', config.postgres + ':5432'])
58
+ logger.info('Postgresql proxy at localhost:' + config.postgres)
59
+ spawnCommand('kubectl', ['port-forward', 'svc/nodejs', config.nodejs + ':3000'])
60
+ logger.info('Nodejs proxy at localhost:' + config.nodejs)
61
+ }
62
+ if (!orchestrator) {
63
+ logger.error('No cyk server is defined in current folder ' + process.cwd())
64
+ }
65
+ }
66
+ catch (err) {
67
+ console.error(err)
68
+ }
69
+ }
70
+
71
+ /**
72
+ * function stopServer
73
+ */
74
+ async function stopServer() {
75
+ try {
76
+ const orchestrator = getOrchestrator()
77
+ if (orchestrator === 'docker') {
78
+ await spawnCommand('docker', ['compose', 'down'])
79
+ }
80
+ if (orchestrator === 'kubernetes') {
81
+ await spawnCommand('kubectl', ['delete', '-f', path.join(process.cwd(), 'k8s')])
82
+ logger.info('Kubernetes deployments and services deleted')
83
+ }
84
+ if (!orchestrator) {
85
+ logger.error('No cyk server is defined in current folder ' + process.cwd())
86
+ }
87
+ }
88
+ catch (err) {
89
+ console.error(err)
90
+ }
91
+ }
92
+
93
+ /**
94
+ * class StartCommand
95
+ */
96
+ class StartCommand extends Command {
97
+ constructor(name: string) {
98
+ super(name)
99
+ this.description('start cyk server defined in current project folder')
100
+ .action(async (options: any) => {
101
+ await startServer()
102
+ })
103
+ }
104
+ }
105
+
106
+ /**
107
+ * class StopCommand
108
+ */
109
+ class StopCommand extends Command {
110
+ constructor(name: string) {
111
+ super(name)
112
+ this.description('stop cyk server defined in current project folder')
113
+ .action(async (options: any) => {
114
+ await stopServer()
115
+ })
116
+ }
117
+ }
118
+
119
+ /**
120
+ * Main program
121
+ */
17
122
  const program = new Command()
18
123
  program.name('cyk').description('cyklang CLI')
19
- .version('0.5.6')
20
-
124
+ .version('0.6.0')
125
+ program.addCommand(new StartCommand('start'))
126
+ program.addCommand(new StopCommand('stop'))
21
127
  program.addCommand(new InstallCommand('install'))
22
128
  program.addCommand(new InstallCommand('i'))
23
129
  program.addCommand(new UninstallCommand('uninstall'))
@@ -31,6 +137,8 @@ program.addCommand(new QueryCommand('query'))
31
137
  program.addCommand(new QueryCommand('q'))
32
138
  program.addCommand(new TableCommand('table'))
33
139
  program.addCommand(new TableCommand('t'))
140
+ program.addCommand(new TableDataCommand('data', 'show table data'))
141
+ program.addCommand(new TableDataCommand('d', 'show table data'))
34
142
  program.addCommand(new TestCommand())
35
143
  program.addCommand(new RunCommand('run'))
36
144
  program.addCommand(new RunCommand('r'))