]> granicus.if.org Git - icinga2/commitdiff
Move objectlistcommands printObject to objectlistuility
authorJean Flach <jean-marcel.flach@netways.de>
Wed, 18 Feb 2015 09:23:27 +0000 (10:23 +0100)
committerJean Flach <jean-marcel.flach@netways.de>
Wed, 18 Feb 2015 09:23:27 +0000 (10:23 +0100)
PrintObject() is now public and now everybody can use pretty object printing

lib/cli/CMakeLists.txt
lib/cli/objectlistcommand.cpp
lib/cli/objectlistcommand.hpp
lib/cli/objectlistutility.cpp [new file with mode: 0644]
lib/cli/objectlistutility.hpp [new file with mode: 0644]

index 19f95232e953e5a5eb0e54ed7745228688a7b1e1..abf51041acafd0d0660111fc981e542df25706d6 100644 (file)
@@ -22,7 +22,7 @@ set(cli_SOURCES
   consolecommand.cpp
   daemoncommand.cpp daemonutility.cpp
   featureenablecommand.cpp featuredisablecommand.cpp featurelistcommand.cpp featureutility.cpp
-  objectlistcommand.cpp
+  objectlistcommand.cpp objectlistutility.cpp
   pkinewcacommand.cpp pkinewcertcommand.cpp pkisigncsrcommand.cpp pkirequestcommand.cpp pkisavecertcommand.cpp pkiticketcommand.cpp
   pkiutility.cpp
   repositoryclearchangescommand.cpp repositorycommitcommand.cpp repositoryobjectcommand.cpp repositoryutility.cpp
index 8cb346a726f96a407d5994f4b673b595094a91a3..c503ed364ecbf8c059a8ba541e7ca5edd75a3b52 100644 (file)
@@ -18,6 +18,7 @@
  ******************************************************************************/
 
 #include "cli/objectlistcommand.hpp"
+#include "cli/objectlistutility.hpp"
 #include "base/logger.hpp"
 #include "base/application.hpp"
 #include "base/convert.hpp"
@@ -103,7 +104,7 @@ int ObjectListCommand::Run(const boost::program_options::variables_map& vm, cons
                if (srs != StatusNewItem)
                        continue;
 
-               PrintObject(std::cout, first, message, type_count, name_filter, type_filter);
+               ObjectListUtility::PrintObject(std::cout, first, message, type_count, name_filter, type_filter);
                objects_count++;
        }
 
@@ -124,101 +125,6 @@ int ObjectListCommand::Run(const boost::program_options::variables_map& vm, cons
        return 0;
 }
 
-void ObjectListCommand::PrintObject(std::ostream& fp, bool& first, const String& message, std::map<String, int>& type_count, const String& name_filter, const String& type_filter)
-{
-       Dictionary::Ptr object = JsonDecode(message);
-
-       Dictionary::Ptr properties = object->Get("properties");
-
-       String internal_name = properties->Get("__name");
-       String name = object->Get("name");
-       String type = object->Get("type");
-
-       if (!name_filter.IsEmpty() && !Utility::Match(name_filter, name) && !Utility::Match(name_filter, internal_name))
-               return;
-       if (!type_filter.IsEmpty() && !Utility::Match(type_filter, type))
-               return;
-
-       if (first)
-               first = false;
-       else
-               fp << "\n";
-
-       Dictionary::Ptr debug_hints = object->Get("debug_hints");
-
-       fp << "Object '" << ConsoleColorTag(Console_ForegroundBlue | Console_Bold) << internal_name << ConsoleColorTag(Console_Normal) << "'";
-       fp << " of type '" << ConsoleColorTag(Console_ForegroundMagenta | Console_Bold) << type << ConsoleColorTag(Console_Normal) << "':\n";
-
-       Array::Ptr di = object->Get("debug_info");
-
-       if (di) {
-               fp << ConsoleColorTag(Console_ForegroundCyan) << "  % declared in '" << di->Get(0) << "', lines "
-                   << di->Get(1) << ":" << di->Get(2) << "-" << di->Get(3) << ":" << di->Get(4) << ConsoleColorTag(Console_Normal) << "\n";
-       }
-
-       PrintProperties(fp, properties, debug_hints, 2);
-
-       type_count[type]++;
-}
-
-void ObjectListCommand::PrintProperties(std::ostream& fp, const Dictionary::Ptr& props, const Dictionary::Ptr& debug_hints, int indent)
-{
-       /* get debug hint props */
-       Dictionary::Ptr debug_hint_props;
-       if (debug_hints)
-               debug_hint_props = debug_hints->Get("properties");
-
-       int offset = 2;
-
-       ObjectLock olock(props);
-       BOOST_FOREACH(const Dictionary::Pair& kv, props) {
-               String key = kv.first;
-               Value val = kv.second;
-
-               /* key & value */
-               fp << std::setw(indent) << " " << "* " << ConsoleColorTag(Console_ForegroundGreen) << key << ConsoleColorTag(Console_Normal);
-
-               /* extract debug hints for key */
-               Dictionary::Ptr debug_hints_fwd;
-               if (debug_hint_props)
-                       debug_hints_fwd = debug_hint_props->Get(key);
-
-               /* print dicts recursively */
-               if (val.IsObjectType<Dictionary>()) {
-                       fp << "\n";
-                       PrintHints(fp, debug_hints_fwd, indent + offset);
-                       PrintProperties(fp, val, debug_hints_fwd, indent + offset);
-               } else {
-                       fp << " = ";
-                       PrintValue(fp, val);
-                       fp << "\n";
-                       PrintHints(fp, debug_hints_fwd, indent + offset);
-               }
-       }
-}
-
-void ObjectListCommand::PrintHints(std::ostream& fp, const Dictionary::Ptr& debug_hints, int indent)
-{
-       if (!debug_hints)
-               return;
-
-       Array::Ptr messages = debug_hints->Get("messages");
-
-       if (messages) {
-               ObjectLock olock(messages);
-
-               BOOST_FOREACH(const Value& msg, messages) {
-                       PrintHint(fp, msg, indent);
-               }
-       }
-}
-
-void ObjectListCommand::PrintHint(std::ostream& fp, const Array::Ptr& msg, int indent)
-{
-       fp << std::setw(indent) << " " << ConsoleColorTag(Console_ForegroundCyan) << "% " << msg->Get(0) << " modified in '" << msg->Get(1) << "', lines "
-           << msg->Get(2) << ":" << msg->Get(3) << "-" << msg->Get(4) << ":" << msg->Get(5) << ConsoleColorTag(Console_Normal) << "\n";
-}
-
 void ObjectListCommand::PrintTypeCounts(std::ostream& fp, const std::map<String, int>& type_count)
 {
        typedef std::map<String, int>::value_type TypeCount;
@@ -231,48 +137,4 @@ void ObjectListCommand::PrintTypeCounts(std::ostream& fp, const std::map<String,
 
                fp << ".\n";
        }
-}
-
-void ObjectListCommand::PrintValue(std::ostream& fp, const Value& val)
-{
-       if (val.IsObjectType<Array>()) {
-               PrintArray(fp, val);
-               return;
-       }
-
-       if (val.IsString()) {
-               fp << "\"" << Convert::ToString(val) << "\"";
-               return;
-       }
-
-       if (val.IsEmpty()) {
-               fp << "null";
-               return;
-       }
-
-       fp << Convert::ToString(val);
-}
-
-void ObjectListCommand::PrintArray(std::ostream& fp, const Array::Ptr& arr)
-{
-       bool first = true;
-
-       fp << "[ ";
-
-       if (arr) {
-               ObjectLock olock(arr);
-               BOOST_FOREACH(const Value& value, arr) {
-                       if (first)
-                               first = false;
-                       else
-                               fp << ", ";
-
-                       PrintValue(fp, value);
-               }
-       }
-
-       if (!first)
-               fp << " ";
-
-       fp << "]";
-}
+}
\ No newline at end of file
index 8b30f8a0d8f6c22b09097aaa9c3099b574f55b9e..fad0ffe7b2e1ca1f7a342df4b4e946babf661ba9 100644 (file)
@@ -45,13 +45,7 @@ public:
        virtual int Run(const boost::program_options::variables_map& vm, const std::vector<std::string>& ap) const;
 
 private:
-       static void PrintObject(std::ostream& fp, bool& first, const String& message, std::map<String, int>& type_count, const String& name_filter, const String& type_filter);
-       static void PrintProperties(std::ostream& fp, const Dictionary::Ptr& props, const Dictionary::Ptr& debug_hints, int indent = 0);
-       static void PrintHints(std::ostream& fp, const Dictionary::Ptr& hints, int indent = 0);
-       static void PrintHint(std::ostream& fp, const Array::Ptr& msg, int indent = 0);
        static void PrintTypeCounts(std::ostream& fp, const std::map<String, int>& type_count);
-       static void PrintValue(std::ostream& fp, const Value& val);
-       static void PrintArray(std::ostream& fp, const Array::Ptr& arr);
 };
 
 }
diff --git a/lib/cli/objectlistutility.cpp b/lib/cli/objectlistutility.cpp
new file mode 100644 (file)
index 0000000..746fe9f
--- /dev/null
@@ -0,0 +1,154 @@
+#include "cli/objectlistutility.hpp"
+#include "base/json.hpp"
+#include "base/utility.hpp"
+#include "base/console.hpp"
+#include "base/objectlock.hpp"
+#include "base/convert.hpp"
+#include <boost/foreach.hpp>
+#include <iostream>
+#include <iomanip>
+
+using namespace icinga;
+
+bool ObjectListUtility::PrintObject(std::ostream& fp, bool& first, const String& message, std::map<String, int>& type_count, const String& name_filter, const String& type_filter)
+{
+       Dictionary::Ptr object = JsonDecode(message);
+
+       Dictionary::Ptr properties = object->Get("properties");
+
+       String internal_name = properties->Get("__name");
+       String name = object->Get("name");
+       String type = object->Get("type");
+
+       if (!name_filter.IsEmpty() && !Utility::Match(name_filter, name) && !Utility::Match(name_filter, internal_name))
+               return false;
+       if (!type_filter.IsEmpty() && !Utility::Match(type_filter, type))
+               return false;
+
+       if (first)
+               first = false;
+       else
+               fp << "\n";
+
+       Dictionary::Ptr debug_hints = object->Get("debug_hints");
+
+       fp << "Object '" << ConsoleColorTag(Console_ForegroundBlue | Console_Bold) << internal_name << ConsoleColorTag(Console_Normal) << "'";
+       fp << " of type '" << ConsoleColorTag(Console_ForegroundMagenta | Console_Bold) << type << ConsoleColorTag(Console_Normal) << "':\n";
+
+       Array::Ptr di = object->Get("debug_info");
+
+       if (di) {
+               fp << ConsoleColorTag(Console_ForegroundCyan) << "  % declared in '" << di->Get(0) << "', lines "
+                       << di->Get(1) << ":" << di->Get(2) << "-" << di->Get(3) << ":" << di->Get(4) << ConsoleColorTag(Console_Normal) << "\n";
+       }
+
+       PrintProperties(fp, properties, debug_hints, 2);
+
+       type_count[type]++;
+       return true;
+}
+
+void ObjectListUtility::PrintProperties(std::ostream& fp, const Dictionary::Ptr& props, const Dictionary::Ptr& debug_hints, int indent)
+{
+       /* get debug hint props */
+       Dictionary::Ptr debug_hint_props;
+       if (debug_hints)
+               debug_hint_props = debug_hints->Get("properties");
+
+       int offset = 2;
+
+       ObjectLock olock(props);
+       BOOST_FOREACH(const Dictionary::Pair& kv, props)
+       {
+               String key = kv.first;
+               Value val = kv.second;
+
+               /* key & value */
+               fp << std::setw(indent) << " " << "* " << ConsoleColorTag(Console_ForegroundGreen) << key << ConsoleColorTag(Console_Normal);
+
+               /* extract debug hints for key */
+               Dictionary::Ptr debug_hints_fwd;
+               if (debug_hint_props)
+                       debug_hints_fwd = debug_hint_props->Get(key);
+
+               /* print dicts recursively */
+               if (val.IsObjectType<Dictionary>()) {
+                       fp << "\n";
+                       PrintHints(fp, debug_hints_fwd, indent + offset);
+                       PrintProperties(fp, val, debug_hints_fwd, indent + offset);
+               } else {
+                       fp << " = ";
+                       PrintValue(fp, val);
+                       fp << "\n";
+                       PrintHints(fp, debug_hints_fwd, indent + offset);
+               }
+       }
+}
+
+void ObjectListUtility::PrintHints(std::ostream& fp, const Dictionary::Ptr& debug_hints, int indent)
+{
+       if (!debug_hints)
+               return;
+
+       Array::Ptr messages = debug_hints->Get("messages");
+
+       if (messages) {
+               ObjectLock olock(messages);
+
+               BOOST_FOREACH(const Value& msg, messages)
+               {
+                       PrintHint(fp, msg, indent);
+               }
+       }
+}
+
+void ObjectListUtility::PrintHint(std::ostream& fp, const Array::Ptr& msg, int indent)
+{
+       fp << std::setw(indent) << " " << ConsoleColorTag(Console_ForegroundCyan) << "% " << msg->Get(0) << " modified in '" << msg->Get(1) << "', lines "
+               << msg->Get(2) << ":" << msg->Get(3) << "-" << msg->Get(4) << ":" << msg->Get(5) << ConsoleColorTag(Console_Normal) << "\n";
+}
+
+void ObjectListUtility::PrintValue(std::ostream& fp, const Value& val)
+{
+       if (val.IsObjectType<Array>()) {
+               PrintArray(fp, val);
+               return;
+       }
+
+       if (val.IsString()) {
+               fp << "\"" << Convert::ToString(val) << "\"";
+               return;
+       }
+
+       if (val.IsEmpty()) {
+               fp << "null";
+               return;
+       }
+
+       fp << Convert::ToString(val);
+}
+
+void ObjectListUtility::PrintArray(std::ostream& fp, const Array::Ptr& arr)
+{
+       bool first = true;
+
+       fp << "[ ";
+
+       if (arr) {
+               ObjectLock olock(arr);
+               BOOST_FOREACH(const Value& value, arr)
+               {
+                       if (first)
+                               first = false;
+                       else
+                               fp << ", ";
+
+                       PrintValue(fp, value);
+               }
+       }
+
+       if (!first)
+               fp << " ";
+
+       fp << "]";
+}
\ No newline at end of file
diff --git a/lib/cli/objectlistutility.hpp b/lib/cli/objectlistutility.hpp
new file mode 100644 (file)
index 0000000..5fae1dd
--- /dev/null
@@ -0,0 +1,48 @@
+/******************************************************************************
+* Icinga 2                                                                   *
+* Copyright (C) 2012-2015 Icinga Development Team (http://www.icinga.org)    *
+*                                                                            *
+* This program is free software; you can redistribute it and/or              *
+* modify it under the terms of the GNU General Public License                *
+* as published by the Free Software Foundation; either version 2             *
+* of the License, or (at your option) any later version.                     *
+*                                                                            *
+* This program is distributed in the hope that it will be useful,            *
+* but WITHOUT ANY WARRANTY; without even the implied warranty of             *
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
+* GNU General Public License for more details.                               *
+*                                                                            *
+* You should have received a copy of the GNU General Public License          *
+* along with this program; if not, write to the Free Software Foundation     *
+* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.             *
+******************************************************************************/
+#ifndef OBJECTLISTUTILITY_H
+#define OBJECTLISTUTILITY_H
+
+#include "base/i2-base.hpp"
+#include "base/dictionary.hpp"
+#include "base/array.hpp"
+#include "base/value.hpp"
+#include "base/string.hpp"
+
+namespace icinga
+{
+class ObjectListUtility
+{
+public:
+       /*
+        * Print decoded json blob *message* to *fp*, filtering by *name_filter* and *type_filter*.
+        * An enumeration by type is written to *type_count*
+        * *first* needs to be true
+        * returns true if object was printed
+       */
+       static bool PrintObject(std::ostream& fp, bool& first, const String& message, std::map<String, int>& type_count, const String& name_filter, const String& type_filter);
+private:
+       static void PrintProperties(std::ostream& fp, const Dictionary::Ptr& props, const Dictionary::Ptr& debug_hints, int indent);
+       static void PrintHints(std::ostream& fp, const Dictionary::Ptr& debug_hints, int indent);
+       static void PrintHint(std::ostream& fp, const Array::Ptr& msg, int indent);
+       static void PrintValue(std::ostream& fp, const Value& val);
+       static void PrintArray(std::ostream& fp, const Array::Ptr& arr);
+};
+}
+#endif /* OBJECTLISTUTILITY_H */
\ No newline at end of file