2 * Copyright (c) 2017 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
16 #include <boost/algorithm/string.hpp>
20 #include "vom/inspect.hpp"
21 #include "vom/logger.hpp"
25 std::unique_ptr<std::map<std::string, inspect::command_handler*>>
26 inspect::m_cmd_handlers;
28 std::unique_ptr<std::deque<std::pair<std::vector<std::string>, std::string>>>
29 inspect::m_help_handlers;
32 inspect::handle_input(const std::string& message, std::ostream& output)
34 if (message.length()) {
35 if (message.find("help") != std::string::npos) {
36 output << "Command Options: " << std::endl;
37 output << " keys - Show all keys owning objects"
39 output << " key:XXX - Show all object referenced by "
42 output << " all - Show All objects" << std::endl;
43 output << "Individual object_base Types:" << std::endl;
45 for (auto h : *m_help_handlers) {
48 for (auto s : h.first) {
55 } else if (message.find("keys") != std::string::npos) {
57 } else if (message.find("key") != std::string::npos) {
58 std::vector<std::string> results;
59 boost::split(results, message, boost::is_any_of(":\n"));
61 OM::dump(results[1], output);
62 } else if (message.find("all") != std::string::npos) {
64 * get the unique set of handlers, then invoke each
66 std::set<command_handler*> hdlrs;
67 for (auto h : *m_cmd_handlers) {
68 hdlrs.insert(h.second);
70 for (auto h : hdlrs) {
74 auto it = m_cmd_handlers->find(message);
76 if (it != m_cmd_handlers->end()) {
77 it->second->show(output);
79 output << "Unknown Command: " << message << std::endl;
86 inspect::register_handler(const std::vector<std::string>& cmds,
87 const std::string& help,
88 command_handler* handler)
90 if (!m_cmd_handlers) {
91 m_cmd_handlers.reset(new std::map<std::string, command_handler*>);
92 m_help_handlers.reset(
93 new std::deque<std::pair<std::vector<std::string>, std::string>>);
96 for (auto cmd : cmds) {
97 (*m_cmd_handlers)[cmd] = handler;
99 m_help_handlers->push_front(std::make_pair(cmds, help));
103 * fd.io coding-style-patch-verification: ON
106 * eval: (c-set-style "mozilla")