summaryrefslogtreecommitdiff
path: root/tools/Tool.cpp
blob: 82ed79686eaf0ee70b4fb1c749e6f629962787fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// Tool.cpp

#include "Tool.h"

#include <iostream>

#include <boost/filesystem/operations.hpp>

#include "util/file_allocator.h"

using namespace std;
using namespace mongo;

namespace po = boost::program_options;

mongo::Tool::Tool( string name , string defaultDB , string defaultCollection ) :
    _name( name ) , _db( defaultDB ) , _coll( defaultCollection ), _useDirect() {

    _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)" )
        ("dbpath",po::value<string>(), "directly access mongod data files in this path, instead of connecting to a mongod instance" )
        ;

}

mongo::Tool::~Tool(){
    delete( _options );
}

void mongo::Tool::printExtraHelp( ostream & out ){
}

void mongo::Tool::printHelp(ostream &out) {
    _options->print(out);
    printExtraHelp(out);
}

int mongo::Tool::main( int argc , char ** argv ){
    boost::filesystem::path::default_name_check( boost::filesystem::no_check );

    po::store( po::command_line_parser( argc , argv ).
               options( *_options ).
               positional( _positonalOptions ).run() , _params );

    po::notify( _params );

    if ( _params.count( "help" ) ){
        printHelp(cerr);
        return 0;
    }

    if ( !hasParam( "dbpath" ) ) {
        const char * host = "127.0.0.1";
        if ( _params.count( "host" ) )
            host = _params["host"].as<string>().c_str();

        string errmsg;
        if ( ! _conn.connect( host , errmsg ) ){
            cerr << "couldn't connect to [" << host << "] " << errmsg << endl;
            return -1;
        }

        cerr << "connected to: " << host << endl;
    } else {
        _useDirect = true;
        static string myDbpath = getParam( "dbpath" );
        mongo::dbpath = myDbpath.c_str();
        mongo::acquirePathLock();
        theFileAllocator().start();
    }

    if ( _params.count( "db" ) )
        _db = _params["db"].as<string>();

    if ( _params.count( "collection" ) )
        _coll = _params["collection"].as<string>();

    return run();
}