/
mongo-import
executable file
·116 lines (94 loc) · 3.29 KB
/
mongo-import
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
#!/usr/bin/env node
'use strict'; // eslint-disable-line strict
const program = require('commander');
const fs = require('fs');
const path = require('path');
const yaml = require('js-yaml');
const parse = require('url-parse');
const spawn = require('child_process').spawn;
const colour = require('cli-color');
const _ = require('lodash');
const red = colour.red;
const green = colour.green;
const yellow = colour.yellow;
const manifestFiles = {
'dev': 'manifest.yml',
'qa': 'manifest-test.yml',
'uat': 'manifest-uat.yml'
};
const validEnvironments = _.union(_.keys(manifestFiles), ['local']);
const mongoUrl = env => {
if (env === 'local') {
return 'mongodb://localhost:27017/dev';
} else {
const manifestFile = manifestFiles[env];
const manifest = yaml.safeLoad(fs.readFileSync(manifestFile, 'utf8'));
return _.get(manifest, 'applications[0].env.MONGO_DB_URL_LOCAL');
}
};
var importStarted = false;
const exit = message => {
console.log(message);
process.exit(1);
};
const validateEnvironment = env => {
if (! _.includes(validEnvironments, env)) {
exit(` error:invalid environment '${env}' - should be one of '${validEnvironments.join("', '")}'`);
}
};
const importOne = (mongoUrl, csv, collection) => {
const url = parse(mongoUrl, true);
const database = url.pathname.substr(1);
console.log(yellow(`Importing '${csv}' to the '${collection}' collection in the '${database}' database on '${url.hostname}:${url.port}'`));
let args = ['--type=csv', '--headerline', '--drop', `--file=${csv}`, `--collection=${collection}`];
function appendArg(option, value) {
if (value !== '' && value !== undefined) {
args.push(`--${option}=${value}`);
}
}
appendArg('host', url.hostname);
appendArg('port', url.port);
appendArg('username', url.username);
appendArg('password', url.password);
appendArg('db', database);
if (url.query.ssl === 'true') {
args.push('--ssl');
}
const importProcess = spawn('mongoimport', args);
function log(colour, string) {
string.split(/(\r?\n)/g).forEach((line) => {
const output = line.replace(/\s+$/, '').replace(/^[^\s]*/, '').replace(/^\s+/, ' ');
if (output !== '') {
console.log(colour(output));
}
});
}
importProcess.stderr.on('data', data => {
const string = data.toString();
const colour = string.match(/(failed|imported 0|error)/i) ? red : yellow;
log(colour, string);
});
importProcess.on('close', code => {
const message = code === 0 ? green(` Import of ${collection} succeeded.\n`) : red(` Import of the '${collection}' collection into the '${database}' database on '${url.hostname}:${url.port} failed (code=${code})\n`);
console.log(message);
});
};
const doImport = mongoUrl => {
_.each(fs.readdirSync('./tables'), file => {
if (_.endsWith(file, '.csv')) {
const collection = path.basename(file, '.csv');
importOne(mongoUrl, './tables/' + file, collection);
}
});
};
program
.arguments('mongo-import <env> ')
.action( env => {
validateEnvironment(env);
importStarted = true;
doImport(mongoUrl(env));
});
program.parse(process.argv);
if (!importStarted) {
console.log(` error: missing required argument 'env' - should be one of '${validEnvironments.join("', '")}'`);
}