zeus
4 years ago
1 changed files with 75 additions and 0 deletions
@ -0,0 +1,75 @@ |
|||
function getArgs () { |
|||
const args = {}; |
|||
process.argv |
|||
.slice(2, process.argv.length) |
|||
.forEach( arg => { |
|||
// long arg
|
|||
if (arg.slice(0,2) === '--') { |
|||
const longArg = arg.split('='); |
|||
const longArgFlag = longArg[0].slice(2,longArg[0].length); |
|||
const longArgValue = longArg.length > 1 ? longArg[1] : true; |
|||
args[longArgFlag] = longArgValue; |
|||
} |
|||
// flags
|
|||
else if (arg[0] === '-') { |
|||
const flags = arg.slice(1,arg.length).split(''); |
|||
flags.forEach(flag => { |
|||
args[flag] = true; |
|||
}); |
|||
} |
|||
}); |
|||
return args; |
|||
} |
|||
const args = getArgs(); |
|||
|
|||
//console.log(args)
|
|||
|
|||
//node ./venus-stats.js --insert=yes --port=30001 --ip='127.0.0.1' --user=swarmlab --password=swarmlab --db=venus --coll=logs --json='{ "name" : "Hello", "log" : "World" }'
|
|||
|
|||
if (args["insert"] == 'yes'){ |
|||
try{ |
|||
var dbuser = args["user"] |
|||
var dbpassword = args["password"] |
|||
var dbName = args["db"] |
|||
var collName = args["coll"] |
|||
var dbNamePort = args["port"] |
|||
var dbNameIP = args["ip"] |
|||
var dbjson = JSON.parse(args["json"]) |
|||
|
|||
var MongoClient = require('mongodb').MongoClient; |
|||
var mongourl = `mongodb://${dbuser}:${dbpassword}@${dbNameIP}:${dbNamePort}`; |
|||
const OPTS = { |
|||
useNewUrlParser: true, |
|||
useUnifiedTopology: true |
|||
}; |
|||
|
|||
MongoClient.connect(mongourl, OPTS, function(err, db) { |
|||
if(err){ |
|||
console.log(err); |
|||
} else { |
|||
var dbo = db.db(dbName); |
|||
dbo.listCollections({name: collName}) |
|||
.next(function(err, collinfo) { |
|||
if (collinfo) { |
|||
}else{ |
|||
dbo.createCollection(collName, { |
|||
capped: true, |
|||
size: 100000 |
|||
}); |
|||
} |
|||
}); |
|||
dbo.collection(collName).insertOne(dbjson, function(err, res) { |
|||
if(err){ |
|||
console.log(err); |
|||
} else { |
|||
console.log("ok"); |
|||
db.close(); |
|||
} |
|||
}); |
|||
} |
|||
}); |
|||
} catch (err) { |
|||
console.error(err); |
|||
} |
|||
} |
|||
|
Loading…
Reference in new issue