Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 1 | /* |
| 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: |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 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. |
| 14 | */ |
| 15 | |
| 16 | #include <boost/algorithm/string.hpp> |
| 17 | #include <string> |
| 18 | #include <vector> |
| 19 | |
| 20 | #include "vom/inspect.hpp" |
| 21 | #include "vom/logger.hpp" |
| 22 | #include "vom/om.hpp" |
| 23 | |
| 24 | namespace VOM { |
| 25 | std::unique_ptr<std::map<std::string, inspect::command_handler*>> |
| 26 | inspect::m_cmd_handlers; |
| 27 | |
| 28 | std::unique_ptr<std::deque<std::pair<std::vector<std::string>, std::string>>> |
| 29 | inspect::m_help_handlers; |
| 30 | |
| 31 | void |
| 32 | inspect::handle_input(const std::string& message, std::ostream& output) |
| 33 | { |
| 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" |
| 38 | << std::endl; |
| 39 | output << " key:XXX - Show all object referenced by " |
| 40 | "key XXX" |
| 41 | << std::endl; |
| 42 | output << " all - Show All objects" << std::endl; |
| 43 | output << "Individual object_base Types:" << std::endl; |
| 44 | |
| 45 | for (auto h : *m_help_handlers) { |
| 46 | output << " {"; |
| 47 | |
| 48 | for (auto s : h.first) { |
| 49 | output << s << " "; |
| 50 | } |
| 51 | output << "} - \t"; |
| 52 | output << h.second; |
| 53 | output << std::endl; |
| 54 | } |
| 55 | } else if (message.find("keys") != std::string::npos) { |
| 56 | OM::dump(output); |
Mohsin Kazmi | 4e50d2f | 2017-11-09 13:30:59 +0100 | [diff] [blame] | 57 | } else if (message.find("key:") != std::string::npos) { |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 58 | std::vector<std::string> results; |
| 59 | boost::split(results, message, boost::is_any_of(":\n")); |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 60 | OM::dump(results[1], output); |
| 61 | } else if (message.find("all") != std::string::npos) { |
| 62 | /* |
| 63 | * get the unique set of handlers, then invoke each |
| 64 | */ |
| 65 | std::set<command_handler*> hdlrs; |
| 66 | for (auto h : *m_cmd_handlers) { |
| 67 | hdlrs.insert(h.second); |
| 68 | } |
| 69 | for (auto h : hdlrs) { |
| 70 | h->show(output); |
| 71 | } |
| 72 | } else { |
| 73 | auto it = m_cmd_handlers->find(message); |
| 74 | |
| 75 | if (it != m_cmd_handlers->end()) { |
| 76 | it->second->show(output); |
| 77 | } else { |
| 78 | output << "Unknown Command: " << message << std::endl; |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | void |
| 85 | inspect::register_handler(const std::vector<std::string>& cmds, |
| 86 | const std::string& help, |
| 87 | command_handler* handler) |
| 88 | { |
| 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>>); |
| 93 | } |
| 94 | |
| 95 | for (auto cmd : cmds) { |
| 96 | (*m_cmd_handlers)[cmd] = handler; |
| 97 | } |
| 98 | m_help_handlers->push_front(std::make_pair(cmds, help)); |
| 99 | } |
| 100 | } |
| 101 | /* |
| 102 | * fd.io coding-style-patch-verification: ON |
| 103 | * |
| 104 | * Local Variables: |
| 105 | * eval: (c-set-style "mozilla") |
| 106 | * End: |
| 107 | */ |