Refactor duplicated code #4933

This commit is contained in:
Jerry (Xinyu Hou) 2015-10-29 11:42:16 -07:00
parent 582629e968
commit 27f83e1801

View File

@ -17,6 +17,8 @@
#include "CoreInterface.h"
#include "CommandProcess.h"
#include <QCoreApplication>
#include <QProcess>
#include <stdexcept>
@ -83,33 +85,7 @@ QString CoreInterface::run(const QStringList& args, const QString& input)
QCoreApplication::applicationDirPath()
+ "/" + kCoreBinary);
QProcess process;
process.setReadChannel(QProcess::StandardOutput);
process.start(program, args);
bool success = process.waitForStarted();
CommandProcess commandProcess(program, args, input);
return commandProcess.run();
QString output, error;
if (success)
{
if (!input.isEmpty()) {
process.write(input.toStdString().c_str());
}
if (process.waitForFinished()) {
output = process.readAllStandardOutput().trimmed();
error = process.readAllStandardError().trimmed();
}
}
int code = process.exitCode();
if (!error.isEmpty() || !success || code != 0)
{
throw std::runtime_error(
QString("Code: %1\nError: %2")
.arg(process.exitCode())
.arg(error.isEmpty() ? "Unknown" : error)
.toStdString());
}
return output;
}