-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmmongo.js
executable file
·169 lines (140 loc) · 4.31 KB
/
mmongo.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
#!/usr/bin/env node
var url = require('url');
var child_process = require('child_process');
var util = require('util');
subcommands = {
'--help': { help: true },
'run': { exec: 'mongo', db: ["DB"] },
'dump': { exec: 'mongodump', db: ["--db", "DB"] },
'restore': { exec: 'mongorestore', db: ["--db", "DB"] },
'import': { exec: 'mongoimport', db: ["--db", "DB"] },
'export': { exec: 'mongoexport', db: ["--db", "DB"] },
'files': { exec: 'mongofiles', db: ["--db", "DB"] },
'oplog': { exec: 'mongooplog', db: ["--db", "DB"] },
'stat': { exec: 'mongostat', db: [] },
'top': { exec: 'mongotop', db: [] }
};
function main() {
args = parseArgs(process.argv.slice(2));
if (args.command.help)
printUsageAndExit();
var mUrl = getMeteorMongoUrl(args.site);
var urlObj = url.parse(mUrl);
var newArgs = buildArgs (args.command, args.commandArgs, urlObj);
if (args.dryRun) {
var fullCommand = shellQuote([args.command.exec].concat(newArgs));
console.log(fullCommand);
// console.log([args.command.exec].concat(newArgs));
} else {
child_process.spawn(args.command.exec, newArgs, { stdio: 'inherit' });
}
}
function parseArgs(args) {
parsedArgs = {};
// 1. Flags to mmongo
if (args[0] === '--dry') {
parsedArgs.dryRun = true;
args.shift();
}
// 2. A site is specified unless we go directly to the command
if (!(args[0] in subcommands) && args.length > 0) {
parsedArgs.site = args[0];
args.shift();
} else {
parsedArgs.site = null;
}
// 3. A subcommand (or "run" if none given)
if (args[0] in subcommands) {
parsedArgs.command = subcommands[args[0]];
args.shift();
} else {
parsedArgs.command = subcommands['run'];
}
// 4. The rest is args to the subcommand
parsedArgs.commandArgs = args;
return parsedArgs;
}
function buildArgs(command, commandArgs, urlObj) {
var args = ['--host', urlObj.host];
if (typeof urlObj.auth === 'string') {
var auth = urlObj.auth.split(':');
if (auth[0])
args.push('--username', auth[0]);
if (auth[1])
args.push('--password', auth[1]);
}
// The various mongo commands have different conventions
// for passing the database; some don't want a database at all.
var db = urlObj.pathname.replace(/^\//, '');
var dbArgs = command.db.map (function (s) {
return (s === "DB") ? db : s;
});
args = args.concat(dbArgs);
return args.concat(commandArgs);
}
function getMeteorMongoUrl(site) {
var args = ['mongo', '--url'];
if (site)
args.push(site);
var ret;
if (process.platform === 'win32') {
ret = child_process.spawnSync('meteor.bat', args);
} else {
ret = child_process.spawnSync('meteor', args);
}
if (ret.status != 0) {
var exc = new MeteorMongoException(ret.status, ret.stderr);
exc.status = ret.status;
throw(exc);
}
return ret.stdout.toString().trim();
}
function MeteorMongoException(status, stderr) {
this.status = status;
this.stderr = stderr;
this.toString = function () {
return (util.format("meteor mongo returned %d with message:\n%s",
status, stderr.toString()));
};
}
// Adapted from https://github.com/substack/node-shell-quote
// By James Halliday, MIT licensed
function shellQuote(xs) {
return xs.map(function (s) {
if (s && typeof s === 'object') {
return s.op.replace(/(.)/g, '\\$1');
}
else if (/["\s]/.test(s) && !/'/.test(s)) {
return "'" + s.replace(/(['\\])/g, '\\$1') + "'";
}
else if (/["'\s]/.test(s)) {
return '"' + s.replace(/(["\\$`!])/g, '\\$1') + '"';
}
else {
return String(s).replace(/([\\$`()!#&*|])/g, '\\$1');
}
}).join(' ');
}
function repeat(s, n) {
return new Array(n + 1).join(s);
}
function usageCommand(key) {
command = subcommands[key].exec;
if (!command)
return '';
return " " + key + ":" + repeat(" ", 10 - key.length) +
command + "\n";
}
function printUsageAndExit() {
console.log(
'Usage: mmongo [--dry | --help] [SITE] [COMMAND] [ARGS]\n\n'+
" - Use the --dry flag to see what would've been executed.\n" +
' - SITE is a meteor site, like "example.meteor.com".\n' +
' To access the local site, put nothing here.\n' +
' - COMMAND can be any of:\n' +
Object.keys(subcommands).map(usageCommand).join("") +
' - ARGS is any arguments to that command.\n'
);
process.exit(0);
}
main();