aboutsummaryrefslogtreecommitdiffstats
path: root/config/src/apps/configproxy-cmd/main.cpp
blob: 34f2eeab1439f8072b198c5588b6b4af74744f8b (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
87
88
89
90
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include <vespa/fastos/fastos.h>
#include <iostream>
#include <vespa/vespalib/stllike/string.h>
#include "flags.h"
#include "proxycmd.h"
#include "methods.h"

class Application : public FastOS_Application
{
    Flags _flags;
    bool parseOpts();
public:
    void usage(void);
    int Main(void);

    Application() : _flags() {}
};

bool
Application::parseOpts()
{
    char c = '?';
    const char *optArg = NULL;
    int optInd = 0;
    while ((c = GetOpt("m:s:p:", optArg, optInd)) != -1) {
        switch (c) {
        case 'm':
            _flags.method = optArg;
            break;
        case 's':
            _flags.hostname = 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: 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(void)
{
    if (! parseOpts()) {
        usage();
        return 1;
    }
    ProxyCmd client(_flags);
    return client.action();
}

int
main(int argc, char** argv)
{
    Application app;
    return app.Entry(argc, argv);
}