2009-01-27 21:16:09 +01:00
|
|
|
// Tool.cpp
|
|
|
|
|
|
|
|
#include "Tool.h"
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
#include <boost/filesystem/operations.hpp>
|
|
|
|
|
2009-05-20 18:15:26 +02:00
|
|
|
#include "util/file_allocator.h"
|
|
|
|
|
2009-01-27 21:16:09 +01:00
|
|
|
using namespace std;
|
|
|
|
using namespace mongo;
|
|
|
|
|
|
|
|
namespace po = boost::program_options;
|
|
|
|
|
2009-06-25 22:11:38 +02:00
|
|
|
mongo::Tool::Tool( string name , string defaultDB , string defaultCollection ) :
|
2009-05-20 17:39:12 +02:00
|
|
|
_name( name ) , _db( defaultDB ) , _coll( defaultCollection ), _useDirect() {
|
2009-06-25 22:11:38 +02:00
|
|
|
|
2009-01-27 21:16:09 +01:00
|
|
|
_options = new po::options_description( name + " options" );
|
|
|
|
_options->add_options()
|
|
|
|
("help","produce help message")
|
|
|
|
("host,h",po::value<string>(), "mongo host to connect to" )
|
|
|
|
("db,d",po::value<string>(), "database to use" )
|
|
|
|
("collection,c",po::value<string>(), "collection to use (some commands)" )
|
2009-05-20 17:39:12 +02:00
|
|
|
("dbpath",po::value<string>(), "directly access mongod data files in this path, instead of connecting to a mongod instance" )
|
2009-01-27 21:16:09 +01:00
|
|
|
;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
mongo::Tool::~Tool(){
|
|
|
|
delete( _options );
|
|
|
|
}
|
|
|
|
|
2009-02-03 04:16:46 +01:00
|
|
|
void mongo::Tool::printExtraHelp( ostream & out ){
|
|
|
|
}
|
|
|
|
|
2009-06-25 22:15:15 +02:00
|
|
|
void mongo::Tool::printHelp(ostream &out) {
|
|
|
|
_options->print(out);
|
|
|
|
printExtraHelp(out);
|
|
|
|
}
|
|
|
|
|
2009-01-27 21:16:09 +01:00
|
|
|
int mongo::Tool::main( int argc , char ** argv ){
|
|
|
|
boost::filesystem::path::default_name_check( boost::filesystem::no_check );
|
2009-06-25 22:11:38 +02:00
|
|
|
|
2009-01-28 03:37:56 +01:00
|
|
|
po::store( po::command_line_parser( argc , argv ).
|
|
|
|
options( *_options ).
|
|
|
|
positional( _positonalOptions ).run() , _params );
|
|
|
|
|
2009-01-27 21:16:09 +01:00
|
|
|
po::notify( _params );
|
|
|
|
|
|
|
|
if ( _params.count( "help" ) ){
|
2009-06-25 22:15:15 +02:00
|
|
|
printHelp(cerr);
|
2009-01-27 21:16:09 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2009-05-20 17:39:12 +02:00
|
|
|
|
|
|
|
if ( !hasParam( "dbpath" ) ) {
|
|
|
|
const char * host = "127.0.0.1";
|
|
|
|
if ( _params.count( "host" ) )
|
|
|
|
host = _params["host"].as<string>().c_str();
|
2009-06-25 22:11:38 +02:00
|
|
|
|
2009-05-20 17:39:12 +02:00
|
|
|
string errmsg;
|
|
|
|
if ( ! _conn.connect( host , errmsg ) ){
|
|
|
|
cerr << "couldn't connect to [" << host << "] " << errmsg << endl;
|
|
|
|
return -1;
|
|
|
|
}
|
2009-06-25 22:11:38 +02:00
|
|
|
|
2009-05-20 17:39:12 +02:00
|
|
|
cerr << "connected to: " << host << endl;
|
|
|
|
} else {
|
|
|
|
_useDirect = true;
|
|
|
|
static string myDbpath = getParam( "dbpath" );
|
|
|
|
mongo::dbpath = myDbpath.c_str();
|
|
|
|
mongo::acquirePathLock();
|
2009-05-20 18:15:26 +02:00
|
|
|
theFileAllocator().start();
|
2009-01-27 21:16:09 +01:00
|
|
|
}
|
2009-06-25 22:11:38 +02:00
|
|
|
|
2009-01-27 21:16:09 +01:00
|
|
|
if ( _params.count( "db" ) )
|
|
|
|
_db = _params["db"].as<string>();
|
2009-06-25 22:11:38 +02:00
|
|
|
|
2009-01-27 21:16:09 +01:00
|
|
|
if ( _params.count( "collection" ) )
|
|
|
|
_coll = _params["collection"].as<string>();
|
2009-07-10 16:59:30 +02:00
|
|
|
|
|
|
|
try {
|
|
|
|
return run();
|
|
|
|
}
|
|
|
|
catch ( DBException& e ){
|
|
|
|
cerr << "assertion: " << e.toString() << endl;
|
|
|
|
return -1;
|
|
|
|
}
|
2009-01-27 21:16:09 +01:00
|
|
|
}
|