aboutsummaryrefslogtreecommitdiffstats
path: root/config/src/apps/vespa-configproxy-cmd/main.cpp
blob: e5c823d8d247553cfe4c76b832965c0bbc1996d0 (plain) (blame)
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
84
85
86
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "proxycmd.h"
#include "methods.h"
#include <vespa/vespalib/util/signalhandler.h>
#include <iostream>
#include <unistd.h>

class Application
{
    Flags _flags;
    bool parseOpts(int argc, char **argv);
public:
    void usage();
    int main(int argc, char **argv);

    Application() : _flags() {}
};

bool
Application::parseOpts(int argc, char **argv)
{
    int c = '?';
    while ((c = getopt(argc, argv, "m:s:p:h")) != -1) {
        switch (c) {
        case 'm':
            _flags.method = optarg;
            break;
        case 's':
            _flags.targethost = optarg;
            break;
        case 'p':
            _flags.portnumber = atoi(optarg);
            break;
        case 'h':
        default:
            return false; // triggers usage()
        }
    }
    const Method method = methods::find(_flags.method);
    if (optind + method.args <= argc) {
        for (int i = 0; i < method.args; ++i) {
            vespalib::string arg = argv[optind++];
            _flags.args.push_back(arg);
        }
    } else {
        std::cerr << "ERROR: method "<< _flags.method << " requires " << method.args
                  << " arguments, only got " << (argc - optind) << std::endl;
        return false;
    }
    if (optind != argc) {
        std::cerr << "ERROR: "<<(argc - optind)<<" extra arguments\n";
        return false;
    }
    _flags.method = method.rpcMethod;
    return true;
}

void
Application::usage(void)
{
    std::cerr <<
        "Usage: vespa-configproxy-cmd [options]"                                 << std::endl <<
        "    -m <method>                   method"                         << std::endl <<
        "    -s <hostname>                 hostname (default: localhost)"  << std::endl <<
        "    -p <port>                     port number (default: 19090)"   << std::endl <<
        "Available methods for -m option:"                                 << std::endl;
    methods::dump();
}

int
Application::main(int argc, char **argv)
{
    if (! parseOpts(argc, argv)) {
        usage();
        return 1;
    }
    ProxyCmd client(_flags);
    return client.action();
}

int main(int argc, char** argv) {
    vespalib::SignalHandler::PIPE.ignore();
    Application app;
    return app.main(argc, argv);
}