]> git.proxmox.com Git - ceph.git/blob - ceph/src/test/common/get_command_descriptions.cc
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / test / common / get_command_descriptions.cc
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3 /*
4 * Ceph - scalable distributed file system
5 *
6 * Copyright (C) 2013 Cloudwatt <libre.licensing@cloudwatt.com>
7 *
8 * Author: Loic Dachary <loic@dachary.org>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Library Public License as published by
12 * the Free Software Foundation; either version 2, or (at your option)
13 * any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Library Public License for more details.
19 *
20 */
21
22 #include <stdio.h>
23 #include <signal.h>
24 #include "mon/Monitor.h"
25 #include "common/ceph_argparse.h"
26 #include "global/global_init.h"
27
28 static void usage(ostream &out)
29 {
30 out << "usage: get_command_descriptions [options ...]" << std::endl;
31 out << "print on stdout the result of JSON formatted options\n";
32 out << "found in mon/MonCommands.h as produced by the\n";
33 out << "Monitor.cc::get_command_descriptions function.\n";
34 out << "Designed as a helper for ceph_argparse.py unit tests.\n";
35 out << "\n";
36 out << " --all all of mon/MonCommands.h \n";
37 out << " --pull585 reproduce the bug fixed by #585\n";
38 out << "\n";
39 out << "Examples:\n";
40 out << " get_command_descriptions --all\n";
41 out << " get_command_descriptions --pull585\n";
42 }
43
44 static void json_print(const MonCommand *mon_commands, int size)
45 {
46 bufferlist rdata;
47 Formatter *f = Formatter::create("json");
48 Monitor::format_command_descriptions(mon_commands, size, f, &rdata);
49 delete f;
50 string data(rdata.c_str(), rdata.length());
51 cout << data << std::endl;
52 }
53
54 static void all()
55 {
56 #undef FLAG
57 #undef COMMAND
58 #undef COMMAND_WITH_FLAG
59 MonCommand mon_commands[] = {
60 #define FLAG(f) (MonCommand::FLAG_##f)
61 #define COMMAND(parsesig, helptext, modulename, req_perms, avail) \
62 {parsesig, helptext, modulename, req_perms, avail, 0},
63 #define COMMAND_WITH_FLAG(parsesig, helptext, modulename, req_perms, avail, flags) \
64 {parsesig, helptext, modulename, req_perms, avail, flags},
65 #include <mon/MonCommands.h>
66 #undef COMMAND
67 #undef COMMAND_WITH_FLAG
68
69 // FIXME: slurp up the Mgr commands too
70
71 #define COMMAND(parsesig, helptext, modulename, req_perms, avail) \
72 {parsesig, helptext, modulename, req_perms, avail, FLAG(MGR)},
73 #define COMMAND_WITH_FLAG(parsesig, helptext, modulename, req_perms, avail, flags) \
74 {parsesig, helptext, modulename, req_perms, avail, flags | FLAG(MGR)},
75 #include <mgr/MgrCommands.h>
76 #undef COMMAND
77 #undef COMMAND_WITH_FLAG
78 };
79
80 json_print(mon_commands, ARRAY_SIZE(mon_commands));
81 }
82
83 // syntax error https://github.com/ceph/ceph/pull/585
84 static void pull585()
85 {
86 MonCommand mon_commands[] = {
87 { "osd pool create "
88 "name=pool,type=CephPoolname "
89 "name=pg_num,type=CephInt,range=0 "
90 "name=pgp_num,type=CephInt,range=0,req=false" // !!! missing trailing space
91 "name=properties,type=CephString,n=N,req=false,goodchars=[A-Za-z0-9-_.=]",
92 "create pool", "osd", "rw", "cli,rest" }
93 };
94
95 json_print(mon_commands, ARRAY_SIZE(mon_commands));
96 }
97
98 int main(int argc, char **argv) {
99 vector<const char*> args;
100 argv_to_vec(argc, (const char **)argv, args);
101
102 auto cct = global_init(NULL, args, CEPH_ENTITY_TYPE_CLIENT,
103 CODE_ENVIRONMENT_UTILITY, 0);
104 common_init_finish(g_ceph_context);
105
106 if (args.empty()) {
107 usage(cerr);
108 exit(1);
109 }
110 for (std::vector<const char*>::iterator i = args.begin(); i != args.end(); ++i) {
111 string err;
112
113 if (*i == string("help") || *i == string("-h") || *i == string("--help")) {
114 usage(cout);
115 exit(0);
116 } else if (*i == string("--all")) {
117 all();
118 } else if (*i == string("--pull585")) {
119 pull585();
120 }
121 }
122 }
123
124 /*
125 * Local Variables:
126 * compile-command: "cd ../.. ;
127 * make get_command_descriptions &&
128 * ./get_command_descriptions --all --pull585"
129 * End:
130 */
131