2014-10-06 14:21:18 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
2015-01-22 12:00:23 +01:00
|
|
|
* Copyright (C) 2012-2015 Icinga Development Team (http://www.icinga.org) *
|
2014-10-06 14:21:18 +02:00
|
|
|
* *
|
|
|
|
* 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. *
|
|
|
|
******************************************************************************/
|
|
|
|
|
2014-10-20 13:58:21 +02:00
|
|
|
#include "cli/clicommand.hpp"
|
2014-10-19 14:21:12 +02:00
|
|
|
#include "base/logger.hpp"
|
2014-10-17 18:55:34 +02:00
|
|
|
#include "base/type.hpp"
|
|
|
|
#include "base/serializer.hpp"
|
2014-10-06 14:21:18 +02:00
|
|
|
#include <boost/algorithm/string/join.hpp>
|
2014-10-14 16:45:00 +02:00
|
|
|
#include <boost/algorithm/string/trim.hpp>
|
2014-10-06 14:21:18 +02:00
|
|
|
#include <boost/foreach.hpp>
|
|
|
|
#include <boost/program_options.hpp>
|
|
|
|
#include <algorithm>
|
2014-10-16 15:33:03 +02:00
|
|
|
#include <iostream>
|
2014-10-06 14:21:18 +02:00
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
namespace po = boost::program_options;
|
|
|
|
|
2014-10-17 15:54:46 +02:00
|
|
|
std::vector<String> icinga::GetBashCompletionSuggestions(const String& type, const String& word)
|
2014-10-14 16:45:00 +02:00
|
|
|
{
|
|
|
|
std::vector<String> result;
|
|
|
|
|
|
|
|
#ifndef _WIN32
|
2014-10-17 15:54:46 +02:00
|
|
|
String bashArg = "compgen -A " + Utility::EscapeShellArg(type) + " " + Utility::EscapeShellArg(word);
|
2014-10-14 16:45:00 +02:00
|
|
|
String cmd = "bash -c " + Utility::EscapeShellArg(bashArg);
|
|
|
|
|
|
|
|
FILE *fp = popen(cmd.CStr(), "r");
|
|
|
|
|
|
|
|
char line[4096];
|
|
|
|
while (fgets(line, sizeof(line), fp)) {
|
|
|
|
String wline = line;
|
|
|
|
boost::algorithm::trim_right_if(wline, boost::is_any_of("\r\n"));
|
|
|
|
result.push_back(wline);
|
|
|
|
}
|
|
|
|
fclose(fp);
|
|
|
|
|
|
|
|
/* Append a slash if there's only one suggestion and it's a directory */
|
|
|
|
if ((type == "file" || type == "directory") && result.size() == 1) {
|
|
|
|
String path = result[0];
|
|
|
|
|
|
|
|
struct stat statbuf;
|
|
|
|
if (lstat(path.CStr(), &statbuf) >= 0) {
|
|
|
|
if (S_ISDIR(statbuf.st_mode)) {
|
|
|
|
result.clear(),
|
|
|
|
result.push_back(path + "/");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* _WIN32 */
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2014-11-03 00:44:04 +01:00
|
|
|
std::vector<String> icinga::GetFieldCompletionSuggestions(const Type::Ptr& type, const String& word)
|
2014-10-17 13:03:01 +02:00
|
|
|
{
|
2014-10-17 15:54:46 +02:00
|
|
|
std::vector<String> result;
|
|
|
|
|
2014-10-17 14:13:31 +02:00
|
|
|
for (int i = 0; i < type->GetFieldCount(); i++) {
|
|
|
|
Field field = type->GetFieldInfo(i);
|
2014-10-17 13:03:01 +02:00
|
|
|
|
2014-10-31 08:53:25 +01:00
|
|
|
if (!(field.Attributes & FAConfig) || field.Attributes & FAInternal)
|
2014-10-17 13:03:01 +02:00
|
|
|
continue;
|
|
|
|
|
2014-11-10 20:06:28 +01:00
|
|
|
if (strcmp(field.TypeName, "int") != 0 && strcmp(field.TypeName, "double") != 0
|
|
|
|
&& strcmp(field.TypeName, "bool") != 0 && strcmp(field.TypeName, "String") != 0)
|
2014-10-31 08:49:14 +01:00
|
|
|
continue;
|
|
|
|
|
2014-10-17 15:54:46 +02:00
|
|
|
String fname = field.Name;
|
|
|
|
|
|
|
|
String suggestion = fname + "=";
|
|
|
|
|
|
|
|
if (suggestion.Find(word) == 0)
|
|
|
|
result.push_back(suggestion);
|
2014-10-17 13:03:01 +02:00
|
|
|
}
|
|
|
|
|
2014-10-17 15:54:46 +02:00
|
|
|
return result;
|
2014-10-14 16:45:00 +02:00
|
|
|
}
|
|
|
|
|
2014-10-24 13:15:21 +02:00
|
|
|
int CLICommand::GetMinArguments(void) const
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int CLICommand::GetMaxArguments(void) const
|
|
|
|
{
|
|
|
|
return GetMinArguments();
|
|
|
|
}
|
|
|
|
|
2014-11-17 18:42:22 +01:00
|
|
|
bool CLICommand::IsHidden(void) const
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-10-20 13:58:21 +02:00
|
|
|
boost::mutex& CLICommand::GetRegistryMutex(void)
|
|
|
|
{
|
|
|
|
static boost::mutex mtx;
|
|
|
|
return mtx;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::map<std::vector<String>, CLICommand::Ptr>& CLICommand::GetRegistry(void)
|
|
|
|
{
|
|
|
|
static std::map<std::vector<String>, CLICommand::Ptr> registry;
|
|
|
|
return registry;
|
|
|
|
}
|
|
|
|
|
2014-10-06 14:21:18 +02:00
|
|
|
CLICommand::Ptr CLICommand::GetByName(const std::vector<String>& name)
|
|
|
|
{
|
2014-10-20 13:58:21 +02:00
|
|
|
boost::mutex::scoped_lock lock(GetRegistryMutex());
|
2014-10-06 14:21:18 +02:00
|
|
|
|
2014-10-20 13:58:21 +02:00
|
|
|
std::map<std::vector<String>, CLICommand::Ptr>::const_iterator it = GetRegistry().find(name);
|
2014-10-06 14:21:18 +02:00
|
|
|
|
2014-10-20 13:58:21 +02:00
|
|
|
if (it == GetRegistry().end())
|
2014-10-06 14:21:18 +02:00
|
|
|
return CLICommand::Ptr();
|
|
|
|
|
|
|
|
return it->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CLICommand::Register(const std::vector<String>& name, const CLICommand::Ptr& function)
|
|
|
|
{
|
2014-10-20 13:58:21 +02:00
|
|
|
boost::mutex::scoped_lock lock(GetRegistryMutex());
|
|
|
|
GetRegistry()[name] = function;
|
2014-10-06 14:21:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CLICommand::Unregister(const std::vector<String>& name)
|
|
|
|
{
|
2014-10-20 13:58:21 +02:00
|
|
|
boost::mutex::scoped_lock lock(GetRegistryMutex());
|
|
|
|
GetRegistry().erase(name);
|
2014-10-06 14:21:18 +02:00
|
|
|
}
|
|
|
|
|
2014-10-17 15:54:46 +02:00
|
|
|
std::vector<String> CLICommand::GetArgumentSuggestions(const String& argument, const String& word) const
|
|
|
|
{
|
|
|
|
return std::vector<String>();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<String> CLICommand::GetPositionalSuggestions(const String& word) const
|
|
|
|
{
|
|
|
|
return std::vector<String>();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CLICommand::InitParameters(boost::program_options::options_description& visibleDesc,
|
|
|
|
boost::program_options::options_description& hiddenDesc) const
|
|
|
|
{ }
|
|
|
|
|
2014-10-24 15:29:46 +02:00
|
|
|
ImpersonationLevel CLICommand::GetImpersonationLevel(void) const
|
|
|
|
{
|
|
|
|
return ImpersonateIcinga;
|
|
|
|
}
|
|
|
|
|
2014-10-10 11:08:24 +02:00
|
|
|
bool CLICommand::ParseCommand(int argc, char **argv, po::options_description& visibleDesc,
|
2014-10-14 16:45:00 +02:00
|
|
|
po::options_description& hiddenDesc,
|
|
|
|
po::positional_options_description& positionalDesc,
|
|
|
|
po::variables_map& vm, String& cmdname, CLICommand::Ptr& command, bool autocomplete)
|
2014-10-06 14:21:18 +02:00
|
|
|
{
|
2014-10-20 13:58:21 +02:00
|
|
|
boost::mutex::scoped_lock lock(GetRegistryMutex());
|
2014-10-06 14:21:18 +02:00
|
|
|
|
|
|
|
typedef std::map<std::vector<String>, CLICommand::Ptr>::value_type CLIKeyValue;
|
|
|
|
|
|
|
|
std::vector<String> best_match;
|
2015-01-13 14:05:28 +01:00
|
|
|
int arg_end = 0;
|
2014-10-06 14:21:18 +02:00
|
|
|
|
2014-10-20 13:58:21 +02:00
|
|
|
BOOST_FOREACH(const CLIKeyValue& kv, GetRegistry()) {
|
2014-10-06 14:21:18 +02:00
|
|
|
const std::vector<String>& vname = kv.first;
|
|
|
|
|
|
|
|
for (int i = 0, k = 1; i < vname.size() && k < argc; i++, k++) {
|
2014-10-21 16:07:22 +02:00
|
|
|
if (strcmp(argv[k], "--no-stack-rlimit") == 0 || strcmp(argv[k], "--autocomplete") == 0 || strcmp(argv[k], "--scm") == 0) {
|
2014-10-06 14:21:18 +02:00
|
|
|
i--;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (vname[i] != argv[k])
|
|
|
|
break;
|
2014-10-13 18:07:52 +02:00
|
|
|
|
2014-10-06 14:21:18 +02:00
|
|
|
if (i >= best_match.size())
|
|
|
|
best_match.push_back(vname[i]);
|
2014-10-13 18:07:52 +02:00
|
|
|
|
2014-10-06 14:21:18 +02:00
|
|
|
if (i == vname.size() - 1) {
|
|
|
|
cmdname = boost::algorithm::join(vname, " ");
|
|
|
|
command = kv.second;
|
|
|
|
arg_end = k;
|
|
|
|
goto found_command;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-10-13 18:07:52 +02:00
|
|
|
|
2014-10-06 14:21:18 +02:00
|
|
|
found_command:
|
|
|
|
lock.unlock();
|
|
|
|
|
2014-10-10 11:08:24 +02:00
|
|
|
po::options_description vdesc("Command options");
|
2014-10-13 18:07:52 +02:00
|
|
|
|
|
|
|
if (command)
|
2014-10-17 15:54:46 +02:00
|
|
|
command->InitParameters(vdesc, hiddenDesc);
|
2014-10-10 10:07:56 +02:00
|
|
|
|
2014-10-10 11:08:24 +02:00
|
|
|
visibleDesc.add(vdesc);
|
2014-10-10 10:07:56 +02:00
|
|
|
|
|
|
|
if (autocomplete)
|
|
|
|
return true;
|
|
|
|
|
2014-10-10 11:08:24 +02:00
|
|
|
po::options_description adesc;
|
|
|
|
adesc.add(visibleDesc);
|
|
|
|
adesc.add(hiddenDesc);
|
|
|
|
|
2014-10-14 14:27:37 +02:00
|
|
|
po::store(po::command_line_parser(argc - arg_end, argv + arg_end).options(adesc).positional(positionalDesc).run(), vm);
|
2014-10-06 14:21:18 +02:00
|
|
|
po::notify(vm);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-10-10 11:08:24 +02:00
|
|
|
void CLICommand::ShowCommands(int argc, char **argv, po::options_description *visibleDesc,
|
2014-10-17 15:54:46 +02:00
|
|
|
po::options_description *hiddenDesc,
|
|
|
|
ArgumentCompletionCallback globalArgCompletionCallback,
|
2014-10-14 16:45:00 +02:00
|
|
|
bool autocomplete, int autoindex)
|
2014-10-06 14:21:18 +02:00
|
|
|
{
|
2014-10-20 13:58:21 +02:00
|
|
|
boost::mutex::scoped_lock lock(GetRegistryMutex());
|
2014-10-06 14:21:18 +02:00
|
|
|
|
|
|
|
typedef std::map<std::vector<String>, CLICommand::Ptr>::value_type CLIKeyValue;
|
|
|
|
|
|
|
|
std::vector<String> best_match;
|
|
|
|
int arg_begin = 0;
|
|
|
|
CLICommand::Ptr command;
|
|
|
|
|
2014-10-20 13:58:21 +02:00
|
|
|
BOOST_FOREACH(const CLIKeyValue& kv, GetRegistry()) {
|
2014-10-06 14:21:18 +02:00
|
|
|
const std::vector<String>& vname = kv.first;
|
|
|
|
|
|
|
|
arg_begin = 0;
|
2014-10-13 18:07:52 +02:00
|
|
|
|
2014-10-06 14:21:18 +02:00
|
|
|
for (int i = 0, k = 1; i < vname.size() && k < argc; i++, k++) {
|
2014-10-21 16:07:22 +02:00
|
|
|
if (strcmp(argv[k], "--no-stack-rlimit") == 0 || strcmp(argv[k], "--autocomplete") == 0 || strcmp(argv[k], "--scm") == 0) {
|
2014-10-06 14:21:18 +02:00
|
|
|
i--;
|
|
|
|
arg_begin++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-10-22 08:16:56 +02:00
|
|
|
if (autocomplete && i >= autoindex - 1)
|
|
|
|
break;
|
|
|
|
|
2014-10-06 14:21:18 +02:00
|
|
|
if (vname[i] != argv[k])
|
|
|
|
break;
|
2014-10-13 18:07:52 +02:00
|
|
|
|
2014-10-06 14:21:18 +02:00
|
|
|
if (i >= best_match.size()) {
|
|
|
|
best_match.push_back(vname[i]);
|
|
|
|
}
|
2014-10-13 18:07:52 +02:00
|
|
|
|
2014-10-06 14:21:18 +02:00
|
|
|
if (i == vname.size() - 1) {
|
|
|
|
command = kv.second;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-10 10:07:56 +02:00
|
|
|
String aword;
|
|
|
|
|
|
|
|
if (autocomplete) {
|
|
|
|
if (autoindex < argc)
|
|
|
|
aword = argv[autoindex];
|
2014-10-15 08:32:46 +02:00
|
|
|
|
2014-10-15 09:41:54 +02:00
|
|
|
if (autoindex - 1 > best_match.size() && !command)
|
2014-10-15 08:32:46 +02:00
|
|
|
return;
|
2014-10-10 10:07:56 +02:00
|
|
|
} else
|
2014-10-06 14:21:18 +02:00
|
|
|
std::cout << "Supported commands: " << std::endl;
|
2014-10-10 10:07:56 +02:00
|
|
|
|
2014-10-20 13:58:21 +02:00
|
|
|
BOOST_FOREACH(const CLIKeyValue& kv, GetRegistry()) {
|
2014-10-06 14:21:18 +02:00
|
|
|
const std::vector<String>& vname = kv.first;
|
2014-10-10 10:07:56 +02:00
|
|
|
|
2014-11-17 18:42:22 +01:00
|
|
|
if (vname.size() < best_match.size() || kv.second->IsHidden())
|
2014-10-06 14:21:18 +02:00
|
|
|
continue;
|
2014-10-13 18:07:52 +02:00
|
|
|
|
2014-10-06 14:21:18 +02:00
|
|
|
bool match = true;
|
2014-10-13 18:07:52 +02:00
|
|
|
|
2014-10-06 14:21:18 +02:00
|
|
|
for (int i = 0; i < best_match.size(); i++) {
|
|
|
|
if (vname[i] != best_match[i]) {
|
|
|
|
match = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-10-13 18:07:52 +02:00
|
|
|
|
2014-10-06 14:21:18 +02:00
|
|
|
if (!match)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (autocomplete) {
|
2014-10-10 10:07:56 +02:00
|
|
|
String cname;
|
|
|
|
|
|
|
|
if (autoindex - 1 < vname.size()) {
|
|
|
|
cname = vname[autoindex - 1];
|
|
|
|
|
|
|
|
if (cname.Find(aword) == 0)
|
|
|
|
std::cout << cname << "\n";
|
2014-10-06 14:21:18 +02:00
|
|
|
}
|
|
|
|
} else
|
|
|
|
std::cout << " * " << boost::algorithm::join(vname, " ") << " (" << kv.second->GetShortDescription() << ")" << std::endl;
|
|
|
|
}
|
2014-10-10 10:07:56 +02:00
|
|
|
|
2014-10-20 14:13:57 +02:00
|
|
|
if (!autocomplete)
|
|
|
|
std::cout << std::endl;
|
|
|
|
|
2014-10-06 14:21:18 +02:00
|
|
|
if (command && autocomplete) {
|
2014-10-14 16:45:00 +02:00
|
|
|
String aname, prefix, pword;
|
|
|
|
const po::option_description *odesc;
|
|
|
|
|
|
|
|
if (autoindex - 2 >= 0 && strcmp(argv[autoindex - 1], "=") == 0 && strstr(argv[autoindex - 2], "--") == argv[autoindex - 2]) {
|
|
|
|
aname = argv[autoindex - 2] + 2;
|
|
|
|
pword = aword;
|
|
|
|
} else if (autoindex - 1 >= 0 && argv[autoindex - 1][0] == '-' && argv[autoindex - 1][1] == '-') {
|
|
|
|
aname = argv[autoindex - 1] + 2;
|
|
|
|
pword = aword;
|
|
|
|
|
|
|
|
if (pword == "=")
|
|
|
|
pword = "";
|
|
|
|
} else if (aword.GetLength() > 1 && aword[0] == '-' && aword[1] != '-') {
|
|
|
|
aname = aword.SubStr(1, 1);
|
|
|
|
prefix = aword.SubStr(0, 2);
|
|
|
|
pword = aword.SubStr(2);
|
|
|
|
} else {
|
|
|
|
goto complete_option;
|
|
|
|
}
|
2014-10-13 18:07:52 +02:00
|
|
|
|
2014-10-14 16:45:00 +02:00
|
|
|
odesc = visibleDesc->find_nothrow(aname, false);
|
|
|
|
|
|
|
|
if (!odesc)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (odesc->semantic()->min_tokens() == 0)
|
|
|
|
goto complete_option;
|
2014-10-17 15:54:46 +02:00
|
|
|
|
|
|
|
BOOST_FOREACH(const String& suggestion, globalArgCompletionCallback(aname, pword)) {
|
|
|
|
std::cout << prefix << suggestion << "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_FOREACH(const String& suggestion, command->GetArgumentSuggestions(aname, pword)) {
|
2014-10-14 16:45:00 +02:00
|
|
|
std::cout << prefix << suggestion << "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
2014-10-06 14:21:18 +02:00
|
|
|
|
2014-10-14 16:45:00 +02:00
|
|
|
complete_option:
|
2014-11-06 15:13:44 +01:00
|
|
|
BOOST_FOREACH(const boost::shared_ptr<po::option_description>& odesc, visibleDesc->options()) {
|
2014-10-06 14:21:18 +02:00
|
|
|
String cname = "--" + odesc->long_name();
|
2014-10-10 10:07:56 +02:00
|
|
|
|
|
|
|
if (cname.Find(aword) == 0)
|
|
|
|
std::cout << cname << "\n";
|
2014-10-06 14:21:18 +02:00
|
|
|
}
|
2014-10-17 15:54:46 +02:00
|
|
|
|
|
|
|
BOOST_FOREACH(const String& suggestion, command->GetPositionalSuggestions(aword)) {
|
|
|
|
std::cout << suggestion << "\n";
|
|
|
|
}
|
2014-10-06 14:21:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|