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"));
60 OM::dump(results[1], output);
61 } else if (message.find("all") != std::string::npos) {
63 * get the unique set of handlers, then invoke each
65 std::set<command_handler*> hdlrs;
66 for (auto h : *m_cmd_handlers) {
67 hdlrs.insert(h.second);
69 for (auto h : hdlrs) {
73 auto it = m_cmd_handlers->find(message);
75 if (it != m_cmd_handlers->end()) {
76 it->second->show(output);
78 output << "Unknown Command: " << message << std::endl;
85 inspect::register_handler(const std::vector<std::string>& cmds,
86 const std::string& help,
87 command_handler* handler)
89 if (!m_cmd_handlers) {
90 m_cmd_handlers.reset(new std::map<std::string, command_handler*>);
91 m_help_handlers.reset(
92 new std::deque<std::pair<std::vector<std::string>, std::string>>);
95 for (auto cmd : cmds) {
96 (*m_cmd_handlers)[cmd] = handler;
98 m_help_handlers->push_front(std::make_pair(cmds, help));
102 * fd.io coding-style-patch-verification: ON
105 * eval: (c-set-style "mozilla")