From: John Spray Date: Fri, 17 Jun 2016 14:23:03 +0000 (+0100) Subject: common/cmdparse: additional helpers X-Git-Tag: v11.0.1~60^2~67 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=6caae0e33436bdc5597a8a7e07e9744cf8b32fe6;p=ceph.git common/cmdparse: additional helpers For dumping a cmdmap to a Formatter, and for checking the prefix of a cmddesc. Used by ceph-mgr when routing commands to python modules. Signed-off-by: John Spray --- diff --git a/src/common/cmdparse.cc b/src/common/cmdparse.cc index 7c4ad58998d..ea8b8fe5ce6 100644 --- a/src/common/cmdparse.cc +++ b/src/common/cmdparse.cc @@ -14,12 +14,38 @@ #include #include "common/cmdparse.h" +#include "common/Formatter.h" #include "include/str_list.h" #include "json_spirit/json_spirit.h" #include "common/debug.h" using namespace std; +/** + * Given a cmddesc like "foo baz name=bar,type=CephString", + * return the prefix "foo baz". + */ +std::string cmddesc_get_prefix(const std::string &cmddesc) +{ + stringstream ss(cmddesc); + std::string word; + std::ostringstream result; + bool first = true; + while (std::getline(ss, word, ' ')) { + if (word.find_first_of(",=") != string::npos) { + break; + } + + if (!first) { + result << " "; + } + result << word; + first = false; + } + + return result.str(); +} + /** * Read a command description list out of cmd, and dump it to f. * A signature description is a set of space-separated words; @@ -110,6 +136,68 @@ dump_cmddesc_to_json(Formatter *jf, jf->close_section(); // cmd } + +void cmdmap_dump(const cmdmap_t &cmdmap, Formatter *f) +{ + assert(f != nullptr); + + class dump_visitor : public boost::static_visitor + { + Formatter *f; + std::string const &key; + public: + dump_visitor(Formatter *f_, std::string const &key_) + : f(f_), key(key_) + { + } + + void operator()(const std::string &operand) const + { + f->dump_string(key.c_str(), operand); + } + + void operator()(const bool &operand) const + { + f->dump_bool(key.c_str(), operand); + } + + void operator()(const int64_t &operand) const + { + f->dump_int(key.c_str(), operand); + } + + void operator()(const double &operand) const + { + f->dump_float(key.c_str(), operand); + } + + void operator()(const std::vector &operand) const + { + f->open_array_section(key.c_str()); + for (const auto i : operand) { + f->dump_string("item", i); + } + f->close_section(); + } + + void operator()(const std::vector &operand) const + { + f->open_array_section(key.c_str()); + for (const auto i : operand) { + f->dump_int("item", i); + } + f->close_section(); + } + }; + + //f->open_object_section("cmdmap"); + for (const auto &i : cmdmap) { + boost::apply_visitor(dump_visitor(f, i.first), i.second); + } + //f->close_section(); +} + + /** Parse JSON in vector cmd into a map from field to map of values * (use mValue/mObject) * 'cmd' should not disappear over lifetime of map diff --git a/src/common/cmdparse.h b/src/common/cmdparse.h index 0d022af71cb..b13bfbde7b6 100644 --- a/src/common/cmdparse.h +++ b/src/common/cmdparse.h @@ -25,6 +25,7 @@ typedef boost::variant> cmd_vartype; typedef std::map cmdmap_t; +std::string cmddesc_get_prefix(const std::string &cmddesc); void dump_cmd_to_json(ceph::Formatter *f, const std::string& cmd); void dump_cmd_and_help_to_json(ceph::Formatter *f, const std::string& secname, @@ -39,6 +40,7 @@ void dump_cmddesc_to_json(ceph::Formatter *jf, const std::string& avail); bool cmdmap_from_json(std::vector cmd, cmdmap_t *mapp, std::stringstream &ss); +void cmdmap_dump(const cmdmap_t &cmdmap, ceph::Formatter *f); void handle_bad_get(CephContext *cct, std::string k, const char *name); std::string cmd_vartype_stringify(const cmd_vartype& v);