Files
mongo/tools/tool.h

141 lines
3.6 KiB
C
Raw Normal View History

/*
* Copyright (C) 2010 10gen Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
2009-01-27 15:16:09 -05:00
// Tool.h
#pragma once
#include <string>
#include <boost/program_options.hpp>
2009-02-10 10:48:41 -05:00
#if defined(_WIN32)
#include <io.h>
#endif
2009-01-27 15:16:09 -05:00
#include "client/dbclient.h"
#include "db/instance.h"
2009-01-27 15:16:09 -05:00
using std::string;
namespace mongo {
2009-06-25 16:11:38 -04:00
2009-01-27 15:16:09 -05:00
class Tool {
public:
Tool( string name , bool localDBAllowed=true, string defaultDB="test" ,
string defaultCollection="", bool usesstdout=true );
2009-01-27 15:16:09 -05:00
virtual ~Tool();
int main( int argc , char ** argv );
2009-06-25 16:11:38 -04:00
2009-01-27 15:16:09 -05:00
boost::program_options::options_description_easy_init add_options(){
return _options->add_options();
}
boost::program_options::options_description_easy_init add_hidden_options(){
return _hidden_options->add_options();
}
2009-01-27 21:37:56 -05:00
void addPositionArg( const char * name , int pos ){
_positonalOptions.add( name , pos );
}
2009-06-25 16:11:38 -04:00
2009-01-27 15:16:09 -05:00
string getParam( string name , string def="" ){
if ( _params.count( name ) )
return _params[name.c_str()].as<string>();
return def;
}
2010-02-28 12:35:18 -05:00
int getParam( string name , int def ){
if ( _params.count( name ) )
return _params[name.c_str()].as<int>();
return def;
}
2009-01-29 13:01:45 -05:00
bool hasParam( string name ){
return _params.count( name );
}
string getNS(){
if ( _coll.size() == 0 ){
cerr << "no collection specified!" << endl;
throw -1;
}
return _db + "." + _coll;
}
2010-04-16 01:15:09 -04:00
virtual void preSetup(){}
2009-01-27 15:16:09 -05:00
virtual int run() = 0;
2009-06-25 16:11:38 -04:00
virtual void printHelp(ostream &out);
2010-07-12 13:17:34 -04:00
virtual void printExtraHelp( ostream & out ){}
virtual void printExtraHelpAfter( ostream & out ){}
2009-09-01 15:53:31 -04:00
2009-01-27 15:16:09 -05:00
protected:
2009-08-12 16:31:22 -04:00
mongo::DBClientBase &conn( bool slaveIfPaired = false );
2009-08-12 16:31:22 -04:00
void auth( string db = "" );
2009-01-27 15:16:09 -05:00
string _name;
2009-09-01 15:53:31 -04:00
2009-01-27 15:16:09 -05:00
string _db;
string _coll;
2009-09-01 15:53:31 -04:00
2009-08-12 16:31:22 -04:00
string _username;
string _password;
bool _usesstdout;
2010-04-16 01:15:09 -04:00
bool _noconnection;
2010-06-09 13:20:35 -04:00
bool _autoreconnect;
2009-01-27 15:16:09 -05:00
2009-10-12 15:05:42 -04:00
void addFieldOptions();
void needFields();
2009-10-12 15:05:42 -04:00
vector<string> _fields;
BSONObj _fieldsObj;
2009-09-01 15:53:31 -04:00
string _host;
2010-04-16 01:15:09 -04:00
protected:
mongo::DBClientBase * _conn;
bool _paired;
2009-09-01 15:53:31 -04:00
2009-01-27 15:16:09 -05:00
boost::program_options::options_description * _options;
boost::program_options::options_description * _hidden_options;
2009-01-27 21:37:56 -05:00
boost::program_options::positional_options_description _positonalOptions;
2009-01-27 15:16:09 -05:00
boost::program_options::variables_map _params;
2009-06-25 16:11:38 -04:00
2009-01-27 15:16:09 -05:00
};
2009-06-25 16:11:38 -04:00
2010-06-09 11:31:30 -04:00
class BSONTool : public Tool {
bool _objcheck;
auto_ptr<Matcher> _matcher;
public:
BSONTool( const char * name , bool objcheck = false );
virtual int doRun() = 0;
virtual void gotObject( const BSONObj& obj ) = 0;
virtual int run();
long long processFile( const path& file );
};
2009-01-27 15:16:09 -05:00
}