From c719333b6038d17d852d5864e4af68ff3088bebc Mon Sep 17 00:00:00 2001 From: Gunnar Beutner Date: Fri, 24 Oct 2014 12:04:14 +0200 Subject: [PATCH] Make the "agent list" and "agent remove" commands work refs #7245 --- lib/cli/agentlistcommand.cpp | 11 ++--- lib/cli/agentremovecommand.cpp | 2 +- lib/cli/agentutility.cpp | 89 ++++++++++++++++------------------ lib/cli/agentutility.hpp | 6 +-- 4 files changed, 48 insertions(+), 60 deletions(-) diff --git a/lib/cli/agentlistcommand.cpp b/lib/cli/agentlistcommand.cpp index 670c319e1..4d237b9b4 100644 --- a/lib/cli/agentlistcommand.cpp +++ b/lib/cli/agentlistcommand.cpp @@ -63,15 +63,10 @@ int AgentListCommand::Run(const boost::program_options::variables_map& vm, const << "Ignoring parameters: " << boost::algorithm::join(ap, " "); } - if (vm.count("batch")) { + if (vm.count("batch")) AgentUtility::PrintAgentsJson(std::cout); - std::cout << "\n"; - return 0; - } - - std::cout << "Configured agents: \n"; - AgentUtility::PrintAgents(std::cout); - std::cout << "\n"; + else + AgentUtility::PrintAgents(std::cout); return 0; } diff --git a/lib/cli/agentremovecommand.cpp b/lib/cli/agentremovecommand.cpp index 245589823..b6014626b 100644 --- a/lib/cli/agentremovecommand.cpp +++ b/lib/cli/agentremovecommand.cpp @@ -45,7 +45,7 @@ String AgentRemoveCommand::GetShortDescription(void) const std::vector AgentRemoveCommand::GetPositionalSuggestions(const String& word) const { - return AgentUtility::GetFieldCompletionSuggestions(word); + return AgentUtility::GetAgentCompletionSuggestions(word); } /** diff --git a/lib/cli/agentutility.cpp b/lib/cli/agentutility.cpp index dcd37b5c4..58690cb3b 100644 --- a/lib/cli/agentutility.cpp +++ b/lib/cli/agentutility.cpp @@ -54,18 +54,15 @@ String AgentUtility::GetAgentSettingsFile(const String& name) return GetRepositoryPath() + "/" + SHA256(name) + ".settings"; } -std::vector AgentUtility::GetFieldCompletionSuggestions(const String& word) +std::vector AgentUtility::GetAgentCompletionSuggestions(const String& word) { - std::vector cache; std::vector suggestions; - GetAgents(cache); + BOOST_FOREACH(const Dictionary::Ptr& agent, GetAgents()) { + String agent_name = agent->Get("endpoint"); - std::sort(cache.begin(), cache.end()); - - BOOST_FOREACH(const String& suggestion, cache) { - if (suggestion.Find(word) == 0) - suggestions.push_back(suggestion); + if (agent_name.Find(word) == 0) + suggestions.push_back(agent_name); } return suggestions; @@ -73,43 +70,46 @@ std::vector AgentUtility::GetFieldCompletionSuggestions(const String& wo void AgentUtility::PrintAgents(std::ostream& fp) { - std::vector agents; - GetAgents(agents); + bool first = false; - BOOST_FOREACH(const String& agent, agents) { - Dictionary::Ptr agent_obj = GetAgentFromRepository(GetAgentRepositoryFile(agent)); - fp << "Agent Name: " << agent << "\n"; + BOOST_FOREACH(const Dictionary::Ptr& agent, GetAgents()) { + if (first) + first = false; + else + fp << "\n"; - if (agent_obj) { - fp << "Endpoint: " << agent_obj->Get("endpoint") << "\n"; - fp << "Zone: " << agent_obj->Get("zone") << "\n"; - fp << "Repository: "; - fp << std::setw(4); - PrintAgentRepository(fp, agent_obj->Get("repository")); - fp << std::setw(0) << "\n"; - } + fp << "Agent '" + << ConsoleColorTag(Console_ForegroundBlue | Console_Bold) << agent->Get("endpoint") << ConsoleColorTag(Console_Normal) + << "' (last seen: " << Utility::FormatDateTime("%c", agent->Get("seen")) << ")\n"; + + PrintAgentRepository(fp, agent->Get("repository")); } - - fp << "All agents: " << boost::algorithm::join(agents, " ") << "\n"; } void AgentUtility::PrintAgentRepository(std::ostream& fp, const Dictionary::Ptr& repository) { - //TODO better formatting - fp << JsonSerialize(repository); + ObjectLock olock(repository); + BOOST_FOREACH(const Dictionary::Pair& kv, repository) { + fp << std::setw(4) << " " + << "* Host '" << ConsoleColorTag(Console_ForegroundGreen | Console_Bold) << kv.first << ConsoleColorTag(Console_Normal) << "'\n"; + + Array::Ptr services = kv.second; + ObjectLock xlock(services); + BOOST_FOREACH(const String& service, services) { + fp << std::setw(8) << " " << "* Service '" << ConsoleColorTag(Console_ForegroundGreen | Console_Bold) << service << ConsoleColorTag(Console_Normal) << "'\n"; + } + } } void AgentUtility::PrintAgentsJson(std::ostream& fp) { - std::vector agents; - GetAgents(agents); + Dictionary::Ptr result = make_shared(); - BOOST_FOREACH(const String& agent, agents) { - Dictionary::Ptr agent_obj = GetAgentFromRepository(GetAgentRepositoryFile(agent)); - if (agent_obj) { - fp << JsonSerialize(agent_obj); - } + BOOST_FOREACH(const Dictionary::Ptr& agent, GetAgents()) { + result->Set(agent->Get("endpoint"), agent); } + + fp << JsonSerialize(result); } bool AgentUtility::AddAgent(const String& name) @@ -230,34 +230,27 @@ Dictionary::Ptr AgentUtility::GetAgentFromRepository(const String& filename) String content((std::istreambuf_iterator(fp)), std::istreambuf_iterator()); - std::cout << "Content: " << content << "\n"; - fp.close(); return JsonDeserialize(content); } -bool AgentUtility::GetAgents(std::vector& agents) +std::vector AgentUtility::GetAgents(void) { - String path = GetRepositoryPath(); + std::vector agents; - if (!Utility::Glob(path + "/*.repo", - boost::bind(&AgentUtility::CollectAgents, _1, boost::ref(agents)), GlobFile)) { - Log(LogCritical, "cli") - << "Cannot access path '" << path << "'."; - return false; - } + Utility::Glob(GetRepositoryPath() + "/*.repo", + boost::bind(&AgentUtility::CollectAgents, _1, boost::ref(agents)), GlobFile); - return true; + return agents; } -void AgentUtility::CollectAgents(const String& agent_file, std::vector& agents) +void AgentUtility::CollectAgents(const String& agent_file, std::vector& agents) { - String agent = Utility::BaseName(agent_file); - boost::algorithm::replace_all(agent, ".repo", ""); + Dictionary::Ptr agent = GetAgentFromRepository(agent_file); - Log(LogDebug, "cli") - << "Adding agent: " << agent; + if (!agent) + return; agents.push_back(agent); } diff --git a/lib/cli/agentutility.hpp b/lib/cli/agentutility.hpp index d15ae95b5..dae441d9a 100644 --- a/lib/cli/agentutility.hpp +++ b/lib/cli/agentutility.hpp @@ -39,7 +39,7 @@ public: static String GetRepositoryPath(void); static String GetAgentRepositoryFile(const String& name); static String GetAgentSettingsFile(const String& name); - static std::vector GetFieldCompletionSuggestions(const String& word); + static std::vector GetAgentCompletionSuggestions(const String& word); static void PrintAgents(std::ostream& fp); static void PrintAgentsJson(std::ostream& fp); @@ -52,7 +52,7 @@ public: static bool WriteAgentToRepository(const String& filename, const Dictionary::Ptr& item); static Dictionary::Ptr GetAgentFromRepository(const String& filename); - static bool GetAgents(std::vector& agents); + static std::vector GetAgents(void); static bool CreateBackupFile(const String& target); @@ -67,7 +67,7 @@ public: private: AgentUtility(void); static bool RemoveAgentFile(const String& path); - static void CollectAgents(const String& agent_file, std::vector& agents); + static void CollectAgents(const String& agent_file, std::vector& agents); static void SerializeObject(std::ostream& fp, const String& name, const String& type, const Dictionary::Ptr& object); static void FormatValue(std::ostream& fp, const Value& val);