]> granicus.if.org Git - icinga2/blob - lib/cli/replcommand.cpp
Implement more unit tests
[icinga2] / lib / cli / replcommand.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2014 Icinga Development Team (http://www.icinga.org)    *
4  *                                                                            *
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.                     *
9  *                                                                            *
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.                               *
14  *                                                                            *
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  ******************************************************************************/
19
20 #include "cli/replcommand.hpp"
21 #include "config/configcompiler.hpp"
22 #include "config/configcompilercontext.hpp"
23 #include "base/json.hpp"
24 #include <iostream>
25
26 using namespace icinga;
27 namespace po = boost::program_options;
28
29 REGISTER_CLICOMMAND("repl", ReplCommand);
30
31 String ReplCommand::GetDescription(void) const
32 {
33         return "Interprets Icinga script expressions.";
34 }
35
36 String ReplCommand::GetShortDescription(void) const
37 {
38         return "Icinga REPL console";
39 }
40
41 bool ReplCommand::IsHidden(void) const
42 {
43         return true;
44 }
45
46 void ReplCommand::InitParameters(boost::program_options::options_description& visibleDesc,
47     boost::program_options::options_description& hiddenDesc) const
48 {
49 }
50
51 /**
52  * The entry point for the "repl" CLI command.
53  *
54  * @returns An exit status.
55  */
56 int ReplCommand::Run(const po::variables_map& vm, const std::vector<std::string>& ap) const
57 {
58         VMFrame frame;
59
60         while (std::cin.good()) {
61                 std::cout << "=> ";
62
63                 std::string line;
64                 std::getline(std::cin, line);
65
66                 Expression *expr;
67
68                 try {
69                         ConfigCompilerContext::GetInstance()->Reset();
70
71                         expr = ConfigCompiler::CompileText("<console>", line);
72
73                         BOOST_FOREACH(const ConfigCompilerMessage& message, ConfigCompilerContext::GetInstance()->GetMessages()) {
74                                 std::cout << (message.Error ? "Error" : "Warning") << ": " << message.Text << "\n";
75                         }
76
77                         if (expr) {
78                                 Value result = expr->Evaluate(frame);
79                                 if (!result.IsObject() || result.IsObjectType<Array>() || result.IsObjectType<Dictionary>())
80                                         std::cout << JsonEncode(result) << "\n";
81                                 else
82                                         std::cout << result << "\n";
83                         }
84                 } catch (const ConfigError& ex) {
85                         std::cout << ex.what() << "\n";
86                 } catch (const std::exception& ex) {
87                         std::cout << "Error: " << DiagnosticInformation(ex) << "\n";
88                 }
89
90                 delete expr;
91         }
92 }