]> granicus.if.org Git - icinga2/commitdiff
CLI: Add filter by name or type to 'object list'
authorMichael Friedrich <michael.friedrich@netways.de>
Wed, 15 Oct 2014 12:12:59 +0000 (14:12 +0200)
committerMichael Friedrich <michael.friedrich@netways.de>
Wed, 15 Oct 2014 12:33:55 +0000 (14:33 +0200)
fixes #7199

lib/cli/objectlistcommand.cpp
lib/cli/objectlistcommand.hpp

index 0f83cf316e7ae27646c27c0594acfcce3b0da003..08d0d3f65f80ef0790243d07505e628bba189f63 100644 (file)
@@ -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<std::string>(), "filter by name matches")
+               ("type,t", po::value<std::string>(), "filter by type matches");
 }
 
 /**
@@ -87,8 +88,15 @@ int ObjectListCommand::Run(const boost::program_options::variables_map& vm, cons
        std::map<String, int> type_count;
 
        String message;
+       String name_filter, type_filter = "";
+
+       if (vm.count("name"))
+               name_filter = vm["name"].as<std::string>();
+       if (vm.count("type"))
+               type_filter = vm["type"].as<std::string>();
+
        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<String, int>& type_count)
+void ObjectListCommand::ReadObject(const String& message, std::map<String, int>& 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<String, int>&
 
        std::cout << msgbuf.str() << "\n";
 
+       type_count[type]++;
 }
 
 String ObjectListCommand::FormatProperties(const Dictionary::Ptr& props, const Dictionary::Ptr& debug_hints, int indent)
index 1de9ae304771f9b18d82dd86d7c592f0d4f14dda..2db02fa832705d60991685c3d8fc650c12b324ac 100644 (file)
@@ -46,7 +46,7 @@ public:
         virtual int Run(const boost::program_options::variables_map& vm, const std::vector<std::string>& ap) const;
 
 private:
-        static void ReadObject(const String& message, std::map<String, int>& type_count);
+        static void ReadObject(const String& message, std::map<String, int>& 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);