Created
May 16, 2013 08:32
-
-
Save RyanQuackenbush/5590265 to your computer and use it in GitHub Desktop.
Takes a host name, db name, collection name, username, password, and file regex to search for. Then runs mongoimport command with matching files of the regex in the current directory. Files should be JSON formatted prior to importing. Requires argparse: "npm install argparse"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
/** | |
* Takes a host name, db name, collection name, username, password, and file regex to search for. Then runs mongoimport command with matching files of the regex in the current directory. Files should be JSON formatted prior to importing. | |
* //mongoimport -h <host> -d <database> -c <collection> -u <user> -p <password> --file <input file> | |
*/ | |
'use strict'; | |
var fs = require('fs' ), | |
exec = require('child_process').exec; | |
var ArgumentParser = require('argparse').ArgumentParser, | |
args; | |
var parser = new ArgumentParser({ | |
version: '0.0.1', | |
addHelp:true, | |
description: 'Takes a host, db, collection, user, password, and file regex to search for. Runs mongoimport command ' | |
+ 'with matching files of the regex in the current directory.' | |
}); | |
parser.addArgument( | |
[ '--host' ], { //can't use -h here easily with help cmd.. | |
defaultValue: '<INSERT DEFAULT HOST>', | |
help: 'host name', | |
dest: 'host' | |
} | |
); | |
parser.addArgument( | |
[ '-d', '--database' ], { | |
defaultValue: '<INSERT DEFAULT DB>', | |
help: 'database name', | |
dest: 'd' | |
} | |
); | |
parser.addArgument( | |
[ '-c', '--collection' ],{ | |
defaultValue: '<INSERT DEFAULT COLLECTION>', | |
help: 'database collection name', | |
dest: 'c' | |
} | |
); | |
parser.addArgument( | |
[ '-u', '--user' ],{ | |
defaultValue: '<INSERT DEFAULT USER>', | |
help: 'database user name', | |
dest: 'u' | |
} | |
); | |
parser.addArgument( | |
[ '-p', '--pass' ],{ | |
help: 'database user name password', | |
dest: 'p' | |
} | |
); | |
parser.addArgument( | |
[ '-f', '--fileRE' ],{ | |
help: 'regexp to match against cwd files', | |
dest: 're' | |
} | |
); | |
parser.addArgument( | |
[ '--dry' ],{ | |
help: 'will not exec imports, outputs file which would be imported to stdout instead', | |
action: 'storeTrue', | |
defaultValue: false, | |
required: false, | |
dest: 'dry' | |
} | |
); | |
parser.addArgument( | |
[ '--limit' ],{ | |
help: 'limit to first <limit> number of files. --dry argument can also be used here to test', | |
defaultValue: 0, | |
type: Number, | |
required: false, | |
dest: 'limit' | |
} | |
); | |
args = parser.parseArgs(); | |
console.log("Args: ", args); | |
var cwd = process.cwd(); | |
function handleDirFiles( err, files ){ | |
if( err ){ | |
console.error( err ); | |
return; | |
} | |
var regExp = new RegExp( args['re'], 'g' ); | |
var useFiles = files.filter( function( file ){ | |
return regExp.test( file ); | |
}); | |
for ( var i = 0, n = args['limit'] || useFiles.length; i < n; i += 1 ) { | |
var file = useFiles[i]; | |
if( args['dry'] ){ | |
console.log("Would run exec on: ", file ); | |
}else{ | |
exec('mongoimport -h '+args['host']+' -d '+args['d']+' -c '+args['c']+' -u '+args['u']+' -p '+args['p']+' --file '+file, | |
function (error, stdout, stderr) { | |
console.log(stdout); | |
if( stderr ){ | |
console.log('stderr: ' + stderr); | |
} | |
if (error !== null) { | |
console.log('exec error: ' + error); | |
} | |
}); | |
} | |
} | |
} | |
//kickstart with getting files | |
fs.readdir( cwd, handleDirFiles ); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment