diff --git a/lib/cli/repositoryobjectcommand.cpp b/lib/cli/repositoryobjectcommand.cpp index 53716d8e6..22a328a21 100644 --- a/lib/cli/repositoryobjectcommand.cpp +++ b/lib/cli/repositoryobjectcommand.cpp @@ -146,6 +146,12 @@ int RepositoryObjectCommand::Run(const boost::program_options::variables_map& vm { Dictionary::Ptr attrs = RepositoryUtility::GetArgumentAttributes(ap); + /* shortcut for list command */ + if (m_Command == RepositoryCommandList) { + RepositoryUtility::PrintObjects(std::cout, m_Type); + return 0; + } + if (!attrs->Contains("name")) { Log(LogCritical, "cli", "Object requires a name (Hint: 'name=')!"); return 1; @@ -172,10 +178,8 @@ int RepositoryObjectCommand::Run(const boost::program_options::variables_map& vm attrs->Set("import", imports); } - if (m_Command == RepositoryCommandList) { - RepositoryUtility::PrintObjects(std::cout, m_Type); - } - else if (m_Command == RepositoryCommandAdd) { + + if (m_Command == RepositoryCommandAdd) { RepositoryUtility::AddObject(name, m_Type, attrs); } else if (m_Command == RepositoryCommandRemove) { diff --git a/lib/cli/repositoryutility.cpp b/lib/cli/repositoryutility.cpp index 169932dd3..1075c90f4 100644 --- a/lib/cli/repositoryutility.cpp +++ b/lib/cli/repositoryutility.cpp @@ -33,6 +33,7 @@ #include #include #include +#include #include #include @@ -65,9 +66,8 @@ String RepositoryUtility::GetRepositoryObjectConfigPath(const String& type, cons { String path = GetRepositoryConfigPath() + "/"; - if (type == "Host") { + if (type == "Host") path += "hosts"; - } else if (type == "Service") path += "hosts/" + object->Get("host_name"); else if (type == "Zone") @@ -78,6 +78,23 @@ String RepositoryUtility::GetRepositoryObjectConfigPath(const String& type, cons return path; } +bool RepositoryUtility::FilterRepositoryObjects(const String& type, const String& path) +{ + if (type == "Host") { + boost::regex expr("hosts/[^/]*.conf", boost::regex::icase); + boost::smatch what; + return boost::regex_search(path.GetData(), what, expr); + } + else if (type == "Service") + return Utility::Match("*hosts/*/*.conf", path); + else if (type == "Zone") + return Utility::Match("*zones/*.conf", path); + else if (type == "Endpoints") + return Utility::Match("*endpoints/*.conf", path); + + return false; +} + String RepositoryUtility::GetRepositoryObjectConfigFilePath(const String& type, const Dictionary::Ptr& object) { String path = GetRepositoryObjectConfigPath(type, object); @@ -97,12 +114,18 @@ void RepositoryUtility::PrintObjects(std::ostream& fp, const String& type) std::vector objects = GetObjects(); //full path BOOST_FOREACH(const String& object, objects) { - Dictionary::Ptr obj = GetObjectFromRepository(object); - - if (obj) { - fp << "Object Name: " << object << "\n"; - fp << JsonEncode(obj); + if (!FilterRepositoryObjects(type, object)) { + Log(LogDebug, "cli") + << "Ignoring object '" << object << "'. Type '" << type << "' does not match."; + continue; } + + fp << "Object Path: " << object << "\n"; + + Dictionary::Ptr obj = GetObjectFromRepository(object); //TODO: config parser not implemented yet! + + if (obj) + fp << JsonEncode(obj); } } @@ -203,7 +226,7 @@ bool RepositoryUtility::SetObjectAttributeInternal(const String& name, const Str //Fixme String path = GetRepositoryObjectConfigPath(type, attr) + "/" + name + ".conf"; - Dictionary::Ptr obj = GetObjectFromRepository(path); + Dictionary::Ptr obj = GetObjectFromRepository(path); //TODO if (!obj) { Log(LogCritical, "cli") @@ -305,9 +328,9 @@ Dictionary::Ptr RepositoryUtility::GetObjectFromRepositoryChangeLog(const String std::vector RepositoryUtility::GetObjects(void) { std::vector objects; - String path = GetRepositoryConfigPath() + "/"; + String path = GetRepositoryConfigPath(); - Utility::Glob(path + "/*.conf", + Utility::GlobRecursive(path, "*.conf", boost::bind(&RepositoryUtility::CollectObjects, _1, boost::ref(objects)), GlobFile); return objects; diff --git a/lib/cli/repositoryutility.hpp b/lib/cli/repositoryutility.hpp index 757b5221d..375c98706 100644 --- a/lib/cli/repositoryutility.hpp +++ b/lib/cli/repositoryutility.hpp @@ -44,6 +44,8 @@ public: static String GetRepositoryChangeLogPath(void); + static bool FilterRepositoryObjects(const String& type, const String& path); + static void PrintObjects(std::ostream& fp, const String& type); static void PrintChangeLog(std::ostream& fp);