2014-11-23 12:35:13 +01:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
2016-01-12 08:29:59 +01:00
|
|
|
* Copyright (C) 2012-2016 Icinga Development Team (https://www.icinga.org/) *
|
2014-11-23 12:35:13 +01: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. *
|
|
|
|
******************************************************************************/
|
|
|
|
|
2015-01-12 14:29:08 +01:00
|
|
|
#include "cli/consolecommand.hpp"
|
2014-11-23 12:35:13 +01:00
|
|
|
#include "config/configcompiler.hpp"
|
2015-11-02 16:35:21 +01:00
|
|
|
#include "remote/apiclient.hpp"
|
|
|
|
#include "remote/consolehandler.hpp"
|
|
|
|
#include "remote/url.hpp"
|
2015-10-26 11:05:24 +01:00
|
|
|
#include "base/configwriter.hpp"
|
2015-11-02 16:35:21 +01:00
|
|
|
#include "base/serializer.hpp"
|
2014-11-23 12:35:13 +01:00
|
|
|
#include "base/json.hpp"
|
2014-11-23 12:38:16 +01:00
|
|
|
#include "base/console.hpp"
|
2014-12-16 10:36:59 +01:00
|
|
|
#include "base/application.hpp"
|
2015-03-18 07:17:15 +01:00
|
|
|
#include "base/objectlock.hpp"
|
2014-12-16 19:28:46 +01:00
|
|
|
#include "base/unixsocket.hpp"
|
|
|
|
#include "base/utility.hpp"
|
|
|
|
#include "base/networkstream.hpp"
|
2014-11-30 23:32:13 +01:00
|
|
|
#include "base/exception.hpp"
|
2014-11-23 12:35:13 +01:00
|
|
|
#include <iostream>
|
2015-03-17 10:42:46 +01:00
|
|
|
#ifdef HAVE_EDITLINE
|
|
|
|
#include "cli/editline.hpp"
|
|
|
|
#endif /* HAVE_EDITLINE */
|
2014-12-16 12:22:02 +01:00
|
|
|
|
2014-11-23 12:35:13 +01:00
|
|
|
using namespace icinga;
|
|
|
|
namespace po = boost::program_options;
|
|
|
|
|
2015-08-11 07:12:49 +02:00
|
|
|
static ScriptFrame *l_ScriptFrame;
|
2015-11-02 16:35:21 +01:00
|
|
|
static ApiClient::Ptr l_ApiClient;
|
|
|
|
static String l_Session;
|
2015-03-18 07:17:15 +01:00
|
|
|
|
2015-01-12 14:29:08 +01:00
|
|
|
REGISTER_CLICOMMAND("console", ConsoleCommand);
|
2014-11-23 12:35:13 +01:00
|
|
|
|
2015-11-05 14:29:45 +01:00
|
|
|
INITIALIZE_ONCE(&ConsoleCommand::StaticInitialize);
|
|
|
|
|
|
|
|
void ConsoleCommand::BreakpointHandler(ScriptFrame& frame, ScriptError *ex, const DebugInfo& di)
|
|
|
|
{
|
|
|
|
static boost::mutex mutex;
|
|
|
|
boost::mutex::scoped_lock lock(mutex);
|
|
|
|
|
|
|
|
if (!Application::GetScriptDebuggerEnabled())
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (ex && ex->IsHandledByDebugger())
|
|
|
|
return;
|
|
|
|
|
2015-11-10 17:04:49 +01:00
|
|
|
std::cout << "Breakpoint encountered.\n";
|
2015-11-05 14:29:45 +01:00
|
|
|
|
|
|
|
if (ex) {
|
2015-11-10 17:04:49 +01:00
|
|
|
std::cout << "Exception: " << DiagnosticInformation(*ex) << "\n";
|
2015-11-05 14:29:45 +01:00
|
|
|
ex->SetHandledByDebugger(true);
|
2015-11-10 17:04:49 +01:00
|
|
|
} else
|
|
|
|
ShowCodeLocation(std::cout, di);
|
2015-11-05 14:29:45 +01:00
|
|
|
|
2015-11-07 10:22:55 +01:00
|
|
|
std::cout << "You can inspect expressions (such as variables) by entering them at the prompt.\n"
|
2015-11-09 11:08:04 +01:00
|
|
|
<< "To leave the debugger and continue the program use \"$continue\".\n";
|
2015-11-05 14:29:45 +01:00
|
|
|
|
2015-11-07 10:17:55 +01:00
|
|
|
#ifdef HAVE_EDITLINE
|
|
|
|
rl_completion_entry_function = ConsoleCommand::ConsoleCompleteHelper;
|
|
|
|
rl_completion_append_character = '\0';
|
|
|
|
#endif /* HAVE_EDITLINE */
|
|
|
|
|
2015-11-05 14:29:45 +01:00
|
|
|
ConsoleCommand::RunScriptConsole(frame);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConsoleCommand::StaticInitialize(void)
|
|
|
|
{
|
|
|
|
Expression::OnBreakpoint.connect(&ConsoleCommand::BreakpointHandler);
|
|
|
|
}
|
|
|
|
|
2015-01-12 14:29:08 +01:00
|
|
|
String ConsoleCommand::GetDescription(void) const
|
2014-11-23 12:35:13 +01:00
|
|
|
{
|
|
|
|
return "Interprets Icinga script expressions.";
|
|
|
|
}
|
|
|
|
|
2015-01-12 14:29:08 +01:00
|
|
|
String ConsoleCommand::GetShortDescription(void) const
|
2014-11-23 12:35:13 +01:00
|
|
|
{
|
2015-01-12 14:29:08 +01:00
|
|
|
return "Icinga console";
|
2014-11-23 12:35:13 +01:00
|
|
|
}
|
|
|
|
|
2015-01-12 14:29:08 +01:00
|
|
|
ImpersonationLevel ConsoleCommand::GetImpersonationLevel(void) const
|
2014-12-15 08:37:34 +01:00
|
|
|
{
|
|
|
|
return ImpersonateNone;
|
|
|
|
}
|
|
|
|
|
2015-01-12 14:29:08 +01:00
|
|
|
void ConsoleCommand::InitParameters(boost::program_options::options_description& visibleDesc,
|
2014-11-23 12:35:13 +01:00
|
|
|
boost::program_options::options_description& hiddenDesc) const
|
|
|
|
{
|
2014-12-16 19:28:46 +01:00
|
|
|
visibleDesc.add_options()
|
|
|
|
("connect,c", po::value<std::string>(), "connect to an Icinga 2 instance")
|
2015-11-02 16:35:21 +01:00
|
|
|
("eval,e", po::value<std::string>(), "evaluate expression and terminate")
|
2015-04-16 08:47:26 +02:00
|
|
|
("sandbox", "enable sandbox mode")
|
2014-12-16 19:28:46 +01:00
|
|
|
;
|
2014-11-23 12:35:13 +01:00
|
|
|
}
|
|
|
|
|
2015-03-18 07:17:15 +01:00
|
|
|
#ifdef HAVE_EDITLINE
|
2015-11-02 16:35:21 +01:00
|
|
|
char *ConsoleCommand::ConsoleCompleteHelper(const char *word, int state)
|
2015-03-18 07:17:15 +01:00
|
|
|
{
|
|
|
|
static std::vector<String> matches;
|
|
|
|
|
|
|
|
if (state == 0) {
|
2015-11-02 16:35:21 +01:00
|
|
|
if (!l_ApiClient)
|
|
|
|
matches = ConsoleHandler::GetAutocompletionSuggestions(word, *l_ScriptFrame);
|
|
|
|
else {
|
|
|
|
boost::mutex mutex;
|
|
|
|
boost::condition_variable cv;
|
|
|
|
bool ready = false;
|
|
|
|
Array::Ptr suggestions;
|
|
|
|
|
|
|
|
l_ApiClient->AutocompleteScript(l_Session, word, l_ScriptFrame->Sandboxed,
|
|
|
|
boost::bind(&ConsoleCommand::AutocompleteScriptCompletionHandler,
|
|
|
|
boost::ref(mutex), boost::ref(cv), boost::ref(ready),
|
|
|
|
_1, _2,
|
|
|
|
boost::ref(suggestions)));
|
|
|
|
|
|
|
|
{
|
|
|
|
boost::mutex::scoped_lock lock(mutex);
|
|
|
|
while (!ready)
|
|
|
|
cv.wait(lock);
|
2015-03-18 07:17:15 +01:00
|
|
|
}
|
2015-03-18 08:19:36 +01:00
|
|
|
|
2015-11-02 16:35:21 +01:00
|
|
|
matches.clear();
|
2015-03-18 08:19:36 +01:00
|
|
|
|
2015-11-02 16:35:21 +01:00
|
|
|
ObjectLock olock(suggestions);
|
|
|
|
std::copy(suggestions->Begin(), suggestions->End(), std::back_inserter(matches));
|
2015-03-18 08:10:32 +01:00
|
|
|
}
|
2015-03-18 07:17:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (state >= matches.size())
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return strdup(matches[state].CStr());
|
|
|
|
}
|
|
|
|
#endif /* HAVE_EDITLINE */
|
|
|
|
|
2014-11-23 12:35:13 +01:00
|
|
|
/**
|
2015-01-12 14:29:08 +01:00
|
|
|
* The entry point for the "console" CLI command.
|
2014-11-23 12:35:13 +01:00
|
|
|
*
|
|
|
|
* @returns An exit status.
|
|
|
|
*/
|
2015-01-12 14:29:08 +01:00
|
|
|
int ConsoleCommand::Run(const po::variables_map& vm, const std::vector<std::string>& ap) const
|
2014-11-23 12:35:13 +01:00
|
|
|
{
|
2015-03-18 07:17:15 +01:00
|
|
|
#ifdef HAVE_EDITLINE
|
2015-11-02 16:35:21 +01:00
|
|
|
rl_completion_entry_function = ConsoleCommand::ConsoleCompleteHelper;
|
2015-03-18 08:10:32 +01:00
|
|
|
rl_completion_append_character = '\0';
|
2015-03-18 07:17:15 +01:00
|
|
|
#endif /* HAVE_EDITLINE */
|
|
|
|
|
2015-11-05 14:29:45 +01:00
|
|
|
String addr, session;
|
2015-08-11 07:12:49 +02:00
|
|
|
ScriptFrame scriptFrame;
|
|
|
|
|
2015-11-05 14:29:45 +01:00
|
|
|
session = Utility::NewUniqueID();
|
2015-11-02 16:35:21 +01:00
|
|
|
|
|
|
|
if (vm.count("sandbox"))
|
|
|
|
scriptFrame.Sandboxed = true;
|
|
|
|
|
2015-11-05 10:29:02 +01:00
|
|
|
scriptFrame.Self = scriptFrame.Locals;
|
|
|
|
|
2015-11-02 16:35:21 +01:00
|
|
|
if (!vm.count("eval"))
|
|
|
|
std::cout << "Icinga 2 (version: " << Application::GetAppVersion() << ")\n";
|
|
|
|
|
|
|
|
const char *addrEnv = getenv("ICINGA2_API_URL");
|
|
|
|
if (addrEnv)
|
|
|
|
addr = addrEnv;
|
2014-12-16 19:28:46 +01:00
|
|
|
|
2015-11-05 14:29:45 +01:00
|
|
|
if (vm.count("connect"))
|
2014-12-16 19:28:46 +01:00
|
|
|
addr = vm["connect"].as<std::string>();
|
2015-11-05 14:29:45 +01:00
|
|
|
|
|
|
|
String command;
|
|
|
|
|
|
|
|
if (vm.count("eval"))
|
|
|
|
command = vm["eval"].as<std::string>();
|
|
|
|
|
|
|
|
return RunScriptConsole(scriptFrame, addr, session, command);;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ConsoleCommand::RunScriptConsole(ScriptFrame& scriptFrame, const String& addr, const String& session, const String& commandOnce)
|
|
|
|
{
|
|
|
|
std::map<String, String> lines;
|
|
|
|
int next_line = 1;
|
|
|
|
|
2016-07-29 13:45:16 +02:00
|
|
|
#ifdef HAVE_EDITLINE
|
|
|
|
String homeEnv = getenv("HOME");
|
|
|
|
String historyPath = homeEnv + "/.icinga2_history";
|
|
|
|
|
|
|
|
std::fstream historyfp;
|
|
|
|
historyfp.open(historyPath.CStr(), std::fstream::in);
|
|
|
|
|
|
|
|
String line;
|
|
|
|
while (std::getline(historyfp, line.GetData()))
|
|
|
|
add_history(line.CStr());
|
|
|
|
|
|
|
|
historyfp.close();
|
|
|
|
#endif /* HAVE_EDITLINE */
|
|
|
|
|
2015-11-05 14:29:45 +01:00
|
|
|
l_ScriptFrame = &scriptFrame;
|
|
|
|
l_Session = session;
|
2014-12-16 19:28:46 +01:00
|
|
|
|
2015-11-02 16:35:21 +01:00
|
|
|
if (!addr.IsEmpty()) {
|
|
|
|
Url::Ptr url;
|
|
|
|
|
|
|
|
try {
|
|
|
|
url = new Url(addr);
|
|
|
|
} catch (const std::exception& ex) {
|
|
|
|
Log(LogCritical, "ConsoleCommand", ex.what());
|
2015-04-16 08:47:26 +02:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
2015-11-02 16:35:21 +01:00
|
|
|
const char *usernameEnv = getenv("ICINGA2_API_USERNAME");
|
|
|
|
const char *passwordEnv = getenv("ICINGA2_API_PASSWORD");
|
2015-04-16 08:47:26 +02:00
|
|
|
|
2015-11-02 16:35:21 +01:00
|
|
|
if (usernameEnv)
|
|
|
|
url->SetUsername(usernameEnv);
|
|
|
|
if (passwordEnv)
|
|
|
|
url->SetPassword(passwordEnv);
|
|
|
|
|
|
|
|
if (url->GetPort().IsEmpty())
|
|
|
|
url->SetPort("5665");
|
|
|
|
|
|
|
|
l_ApiClient = new ApiClient(url->GetHost(), url->GetPort(), url->GetUsername(), url->GetPassword());
|
|
|
|
}
|
2014-12-16 10:36:59 +01:00
|
|
|
|
2014-11-23 12:35:13 +01:00
|
|
|
while (std::cin.good()) {
|
2014-12-13 21:16:55 +01:00
|
|
|
String fileName = "<" + Convert::ToString(next_line) + ">";
|
|
|
|
next_line++;
|
|
|
|
|
2015-02-10 13:27:02 +01:00
|
|
|
bool continuation = false;
|
|
|
|
std::string command;
|
|
|
|
|
|
|
|
incomplete:
|
2015-11-02 16:35:21 +01:00
|
|
|
std::string line;
|
|
|
|
|
2015-11-05 14:29:45 +01:00
|
|
|
if (commandOnce.IsEmpty()) {
|
2015-03-17 10:42:46 +01:00
|
|
|
#ifdef HAVE_EDITLINE
|
2015-11-02 16:35:21 +01:00
|
|
|
std::ostringstream promptbuf;
|
|
|
|
std::ostream& os = promptbuf;
|
2015-03-17 10:42:46 +01:00
|
|
|
#else /* HAVE_EDITLINE */
|
2015-11-02 16:35:21 +01:00
|
|
|
std::ostream& os = std::cout;
|
2015-03-17 10:42:46 +01:00
|
|
|
#endif /* HAVE_EDITLINE */
|
|
|
|
|
2015-11-02 16:35:21 +01:00
|
|
|
os << fileName;
|
2015-02-10 13:27:02 +01:00
|
|
|
|
2015-11-02 16:35:21 +01:00
|
|
|
if (!continuation)
|
|
|
|
os << " => ";
|
|
|
|
else
|
|
|
|
os << " .. ";
|
2015-03-17 10:42:46 +01:00
|
|
|
|
|
|
|
#ifdef HAVE_EDITLINE
|
2015-11-02 16:35:21 +01:00
|
|
|
String prompt = promptbuf.str();
|
2015-03-17 10:42:46 +01:00
|
|
|
|
2015-11-02 16:35:21 +01:00
|
|
|
char *cline;
|
|
|
|
cline = readline(prompt.CStr());
|
2015-03-17 10:42:46 +01:00
|
|
|
|
2015-11-02 16:35:21 +01:00
|
|
|
if (!cline)
|
|
|
|
break;
|
2015-03-17 10:42:46 +01:00
|
|
|
|
2016-08-08 08:01:52 +02:00
|
|
|
if (commandOnce.IsEmpty() && cline[0] != '\0') {
|
2016-07-29 13:45:16 +02:00
|
|
|
add_history(cline);
|
|
|
|
|
2016-08-08 08:01:52 +02:00
|
|
|
historyfp.open(historyPath.CStr(), std::fstream::out | std::fstream::app);
|
|
|
|
historyfp << cline << "\n";
|
|
|
|
historyfp.close();
|
2016-07-29 13:45:16 +02:00
|
|
|
}
|
2015-02-10 13:27:02 +01:00
|
|
|
|
2015-11-02 16:35:21 +01:00
|
|
|
line = cline;
|
2014-11-23 12:35:13 +01:00
|
|
|
|
2015-11-02 16:35:21 +01:00
|
|
|
free(cline);
|
2015-03-17 10:42:46 +01:00
|
|
|
#else /* HAVE_EDITLINE */
|
2015-11-02 16:35:21 +01:00
|
|
|
std::getline(std::cin, line);
|
2015-03-17 10:42:46 +01:00
|
|
|
#endif /* HAVE_EDITLINE */
|
2015-11-02 16:35:21 +01:00
|
|
|
} else
|
2015-11-05 14:29:45 +01:00
|
|
|
line = commandOnce;
|
|
|
|
|
|
|
|
if (!line.empty() && line[0] == '$') {
|
2015-11-09 11:08:04 +01:00
|
|
|
if (line == "$continue")
|
2015-11-05 14:29:45 +01:00
|
|
|
break;
|
|
|
|
|
2015-11-07 10:22:55 +01:00
|
|
|
std::cout << "Unknown debugger command: " << line << "\n";
|
|
|
|
continue;
|
2015-11-05 14:29:45 +01:00
|
|
|
}
|
2014-11-23 12:35:13 +01:00
|
|
|
|
2015-02-10 13:27:02 +01:00
|
|
|
if (!command.empty())
|
|
|
|
command += "\n";
|
|
|
|
|
|
|
|
command += line;
|
|
|
|
|
2015-11-02 16:35:21 +01:00
|
|
|
Expression *expr = NULL;
|
2014-12-16 19:28:46 +01:00
|
|
|
|
2015-11-02 16:35:21 +01:00
|
|
|
try {
|
|
|
|
lines[fileName] = command;
|
2014-11-23 12:35:13 +01:00
|
|
|
|
2015-11-02 16:35:21 +01:00
|
|
|
Value result;
|
2014-12-13 21:16:55 +01:00
|
|
|
|
2015-11-02 16:35:21 +01:00
|
|
|
if (!l_ApiClient) {
|
|
|
|
expr = ConfigCompiler::CompileText(fileName, command);
|
|
|
|
result = Serialize(expr->Evaluate(scriptFrame), 0);
|
|
|
|
} else {
|
|
|
|
boost::mutex mutex;
|
|
|
|
boost::condition_variable cv;
|
|
|
|
bool ready = false;
|
|
|
|
boost::exception_ptr eptr;
|
|
|
|
|
|
|
|
l_ApiClient->ExecuteScript(l_Session, command, scriptFrame.Sandboxed,
|
|
|
|
boost::bind(&ConsoleCommand::ExecuteScriptCompletionHandler,
|
|
|
|
boost::ref(mutex), boost::ref(cv), boost::ref(ready),
|
|
|
|
_1, _2,
|
|
|
|
boost::ref(result), boost::ref(eptr)));
|
|
|
|
|
|
|
|
{
|
|
|
|
boost::mutex::scoped_lock lock(mutex);
|
|
|
|
while (!ready)
|
|
|
|
cv.wait(lock);
|
2015-02-10 13:27:02 +01:00
|
|
|
}
|
|
|
|
|
2015-11-02 16:35:21 +01:00
|
|
|
if (eptr)
|
|
|
|
boost::rethrow_exception(eptr);
|
|
|
|
}
|
2014-12-16 19:28:46 +01:00
|
|
|
|
2015-11-05 14:29:45 +01:00
|
|
|
if (commandOnce.IsEmpty()) {
|
2015-11-02 16:35:21 +01:00
|
|
|
std::cout << ConsoleColorTag(Console_ForegroundCyan);
|
|
|
|
ConfigWriter::EmitValue(std::cout, 1, result);
|
|
|
|
std::cout << ConsoleColorTag(Console_Normal) << "\n";
|
|
|
|
} else {
|
|
|
|
std::cout << JsonEncode(result) << "\n";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} catch (const ScriptError& ex) {
|
|
|
|
if (ex.IsIncompleteExpression()) {
|
|
|
|
continuation = true;
|
|
|
|
goto incomplete;
|
|
|
|
}
|
2015-02-10 13:58:35 +01:00
|
|
|
|
2015-11-02 16:35:21 +01:00
|
|
|
DebugInfo di = ex.GetDebugInfo();
|
2015-02-10 13:58:35 +01:00
|
|
|
|
2015-11-02 16:35:21 +01:00
|
|
|
if (lines.find(di.Path) != lines.end()) {
|
|
|
|
String text = lines[di.Path];
|
2015-02-10 13:58:35 +01:00
|
|
|
|
2015-11-02 16:35:21 +01:00
|
|
|
std::vector<String> ulines;
|
|
|
|
boost::algorithm::split(ulines, text, boost::is_any_of("\n"));
|
2015-02-10 13:58:35 +01:00
|
|
|
|
2015-11-02 16:35:21 +01:00
|
|
|
for (int i = 1; i <= ulines.size(); i++) {
|
|
|
|
int start, len;
|
2015-02-10 13:58:35 +01:00
|
|
|
|
2015-11-02 16:35:21 +01:00
|
|
|
if (i == di.FirstLine)
|
|
|
|
start = di.FirstColumn;
|
|
|
|
else
|
|
|
|
start = 0;
|
2015-02-10 13:58:35 +01:00
|
|
|
|
2015-11-02 16:35:21 +01:00
|
|
|
if (i == di.LastLine)
|
|
|
|
len = di.LastColumn - di.FirstColumn + 1;
|
|
|
|
else
|
|
|
|
len = ulines[i - 1].GetLength();
|
|
|
|
|
|
|
|
int offset;
|
2015-02-10 13:58:35 +01:00
|
|
|
|
2015-11-02 16:35:21 +01:00
|
|
|
if (di.Path != fileName) {
|
|
|
|
std::cout << di.Path << ": " << ulines[i - 1] << "\n";
|
|
|
|
offset = 2;
|
|
|
|
} else
|
|
|
|
offset = 4;
|
|
|
|
|
|
|
|
if (i >= di.FirstLine && i <= di.LastLine) {
|
|
|
|
std::cout << String(di.Path.GetLength() + offset, ' ');
|
|
|
|
std::cout << String(start, ' ') << String(len, '^') << "\n";
|
2015-02-10 13:58:35 +01:00
|
|
|
}
|
2015-01-16 10:07:11 +01:00
|
|
|
}
|
2015-11-02 16:35:21 +01:00
|
|
|
} else {
|
2015-11-10 17:04:49 +01:00
|
|
|
ShowCodeLocation(std::cout, di);
|
2014-11-23 12:35:13 +01:00
|
|
|
}
|
|
|
|
|
2015-11-02 16:35:21 +01:00
|
|
|
std::cout << ex.what() << "\n";
|
2014-12-16 19:28:46 +01:00
|
|
|
|
2015-11-05 14:29:45 +01:00
|
|
|
if (!commandOnce.IsEmpty())
|
2015-11-02 16:35:21 +01:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
} catch (const std::exception& ex) {
|
|
|
|
std::cout << "Error: " << DiagnosticInformation(ex) << "\n";
|
2014-11-28 06:28:07 +01:00
|
|
|
|
2015-11-05 14:29:45 +01:00
|
|
|
if (!commandOnce.IsEmpty())
|
2015-11-02 16:35:21 +01:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
2014-11-28 06:28:07 +01:00
|
|
|
|
2015-11-02 16:35:21 +01:00
|
|
|
delete expr;
|
|
|
|
}
|
2014-11-23 12:35:13 +01:00
|
|
|
|
2015-11-02 16:35:21 +01:00
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|
2014-12-16 19:28:46 +01:00
|
|
|
|
2015-11-02 16:35:21 +01:00
|
|
|
void ConsoleCommand::ExecuteScriptCompletionHandler(boost::mutex& mutex, boost::condition_variable& cv,
|
|
|
|
bool& ready, boost::exception_ptr eptr, const Value& result, Value& resultOut, boost::exception_ptr& eptrOut)
|
|
|
|
{
|
|
|
|
if (eptr) {
|
|
|
|
try {
|
|
|
|
boost::rethrow_exception(eptr);
|
|
|
|
} catch (const ScriptError& ex) {
|
|
|
|
eptrOut = boost::current_exception();
|
|
|
|
} catch (const std::exception& ex) {
|
|
|
|
Log(LogCritical, "ConsoleCommand")
|
|
|
|
<< "HTTP query failed: " << ex.what();
|
|
|
|
Application::Exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
2014-12-16 19:28:46 +01:00
|
|
|
|
2015-11-02 16:35:21 +01:00
|
|
|
resultOut = result;
|
|
|
|
|
|
|
|
{
|
|
|
|
boost::mutex::scoped_lock lock(mutex);
|
|
|
|
ready = true;
|
|
|
|
cv.notify_all();
|
|
|
|
}
|
|
|
|
}
|
2014-12-16 19:28:46 +01:00
|
|
|
|
2015-11-02 16:35:21 +01:00
|
|
|
void ConsoleCommand::AutocompleteScriptCompletionHandler(boost::mutex& mutex, boost::condition_variable& cv,
|
|
|
|
bool& ready, boost::exception_ptr eptr, const Array::Ptr& result, Array::Ptr& resultOut)
|
|
|
|
{
|
|
|
|
if (eptr) {
|
|
|
|
try {
|
|
|
|
boost::rethrow_exception(eptr);
|
|
|
|
} catch (const std::exception& ex) {
|
|
|
|
Log(LogCritical, "ConsoleCommand")
|
|
|
|
<< "HTTP query failed: " << ex.what();
|
|
|
|
Application::Exit(EXIT_FAILURE);
|
2014-12-16 19:28:46 +01:00
|
|
|
}
|
2014-11-23 12:35:13 +01:00
|
|
|
}
|
2014-11-23 13:41:45 +01:00
|
|
|
|
2015-11-02 16:35:21 +01:00
|
|
|
resultOut = result;
|
|
|
|
|
|
|
|
{
|
|
|
|
boost::mutex::scoped_lock lock(mutex);
|
|
|
|
ready = true;
|
|
|
|
cv.notify_all();
|
|
|
|
}
|
2014-11-23 12:35:13 +01:00
|
|
|
}
|