1 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
3 #include "cli/variablegetcommand.hpp"
4 #include "cli/variableutility.hpp"
5 #include "base/logger.hpp"
6 #include "base/application.hpp"
7 #include "base/convert.hpp"
8 #include "base/configobject.hpp"
9 #include "base/configtype.hpp"
10 #include "base/json.hpp"
11 #include "base/netstring.hpp"
12 #include "base/stdiostream.hpp"
13 #include "base/debug.hpp"
14 #include "base/objectlock.hpp"
15 #include "base/console.hpp"
16 #include "base/scriptglobal.hpp"
17 #include <boost/algorithm/string/join.hpp>
18 #include <boost/algorithm/string/replace.hpp>
22 using namespace icinga;
23 namespace po = boost::program_options;
25 REGISTER_CLICOMMAND("variable/get", VariableGetCommand);
27 String VariableGetCommand::GetDescription() const
29 return "Prints the value of an Icinga 2 variable.";
32 String VariableGetCommand::GetShortDescription() const
34 return "gets a variable";
37 void VariableGetCommand::InitParameters(boost::program_options::options_description& visibleDesc,
38 boost::program_options::options_description& hiddenDesc) const
40 visibleDesc.add_options()
41 ("current", "Uses the current value (i.e. from the running process, rather than from the vars file)");
44 int VariableGetCommand::GetMinArguments() const
50 * The entry point for the "variable get" CLI command.
52 * @returns An exit status.
54 int VariableGetCommand::Run(const boost::program_options::variables_map& vm, const std::vector<std::string>& ap) const
56 if (vm.count("current")) {
57 std::cout << ScriptGlobal::Get(ap[0], &Empty) << "\n";
61 String varsfile = Configuration::VarsPath;
63 if (!Utility::PathExists(varsfile)) {
64 Log(LogCritical, "cli")
65 << "Cannot open variables file '" << varsfile << "'.";
66 Log(LogCritical, "cli", "Run 'icinga2 daemon -C' to validate config and generate the cache file.");
70 Value value = VariableUtility::GetVariable(ap[0]);
72 std::cout << value << "\n";