From e98d719e5b3f658e69681217de87552bdc14e81b Mon Sep 17 00:00:00 2001 From: Michael Friedrich Date: Sat, 18 Oct 2014 19:31:52 +0200 Subject: [PATCH] CLI: Add feature enable/disable auto-completion Refactor feature list code. Add disabled features to list output. Add more console colors. Change output to stdout for general logging. fixes #7381 fixes #7415 refs #7376 --- lib/cli/CMakeLists.txt | 2 +- lib/cli/featuredisablecommand.cpp | 10 ++-- lib/cli/featuredisablecommand.hpp | 3 +- lib/cli/featureenablecommand.cpp | 15 +++-- lib/cli/featureenablecommand.hpp | 4 +- lib/cli/featurelistcommand.cpp | 52 ++++++---------- lib/cli/featurelistcommand.hpp | 4 -- lib/cli/featureutility.cpp | 99 +++++++++++++++++++++++++++++++ lib/cli/featureutility.hpp | 59 ++++++++++++++++++ 9 files changed, 193 insertions(+), 55 deletions(-) create mode 100644 lib/cli/featureutility.cpp create mode 100644 lib/cli/featureutility.hpp diff --git a/lib/cli/CMakeLists.txt b/lib/cli/CMakeLists.txt index aed5ce951..49f270e0c 100644 --- a/lib/cli/CMakeLists.txt +++ b/lib/cli/CMakeLists.txt @@ -18,7 +18,7 @@ set(cli_SOURCES agentaddcommand.cpp agentblackandwhitelistcommand.cpp agentlistcommand.cpp agentremovecommand.cpp agentsetcommand.cpp agentsetupcommand.cpp agentupdateconfigcommand.cpp agentwizardcommand.cpp agentutility.cpp - featureenablecommand.cpp featuredisablecommand.cpp featurelistcommand.cpp + featureenablecommand.cpp featuredisablecommand.cpp featurelistcommand.cpp featureutility.cpp objectlistcommand.cpp pkinewcacommand.cpp pkinewcertcommand.cpp pkisigncsrcommand.cpp pkirequestcommand.cpp pkiticketcommand.cpp repositoryobjectcommand.cpp diff --git a/lib/cli/featuredisablecommand.cpp b/lib/cli/featuredisablecommand.cpp index 17b06662b..0faa98687 100644 --- a/lib/cli/featuredisablecommand.cpp +++ b/lib/cli/featuredisablecommand.cpp @@ -18,10 +18,12 @@ ******************************************************************************/ #include "cli/featuredisablecommand.hpp" +#include "cli/featureutility.hpp" #include "base/logger_fwd.hpp" #include "base/clicommand.hpp" #include "base/application.hpp" #include "base/convert.hpp" +#include "base/console.hpp" #include #include #include @@ -42,10 +44,9 @@ String FeatureDisableCommand::GetShortDescription(void) const return "disables specified feature"; } -void FeatureDisableCommand::InitParameters(boost::program_options::options_description& visibleDesc, - boost::program_options::options_description& hiddenDesc) const +std::vector FeatureDisableCommand::GetPositionalSuggestions(const String& word) const { - /* Command doesn't support any parameters. */ + return FeatureUtility::GetFieldCompletionSuggestions(FeatureCommandDisable, word); } /** @@ -89,7 +90,8 @@ int FeatureDisableCommand::Run(const boost::program_options::variables_map& vm, continue; } - Log(LogInformation, "cli", "Disabling feature " + feature + " in '" + features_enabled_dir + "'."); + std::cout << "Disabling feature " << ConsoleColorTag(Console_ForegroundMagenta | Console_Bold) << feature + << ConsoleColorTag(Console_Normal) << ". Make sure to restart Icinga 2 for these changes to take effect.\n"; } if (!errors.empty()) { diff --git a/lib/cli/featuredisablecommand.hpp b/lib/cli/featuredisablecommand.hpp index fcb6f7839..5558442aa 100644 --- a/lib/cli/featuredisablecommand.hpp +++ b/lib/cli/featuredisablecommand.hpp @@ -38,8 +38,7 @@ public: virtual String GetDescription(void) const; virtual String GetShortDescription(void) const; - virtual void InitParameters(boost::program_options::options_description& visibleDesc, - boost::program_options::options_description& hiddenDesc) const; + virtual std::vector GetPositionalSuggestions(const String& word) const; virtual int Run(const boost::program_options::variables_map& vm, const std::vector& ap) const; }; diff --git a/lib/cli/featureenablecommand.cpp b/lib/cli/featureenablecommand.cpp index 642924bac..23dbe862b 100644 --- a/lib/cli/featureenablecommand.cpp +++ b/lib/cli/featureenablecommand.cpp @@ -18,16 +18,15 @@ ******************************************************************************/ #include "cli/featureenablecommand.hpp" +#include "cli/featureutility.hpp" #include "base/logger_fwd.hpp" #include "base/clicommand.hpp" #include "base/application.hpp" #include "base/convert.hpp" +#include "base/console.hpp" #include #include #include -#include -#include -#include using namespace icinga; namespace po = boost::program_options; @@ -44,10 +43,9 @@ String FeatureEnableCommand::GetShortDescription(void) const return "enables specified feature"; } -void FeatureEnableCommand::InitParameters(boost::program_options::options_description& visibleDesc, - boost::program_options::options_description& hiddenDesc) const +std::vector FeatureEnableCommand::GetPositionalSuggestions(const String& word) const { - /* Command doesn't support any parameters. */ + return FeatureUtility::GetFieldCompletionSuggestions(FeatureCommandEnable, word); } /** @@ -93,8 +91,6 @@ int FeatureEnableCommand::Run(const boost::program_options::variables_map& vm, c continue; } - Log(LogInformation, "cli", "Enabling feature '" + feature + "' in '" + features_enabled_dir + "'."); - #ifndef _WIN32 if (symlink(source.CStr(), target.CStr()) < 0) { Log(LogCritical, "cli", "Cannot enable feature '" + feature + "'. Linking source '" + source + "' to target file '" + target + @@ -113,6 +109,9 @@ int FeatureEnableCommand::Run(const boost::program_options::variables_map& vm, c fp << "include \"../features-available/" << feature << ".conf\"" << std::endl; fp.close(); #endif /* _WIN32 */ + + std::cout << "Enabling feature " << ConsoleColorTag(Console_ForegroundMagenta | Console_Bold) << feature + << ConsoleColorTag(Console_Normal) << ". Make sure to restart Icinga 2 for these changes to take effect.\n"; } if (!errors.empty()) { diff --git a/lib/cli/featureenablecommand.hpp b/lib/cli/featureenablecommand.hpp index e675fe465..bf5d6bab2 100644 --- a/lib/cli/featureenablecommand.hpp +++ b/lib/cli/featureenablecommand.hpp @@ -38,10 +38,8 @@ public: virtual String GetDescription(void) const; virtual String GetShortDescription(void) const; - virtual void InitParameters(boost::program_options::options_description& visibleDesc, - boost::program_options::options_description& hiddenDesc) const; + virtual std::vector GetPositionalSuggestions(const String& word) const; virtual int Run(const boost::program_options::variables_map& vm, const std::vector& ap) const; - }; } diff --git a/lib/cli/featurelistcommand.cpp b/lib/cli/featurelistcommand.cpp index 8dd47b86f..c5e09040d 100644 --- a/lib/cli/featurelistcommand.cpp +++ b/lib/cli/featurelistcommand.cpp @@ -18,15 +18,14 @@ ******************************************************************************/ #include "cli/featurelistcommand.hpp" +#include "cli/featureutility.hpp" #include "base/logger_fwd.hpp" #include "base/clicommand.hpp" -#include "base/application.hpp" +#include "base/convert.hpp" +#include "base/console.hpp" #include #include -#include -#include -#include -#include +#include using namespace icinga; namespace po = boost::program_options; @@ -54,36 +53,23 @@ int FeatureListCommand::Run(const boost::program_options::variables_map& vm, con Log(LogWarning, "cli", "Ignoring parameters: " + boost::algorithm::join(ap, " ")); } -#ifdef _WIN32 - //TODO: Add Windows support - Log(LogInformation, "cli", "This command is not available on Windows."); -#else - std::vector enabled_features; std::vector available_features; + std::vector disabled_features; + std::vector enabled_features; - if (!Utility::Glob(Application::GetSysconfDir() + "/icinga2/features-enabled/*.conf", - boost::bind(&FeatureListCommand::CollectFeatures, _1, boost::ref(enabled_features)), GlobFile)) { - Log(LogCritical, "cli", "Cannot access path '" + Application::GetSysconfDir() + "/icinga2/features-enabled/'."); - } + if (!FeatureUtility::GetFeatures(FeaturesAvailable, available_features)) + return 1; + if (!FeatureUtility::GetFeatures(FeaturesDisabled, disabled_features)) + return 1; + if (!FeatureUtility::GetFeatures(FeaturesEnabled, enabled_features)) + return 1; - if (!Utility::Glob(Application::GetSysconfDir() + "/icinga2/features-available/*.conf", - boost::bind(&FeatureListCommand::CollectFeatures, _1, boost::ref(available_features)), GlobFile)) { - Log(LogCritical, "cli", "Cannot access path '" + Application::GetSysconfDir() + "/icinga2/available-available/'."); - } - - Log(LogInformation, "cli", "Available features: " + boost::algorithm::join(available_features, " ")); - Log(LogInformation, "cli", "---"); - Log(LogInformation, "cli", "Enabled features: " + boost::algorithm::join(enabled_features, " ")); -#endif /* _WIN32 */ + std::cout << ConsoleColorTag(Console_ForegroundBlue | Console_Bold) << "Available features: " << ConsoleColorTag(Console_Normal) + << boost::algorithm::join(available_features, " ") << "\n"; + std::cout << ConsoleColorTag(Console_ForegroundRed | Console_Bold) << "Disabled features: " << ConsoleColorTag(Console_Normal) + << boost::algorithm::join(disabled_features, " ") << "\n"; + std::cout << ConsoleColorTag(Console_ForegroundGreen | Console_Bold) << "Enabled features: " << ConsoleColorTag(Console_Normal) + << boost::algorithm::join(enabled_features, " ") << "\n"; return 0; -} - -void FeatureListCommand::CollectFeatures(const String& feature_file, std::vector& features) -{ - String feature = Utility::BaseName(feature_file); - boost::algorithm::replace_all(feature, ".conf", ""); - - Log(LogDebug, "cli", "Adding feature: " + feature); - features.push_back(feature); -} +} \ No newline at end of file diff --git a/lib/cli/featurelistcommand.hpp b/lib/cli/featurelistcommand.hpp index 399bf3c69..1dcb18b09 100644 --- a/lib/cli/featurelistcommand.hpp +++ b/lib/cli/featurelistcommand.hpp @@ -39,10 +39,6 @@ public: virtual String GetDescription(void) const; virtual String GetShortDescription(void) const; virtual int Run(const boost::program_options::variables_map& vm, const std::vector& ap) const; - -private: - static void CollectFeatures(const String& feature_file, std::vector& features); - }; } diff --git a/lib/cli/featureutility.cpp b/lib/cli/featureutility.cpp new file mode 100644 index 000000000..91ab66d88 --- /dev/null +++ b/lib/cli/featureutility.cpp @@ -0,0 +1,99 @@ +/****************************************************************************** + * Icinga 2 * + * Copyright (C) 2012-2014 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. * + ******************************************************************************/ + +#include "cli/featureutility.hpp" +#include "base/logger_fwd.hpp" +#include "base/clicommand.hpp" +#include "base/application.hpp" +#include +#include + +using namespace icinga; + +std::vector FeatureUtility::GetFieldCompletionSuggestions(FeatureCommandType fctype, const String& word) +{ + std::vector cache; + std::vector suggestions; + + if (fctype == FeatureCommandEnable) { + /* only suggest features not already enabled */ + GetFeatures(FeaturesDisabled, cache); + } else if (fctype == FeatureCommandDisable) { + /* suggest all enabled features */ + GetFeatures(FeaturesEnabled, cache); + } + + std::sort(cache.begin(), cache.end()); + + BOOST_FOREACH(const String& suggestion, cache) { + if (suggestion.Find(word) == 0) + suggestions.push_back(suggestion); + } + + return suggestions; +} + +bool FeatureUtility::GetFeatures(FeatureType ftype, std::vector& features) +{ + String path = Application::GetSysconfDir() + "/icinga2/"; + + /* disabled = available-enabled */ + if (ftype == FeaturesDisabled) { + std::vector enabled; + std::vector available; + GetFeatures(FeaturesAvailable, available); + GetFeatures(FeaturesEnabled, enabled); + + std::sort(available.begin(), available.end()); + std::sort(enabled.begin(), enabled.end()); + std::set_difference( + available.begin(), available.end(), + enabled.begin(), enabled.end(), + std::back_inserter(features) + ); + + return true; + } else { + if (ftype == FeaturesAvailable) + path += "features-available/"; + else if (ftype == FeaturesEnabled) + path += "features-enabled/"; + else { + Log(LogCritical, "cli", "Unknown feature type passed. Bailing out."); + return false; + } + + if (!Utility::Glob(path + "/*.conf", + boost::bind(&FeatureUtility::CollectFeatures, _1, boost::ref(features)), GlobFile)) { + Log(LogCritical, "cli", "Cannot access path '" + path + "'."); + return false; + } + } + + return true; +} + +void FeatureUtility::CollectFeatures(const String& feature_file, std::vector& features) +{ + String feature = Utility::BaseName(feature_file); + boost::algorithm::replace_all(feature, ".conf", ""); + + Log(LogDebug, "cli", "Adding feature: " + feature); + features.push_back(feature); +} diff --git a/lib/cli/featureutility.hpp b/lib/cli/featureutility.hpp new file mode 100644 index 000000000..4af360296 --- /dev/null +++ b/lib/cli/featureutility.hpp @@ -0,0 +1,59 @@ +/****************************************************************************** + * Icinga 2 * + * Copyright (C) 2012-2014 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 FEATUREUTILITY_H +#define FEATUREUTILITY_H + +#include "base/i2-base.hpp" +#include "base/qstring.hpp" +#include + +namespace icinga +{ + +enum FeatureType +{ + FeaturesAvailable, + FeaturesEnabled, + FeaturesDisabled +}; + +enum FeatureCommandType +{ + FeatureCommandEnable, + FeatureCommandDisable +}; + +/** + * @ingroup cli + */ +class FeatureUtility +{ +public: + static std::vector GetFieldCompletionSuggestions(FeatureCommandType fctype, const String& word); + static bool GetFeatures(FeatureType ftype, std::vector& features); + +private: + FeatureUtility(void); + static void CollectFeatures(const String& feature_file, std::vector& features); +}; + +} + +#endif /* FEATUREUTILITY_H */