Merge pull request #5796 from Icinga/fix/console-error-reporting

Fix error reporting for 'icinga2 console -r'
This commit is contained in:
Gunnar Beutner 2017-11-24 13:03:11 +01:00 committed by GitHub
commit 972b8047d6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 8 deletions

View File

@ -264,25 +264,27 @@ int ConsoleCommand::Run(const po::variables_map& vm, const std::vector<std::stri
} }
} }
String commandFileName;
if (vm.count("eval")) if (vm.count("eval"))
command = vm["eval"].as<std::string>(); command = vm["eval"].as<std::string>();
else if (vm.count("file")) { else if (vm.count("file")) {
std::string fname = vm["file"].as<std::string>(); commandFileName = vm["file"].as<std::string>();
try { try {
std::ifstream fp(fname.c_str()); std::ifstream fp(commandFileName.CStr());
fp.exceptions(std::ifstream::failbit | std::ifstream::badbit); fp.exceptions(std::ifstream::failbit | std::ifstream::badbit);
command = String(std::istreambuf_iterator<char>(fp), std::istreambuf_iterator<char>()); command = String(std::istreambuf_iterator<char>(fp), std::istreambuf_iterator<char>());
} catch (const std::exception&) { } catch (const std::exception&) {
std::cerr << "Could not read file '" << fname << "'." << std::endl; std::cerr << "Could not read file '" << commandFileName << "'." << std::endl;
return EXIT_FAILURE; return EXIT_FAILURE;
} }
} }
return RunScriptConsole(scriptFrame, addr, session, command, syntaxOnly); return RunScriptConsole(scriptFrame, addr, session, command, commandFileName, syntaxOnly);
} }
int ConsoleCommand::RunScriptConsole(ScriptFrame& scriptFrame, const String& addr, const String& session, const String& commandOnce, bool syntaxOnly) int ConsoleCommand::RunScriptConsole(ScriptFrame& scriptFrame, const String& addr, const String& session, const String& commandOnce, const String& commandOnceFileName, bool syntaxOnly)
{ {
std::map<String, String> lines; std::map<String, String> lines;
int next_line = 1; int next_line = 1;
@ -329,7 +331,13 @@ int ConsoleCommand::RunScriptConsole(ScriptFrame& scriptFrame, const String& add
} }
while (std::cin.good()) { while (std::cin.good()) {
String fileName = "<" + Convert::ToString(next_line) + ">"; String fileName;
if (commandOnceFileName.IsEmpty())
fileName = "<" + Convert::ToString(next_line) + ">";
else
fileName = commandOnceFileName;
next_line++; next_line++;
bool continuation = false; bool continuation = false;
@ -448,7 +456,7 @@ incomplete:
DebugInfo di = ex.GetDebugInfo(); DebugInfo di = ex.GetDebugInfo();
if (lines.find(di.Path) != lines.end()) { if (commandOnceFileName.IsEmpty() && lines.find(di.Path) != lines.end()) {
String text = lines[di.Path]; String text = lines[di.Path];
std::vector<String> ulines; std::vector<String> ulines;

View File

@ -47,7 +47,8 @@ public:
virtual int Run(const boost::program_options::variables_map& vm, const std::vector<std::string>& ap) const override; virtual int Run(const boost::program_options::variables_map& vm, const std::vector<std::string>& ap) const override;
static int RunScriptConsole(ScriptFrame& scriptFrame, const String& addr = String(), static int RunScriptConsole(ScriptFrame& scriptFrame, const String& addr = String(),
const String& session = String(), const String& commandOnce = String(), bool syntaxOnly = false); const String& session = String(), const String& commandOnce = String(), const String& commandOnceFileName = String(),
bool syntaxOnly = false);
private: private:
mutable boost::mutex m_Mutex; mutable boost::mutex m_Mutex;