<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:
#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"
#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);
{
ScriptGlobal::Set("MatchAll", MatchAll);
ScriptGlobal::Set("MatchAny", MatchAny);
+
+ ScriptGlobal::Set("GlobFile", GlobFile);
+ ScriptGlobal::Set("GlobDirectory", GlobDirectory);
}
String ScriptUtils::CastString(const Value& value)
{
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);
+}