Refactor duplicated code #4933
This commit is contained in:
parent
582629e968
commit
27f83e1801
|
@ -17,6 +17,8 @@
|
||||||
|
|
||||||
#include "CoreInterface.h"
|
#include "CoreInterface.h"
|
||||||
|
|
||||||
|
#include "CommandProcess.h"
|
||||||
|
|
||||||
#include <QCoreApplication>
|
#include <QCoreApplication>
|
||||||
#include <QProcess>
|
#include <QProcess>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
|
@ -83,33 +85,7 @@ QString CoreInterface::run(const QStringList& args, const QString& input)
|
||||||
QCoreApplication::applicationDirPath()
|
QCoreApplication::applicationDirPath()
|
||||||
+ "/" + kCoreBinary);
|
+ "/" + kCoreBinary);
|
||||||
|
|
||||||
QProcess process;
|
CommandProcess commandProcess(program, args, input);
|
||||||
process.setReadChannel(QProcess::StandardOutput);
|
return commandProcess.run();
|
||||||
process.start(program, args);
|
|
||||||
bool success = process.waitForStarted();
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue