1 /******************************************************************************
3 * Copyright (C) 2012-2014 Icinga Development Team (http://www.icinga.org) *
5 * This program is free software; you can redistribute it and/or *
6 * modify it under the terms of the GNU General Public License *
7 * as published by the Free Software Foundation; either version 2 *
8 * of the License, or (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the Free Software Foundation *
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
18 ******************************************************************************/
20 #include "cli/replcommand.hpp"
21 #include "config/configcompiler.hpp"
22 #include "config/configcompilercontext.hpp"
23 #include "base/json.hpp"
26 using namespace icinga;
27 namespace po = boost::program_options;
29 REGISTER_CLICOMMAND("repl", ReplCommand);
31 String ReplCommand::GetDescription(void) const
33 return "Interprets Icinga script expressions.";
36 String ReplCommand::GetShortDescription(void) const
38 return "Icinga REPL console";
41 bool ReplCommand::IsHidden(void) const
46 void ReplCommand::InitParameters(boost::program_options::options_description& visibleDesc,
47 boost::program_options::options_description& hiddenDesc) const
52 * The entry point for the "repl" CLI command.
54 * @returns An exit status.
56 int ReplCommand::Run(const po::variables_map& vm, const std::vector<std::string>& ap) const
60 while (std::cin.good()) {
64 std::getline(std::cin, line);
69 ConfigCompilerContext::GetInstance()->Reset();
71 expr = ConfigCompiler::CompileText("<console>", line);
73 BOOST_FOREACH(const ConfigCompilerMessage& message, ConfigCompilerContext::GetInstance()->GetMessages()) {
74 std::cout << (message.Error ? "Error" : "Warning") << ": " << message.Text << "\n";
78 Value result = expr->Evaluate(frame);
79 if (!result.IsObject() || result.IsObjectType<Array>() || result.IsObjectType<Dictionary>())
80 std::cout << JsonEncode(result) << "\n";
82 std::cout << result << "\n";
84 } catch (const ConfigError& ex) {
85 std::cout << ex.what() << "\n";
86 } catch (const std::exception& ex) {
87 std::cout << "Error: " << DiagnosticInformation(ex) << "\n";