]> granicus.if.org Git - icinga2/commitdiff
Implement new script functions: path_exists, glob and glob_recursive 5580/head
authorGunnar Beutner <gunnar.beutner@icinga.com>
Mon, 18 Sep 2017 07:08:53 +0000 (09:08 +0200)
committerMichael Friedrich <michael.friedrich@icinga.com>
Mon, 18 Sep 2017 11:59:03 +0000 (13:59 +0200)
doc/18-library-reference.md
lib/base/scriptutils.cpp
lib/base/scriptutils.hpp

index 7caa64613558e82d11f960720f11c97c82e55d74..f0af1138da14c808eb33992e09eb1322104b9c2b 100644 (file)
@@ -459,6 +459,65 @@ Example:
     <2> => basename(path)
     "xmpp-notification.pl"
 
+### path\_exists <a id="global-functions-path-exists"></a>
+
+Signature:
+
+    function path_exists(path)
+
+Returns true if the specified path exists, false otherwise.
+
+Example:
+
+    $ icinga2 console
+    Icinga 2 (version: v2.7.0)
+    <1> => var path = "/etc/icinga2/scripts/xmpp-notification.pl"
+    null
+    <2> => path_exists(path)
+    true
+
+### glob <a id="global-functions-glob"></a>
+
+Signature:
+
+    function glob(pathSpec, type)
+
+Returns an array containing all paths which match the
+`pathSpec` argument.
+
+The `type` argument is optional and specifies which types
+of paths are matched. This can be a combination of the `GlobFile`
+and `GlobDirectory` constants. The default value is `GlobFile | GlobDirectory`.
+
+    $ icinga2 console
+    Icinga 2 (version: v2.7.0)
+    <1> => var pathSpec = "/etc/icinga2/conf.d/*.conf"
+    null
+    <2> => glob(pathSpec)
+    [ "/etc/icinga2/conf.d/app.conf", "/etc/icinga2/conf.d/commands.conf", ... ]
+
+### glob\_recursive <a id="global-functions-glob-recursive"></a>
+
+Signature:
+
+    function glob_recursive(path, pattern, type)
+
+Recursively descends into the specified directory and returns an array containing
+all paths which match the `pattern` argument.
+
+The `type` argument is optional and specifies which types
+of paths are matched. This can be a combination of the `GlobFile`
+and `GlobDirectory` constants. The default value is `GlobFile | GlobDirectory`.
+
+    $ icinga2 console
+    Icinga 2 (version: v2.7.0)
+    <1> => var path = "/etc/icinga2/zones.d/"
+    null
+    <2> => var pattern = "*.conf"
+    null
+    <3> => glob_recursive(path, pattern)
+    [ "/etc/icinga2/zones.d/global-templates/templates.conf", "/etc/icinga2/zones.d/master/hosts.conf", ... ]
+
 ### escape_shell_arg <a id="global-functions-escape_shell_arg"></a>
 
 Signature:
index ef70eccbf8ba62997e06d5865d2a1c2d99f0ec56..7335c58fcc79a49989f1a24414fc4eefaa288a25 100644 (file)
@@ -19,6 +19,8 @@
 
 #include "base/scriptutils.hpp"
 #include "base/function.hpp"
+#include "base/scriptframe.hpp"
+#include "base/exception.hpp"
 #include "base/utility.hpp"
 #include "base/convert.hpp"
 #include "base/json.hpp"
@@ -67,6 +69,9 @@ REGISTER_SAFE_SCRIPTFUNCTION_NS(System, escape_create_process_arg, &Utility::Esc
 #endif /* _WIN32 */
 REGISTER_SCRIPTFUNCTION_NS(System, ptr, &ScriptUtils::Ptr, "object");
 REGISTER_SCRIPTFUNCTION_NS(System, sleep, &Utility::Sleep, "interval");
+REGISTER_SCRIPTFUNCTION_NS(System, path_exists, &Utility::PathExists, "path");
+REGISTER_SCRIPTFUNCTION_NS(System, glob, &ScriptUtils::Glob, "pathspec:callback:type");
+REGISTER_SCRIPTFUNCTION_NS(System, glob_recursive, &ScriptUtils::GlobRecursive, "pathspec:callback:type");
 
 INITIALIZE_ONCE(&ScriptUtils::StaticInitialize);
 
@@ -80,6 +85,9 @@ void ScriptUtils::StaticInitialize(void)
 {
        ScriptGlobal::Set("MatchAll", MatchAll);
        ScriptGlobal::Set("MatchAny", MatchAny);
+
+       ScriptGlobal::Set("GlobFile", GlobFile);
+       ScriptGlobal::Set("GlobDirectory", GlobDirectory);
 }
 
 String ScriptUtils::CastString(const Value& value)
@@ -442,3 +450,44 @@ double ScriptUtils::Ptr(const Object::Ptr& object)
 {
        return reinterpret_cast<intptr_t>(object.get());
 }
+
+static void GlobCallbackHelper(std::vector<String>& paths, const String& path)
+{
+       paths.push_back(path);
+}
+
+Value ScriptUtils::Glob(const std::vector<Value>& args)
+{
+       if (args.size() < 1)
+               BOOST_THROW_EXCEPTION(std::invalid_argument("Path must be specified."));
+
+       String pathSpec = args[0];
+       int type = GlobFile | GlobDirectory;
+
+       if (args.size() > 1)
+               type = args[1];
+
+       std::vector<String> paths;
+       Utility::Glob(pathSpec, boost::bind(&GlobCallbackHelper, boost::ref(paths), _1), type);
+
+       return Array::FromVector(paths);
+}
+
+Value ScriptUtils::GlobRecursive(const std::vector<Value>& args)
+{
+       if (args.size() < 2)
+               BOOST_THROW_EXCEPTION(std::invalid_argument("Path and pattern must be specified."));
+
+       String path = args[0];
+       String pattern = args[1];
+
+       int type = GlobFile | GlobDirectory;
+
+       if (args.size() > 2)
+               type = args[2];
+
+       std::vector<String> paths;
+       Utility::GlobRecursive(path, pattern, boost::bind(&GlobCallbackHelper, boost::ref(paths), _1), type);
+
+       return Array::FromVector(paths);
+}
index e605be5faa4f46b30956009a4b5d25a7f7bb5a31..313c8f4eb9ad5b496fc1e06f94a8613e913edcca 100644 (file)
@@ -56,6 +56,8 @@ public:
        static String MsiGetComponentPathShim(const String& component);
        static Array::Ptr TrackParents(const Object::Ptr& parent);
        static double Ptr(const Object::Ptr& object);
+       static Value Glob(const std::vector<Value>& args);
+       static Value GlobRecursive(const std::vector<Value>& args);
 
 private:
        ScriptUtils(void);