0
0
mirror of https://github.com/mongodb/mongo.git synced 2024-11-30 17:10:48 +01:00
mongodb/tools/tool.cpp

177 lines
5.0 KiB
C++
Raw Normal View History

2009-01-27 21:16:09 +01:00
// Tool.cpp
#include "Tool.h"
#include <iostream>
#include <boost/filesystem/operations.hpp>
#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 ) :
_name( name ) , _db( defaultDB ) , _coll( defaultCollection ) , _conn(0), _paired(false) {
2009-06-25 22:11:38 +02:00
_options = new po::options_description( "options" );
2009-01-27 21:16:09 +01:00
_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-08-12 22:31:22 +02:00
("username,u",po::value<string>(), "username" )
("password,p",po::value<string>(), "password" )
("dbpath",po::value<string>(), "directly access mongod data files in this path, instead of connecting to a mongod instance" )
2009-07-13 20:09:43 +02:00
("verbose,v", "be more verbose (include multiple times for more verbosity e.g. -vvvvv)")
2009-01-27 21:16:09 +01:00
;
_hidden_options = new po::options_description( name + " hidden options" );
/* support for -vv -vvvv etc. */
for (string s = "vv"; s.length() <= 10; s.append("v")) {
_hidden_options->add_options()(s.c_str(), "verbose");
}
2009-01-27 21:16:09 +01:00
}
mongo::Tool::~Tool(){
delete( _options );
delete( _hidden_options );
if ( _conn )
delete _conn;
2009-01-27 21:16:09 +01:00
}
void mongo::Tool::printExtraHelp( ostream & out ){
}
void mongo::Tool::printHelp(ostream &out) {
printExtraHelp(out);
_options->print(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
_name = argv[0];
/* using the same style as db.cpp */
int command_line_style = (((po::command_line_style::unix_style ^
po::command_line_style::allow_guessing) |
po::command_line_style::allow_long_disguise) ^
po::command_line_style::allow_sticky);
try {
po::options_description all_options("all options");
all_options.add(*_options).add(*_hidden_options);
po::store( po::command_line_parser( argc , argv ).
options(all_options).
positional( _positonalOptions ).
style(command_line_style).run() , _params );
po::notify( _params );
} catch (po::error &e) {
cout << "ERROR: " << e.what() << endl << endl;
printHelp(cout);
return EXIT_BADOPTIONS;
}
2009-01-27 21:16:09 +01:00
if ( _params.count( "help" ) ){
printHelp(cerr);
2009-01-27 21:16:09 +01:00
return 0;
}
if ( _params.count( "verbose" ) ) {
2009-07-13 20:09:43 +02:00
logLevel = 1;
}
for (string s = "vv"; s.length() <= 10; s.append("v")) {
if (_params.count(s)) {
logLevel = s.length();
}
}
2009-07-13 20:09:43 +02:00
if ( ! hasParam( "dbpath" ) ) {
_host = "127.0.0.1";
if ( _params.count( "host" ) )
_host = _params["host"].as<string>();
2009-09-01 21:43:23 +02:00
if ( _host.find( "," ) == string::npos ){
DBClientConnection * c = new DBClientConnection();
_conn = c;
2009-09-01 21:43:23 +02:00
string errmsg;
if ( ! c->connect( _host , errmsg ) ){
cerr << "couldn't connect to [" << _host << "] " << errmsg << endl;
return -1;
}
}
else {
DBClientPaired * c = new DBClientPaired();
_paired = true;
_conn = c;
2009-09-01 21:43:23 +02:00
if ( ! c->connect( _host ) ){
cerr << "couldn't connect to paired server: " << _host << endl;
return -1;
}
}
2009-06-25 22:11:38 +02:00
cerr << "connected to: " << _host << endl;
2009-09-01 21:43:23 +02:00
}
else {
_conn = new DBDirectClient();
_host = "DIRECT";
static string myDbpath = getParam( "dbpath" );
mongo::dbpath = myDbpath.c_str();
mongo::acquirePathLock();
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-09-01 21:43:23 +02:00
2009-08-12 22:31:22 +02:00
if ( _params.count( "username" ) )
_username = _params["username"].as<string>();
if ( _params.count( "password" ) )
_password = _params["password"].as<string>();
2009-09-01 21:43:23 +02:00
try {
return run();
}
catch ( DBException& e ){
cerr << "assertion: " << e.toString() << endl;
return -1;
}
2009-01-27 21:16:09 +01:00
}
2009-08-12 22:31:22 +02:00
mongo::DBClientBase& mongo::Tool::conn( bool slaveIfPaired ){
if ( _paired && slaveIfPaired )
return ((DBClientPaired*)_conn)->slaveConn();
return *_conn;
}
2009-08-12 22:31:22 +02:00
void mongo::Tool::auth( string dbname ){
if ( ! dbname.size() )
dbname = _db;
if ( ! ( _username.size() || _password.size() ) )
return;
string errmsg;
if ( _conn->auth( dbname , _username , _password , errmsg ) )
return;
2009-09-01 21:43:23 +02:00
2009-08-12 22:31:22 +02:00
// try against the admin db
string err2;
if ( _conn->auth( "admin" , _username , _password , errmsg ) )
return;
2009-09-01 21:43:23 +02:00
2009-08-12 22:31:22 +02:00
throw mongo::UserException( (string)"auth failed: " + errmsg );
}