From: Michael Friedrich Date: Wed, 15 Oct 2014 12:12:59 +0000 (+0200) Subject: CLI: Add filter by name or type to 'object list' X-Git-Tag: v2.2.0~384 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=153693d284335977b3e567fee113bc5efe0f5b96;p=icinga2 CLI: Add filter by name or type to 'object list' fixes #7199 --- diff --git a/lib/cli/objectlistcommand.cpp b/lib/cli/objectlistcommand.cpp index 0f83cf316..08d0d3f65 100644 --- a/lib/cli/objectlistcommand.cpp +++ b/lib/cli/objectlistcommand.cpp @@ -56,8 +56,9 @@ void ObjectListCommand::InitParameters(boost::program_options::options_descripti boost::program_options::options_description& hiddenDesc, ArgumentCompletionDescription& argCompletionDesc) const { - /* Command doesn't support any parameters. */ - //TODO Add filter regex #7199 + visibleDesc.add_options() + ("name,n", po::value(), "filter by name matches") + ("type,t", po::value(), "filter by type matches"); } /** @@ -87,8 +88,15 @@ int ObjectListCommand::Run(const boost::program_options::variables_map& vm, cons std::map type_count; String message; + String name_filter, type_filter = ""; + + if (vm.count("name")) + name_filter = vm["name"].as(); + if (vm.count("type")) + type_filter = vm["type"].as(); + while (NetString::ReadStringFromStream(sfp, &message)) { - ReadObject(message, type_count); + ReadObject(message, type_count, name_filter, type_filter); objects_count++; } @@ -97,20 +105,23 @@ int ObjectListCommand::Run(const boost::program_options::variables_map& vm, cons std::cout << FormatTypeCounts(type_count) << std::endl; - Log(LogInformation, "cli", "Found " + Convert::ToString(objects_count) + " objects."); + Log(LogInformation, "cli", "Parsed " + Convert::ToString(objects_count) + " objects."); return 0; } -void ObjectListCommand::ReadObject(const String& message, std::map& type_count) +void ObjectListCommand::ReadObject(const String& message, std::map& type_count, String name_filter, String type_filter) { Dictionary::Ptr object = JsonDeserialize(message); + String name = object->Get("name"); String type = object->Get("type"); - type_count[type]++; + if(!name_filter.IsEmpty() && !Utility::Match(name_filter, name)) + return; + if(!type_filter.IsEmpty() && !Utility::Match(type_filter, type)) + return; - String name = object->Get("name"); bool abstract = object->Get("abstract"); Dictionary::Ptr properties = object->Get("properties"); Dictionary::Ptr debug_hints = object->Get("debug_hints"); @@ -129,6 +140,7 @@ void ObjectListCommand::ReadObject(const String& message, std::map& std::cout << msgbuf.str() << "\n"; + type_count[type]++; } String ObjectListCommand::FormatProperties(const Dictionary::Ptr& props, const Dictionary::Ptr& debug_hints, int indent) diff --git a/lib/cli/objectlistcommand.hpp b/lib/cli/objectlistcommand.hpp index 1de9ae304..2db02fa83 100644 --- a/lib/cli/objectlistcommand.hpp +++ b/lib/cli/objectlistcommand.hpp @@ -46,7 +46,7 @@ public: virtual int Run(const boost::program_options::variables_map& vm, const std::vector& ap) const; private: - static void ReadObject(const String& message, std::map& type_count); + static void ReadObject(const String& message, std::map& type_count, String name_filter, String type_filter); static String FormatProperties(const Dictionary::Ptr& props, const Dictionary::Ptr& debug_hints, int indent = 0); static String FormatHints(const Dictionary::Ptr& hints, int indent = 0); static String FormatHint(const Array::Ptr& msg, int indent = 0);