-
Notifications
You must be signed in to change notification settings - Fork 3
/
all_dbs.js
75 lines (69 loc) · 1.99 KB
/
all_dbs.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
/*
* ### Модуль перебора баз данных сервера
*
* - запуск процесса для каждой базы данных
*
* @module all_dbs
*
* Created 10.08.2019
*/
'use strict';
const fetch = require('node-fetch');
const {exec} = require('child_process');
const yargs = require('yargs')
.usage('Usage: $0 [options] <command>')
.demand(1)
.strict()
.version('v', 'Show version', '0.0.1').alias('v', 'version')
.help('h').alias('h', 'help')
.example('node all_dbs exec echo "Database {0}"', 'Get all databases with execute `echo "Database {0}"`')
.command(
'exec [name]',
'Get all databases with execute `name`',
yargs => yargs.positional('name', {type: 'string', describe: 'Empty execute name'}),
args => {
const {name} = args;
if(name){
// инициализируем параметры сеанса и метаданные
const {COUCHPATH} = process.env;
const prefix = 'wb_';
fetch(`${COUCHPATH.replace(prefix, '')}_all_dbs`)
.then(res => res.json())
.then(async res => {
for(const db of res){
console.log(`${db}`);
await _exec(name.replace('{0}', db))
.then(res => {
console.log(res);
})
.catch(err => {
console.error(err);
});
}
})
.catch(err => {
console.log(`error fetch dbs: ${err && err.message}`);
});
}
else {
yargs.showHelp();
process.exit(1);
}
}
)
.epilog('\nMore information about the library: https://github.com/oknosoft/windowbuilder');
const {argv} = yargs;
if(!argv._.length){
yargs.showHelp();
process.exit(1);
}
function _exec(name) {
return new Promise((resolve, reject) => {
exec(name, {env: process.env}, (err, stdout, stderr) => {
if(err){
return reject(err);
}
resolve(stdout);
});
});
}