* The entry point for the "console" CLI command. * * @returns An exit status. */
| 217 | * @returns An exit status. |
| 218 | */ |
| 219 | int ConsoleCommand::Run(const po::variables_map& vm, [[maybe_unused]] const std::vector<std::string>& ap) const |
| 220 | { |
| 221 | #ifdef HAVE_EDITLINE |
| 222 | rl_completion_entry_function = ConsoleCommand::ConsoleCompleteHelper; |
| 223 | rl_completion_append_character = '\0'; |
| 224 | #endif /* HAVE_EDITLINE */ |
| 225 | |
| 226 | String addr, session; |
| 227 | ScriptFrame scriptFrame(true); |
| 228 | |
| 229 | session = Utility::NewUniqueID(); |
| 230 | |
| 231 | if (vm.count("sandbox")) |
| 232 | scriptFrame.Sandboxed = true; |
| 233 | |
| 234 | scriptFrame.Self = scriptFrame.Locals; |
| 235 | |
| 236 | if (!vm.count("eval") && !vm.count("file")) |
| 237 | std::cout << "Icinga 2 (version: " << Application::GetAppVersion() << ")\n" |
| 238 | << "Type $help to view available commands.\n"; |
| 239 | |
| 240 | String addrEnv = Utility::GetFromEnvironment("ICINGA2_API_URL"); |
| 241 | if (!addrEnv.IsEmpty()) |
| 242 | addr = addrEnv; |
| 243 | |
| 244 | /* Initialize remote connect parameters. */ |
| 245 | if (vm.count("connect")) { |
| 246 | addr = vm["connect"].as<std::string>(); |
| 247 | |
| 248 | try { |
| 249 | l_Url = new Url(addr); |
| 250 | } catch (const std::exception& ex) { |
| 251 | Log(LogCritical, "ConsoleCommand", ex.what()); |
| 252 | return EXIT_FAILURE; |
| 253 | } |
| 254 | |
| 255 | String usernameEnv = Utility::GetFromEnvironment("ICINGA2_API_USERNAME"); |
| 256 | String passwordEnv = Utility::GetFromEnvironment("ICINGA2_API_PASSWORD"); |
| 257 | |
| 258 | if (!usernameEnv.IsEmpty()) |
| 259 | l_Url->SetUsername(usernameEnv); |
| 260 | if (!passwordEnv.IsEmpty()) |
| 261 | l_Url->SetPassword(passwordEnv); |
| 262 | |
| 263 | if (l_Url->GetPort().IsEmpty()) |
| 264 | l_Url->SetPort("5665"); |
| 265 | |
| 266 | /* User passed --connect and wants to run the expression via REST API. |
| 267 | * Evaluate this now before any user input happens. |
| 268 | */ |
| 269 | try { |
| 270 | l_TlsStream = ConsoleCommand::Connect(); |
| 271 | } catch (const std::exception& ex) { |
| 272 | return EXIT_FAILURE; |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | String command; |