Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exit code and stderr/stdout handling for SystemCommandExecutor #388

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 19 additions & 20 deletions systemcommandexecutor.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,45 +12,44 @@ class SystemCommandExecutor : public QObject
public:
explicit SystemCommandExecutor(QObject *parent = nullptr) : QObject(parent) {}

Q_INVOKABLE QString executeCommand(const QString &command) {
Q_INVOKABLE int executeCommand(const QString &command) {
QProcess process;
QEventLoop loop;

process.setReadChannel(QProcess::StandardOutput);

auto conn1 = connect(&process, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished), &loop, &QEventLoop::quit);
auto conn2 = connect(&process, &QProcess::errorOccurred, &loop, &QEventLoop::quit);
auto conn3 = connect(&process, &QProcess::readyReadStandardOutput, [&]() {
emit processOutput(QString::fromUtf8(process.readAllStandardOutput()));
auto quitHandler = connect(&process, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished), &loop, &QEventLoop::quit);
auto errHandler = connect(&process, &QProcess::errorOccurred, &loop, &QEventLoop::quit);
auto stdOutHandler = connect(&process, &QProcess::readyReadStandardOutput, [&]() {
emit processStandardOutput(QString::fromUtf8(process.readAllStandardOutput()));
});
auto conn4 = connect(&process, &QProcess::readyReadStandardError, [&]() {
emit processOutput(QString::fromUtf8(process.readAllStandardError()));
auto stdErrHandler = connect(&process, &QProcess::readyReadStandardError, [&]() {
emit processStandardError(QString::fromUtf8(process.readAllStandardError()));
});

process.start("sh", QStringList() << "-c" << command);

if (!process.waitForStarted()) {
disconnect(conn1);
disconnect(conn2);
disconnect(conn3);
disconnect(conn4);
return "Failed to start process";
disconnect(quitHandler);
disconnect(errHandler);
disconnect(stdOutHandler);
disconnect(stdErrHandler);
return -1;
}

loop.exec();

disconnect(conn1);
disconnect(conn2);
disconnect(conn3);
disconnect(conn4);
disconnect(quitHandler);
disconnect(errHandler);
disconnect(stdOutHandler);
disconnect(stdErrHandler);

QString output = process.readAllStandardOutput();
QString errorOutput = process.readAllStandardError();
return output + errorOutput;
return process.exitCode();
}

signals:
void processOutput(QString);
void processStandardOutput(QString);
void processStandardError(QString);

};

Expand Down
Loading