command-class as abstraction for executing git-commands.

This commit is contained in:
Gerhard Hoffmann 2023-07-10 15:57:59 +02:00
parent cbefccd2d3
commit b979fb5b2a
2 changed files with 106 additions and 0 deletions

73
process/command.cpp Normal file
View File

@ -0,0 +1,73 @@
#include "command.h"
#include <QProcess>
#include <QDebug>
#include <QRegularExpression>
Command::Command(QString const &command, int start_timeout, int finish_timeout)
: m_command(command.trimmed())
, m_commandResult("")
, m_waitForStartTimeout(start_timeout)
, m_waitForFinishTimeout(finish_timeout) {
}
QString Command::getCommandResult() const {
return m_commandResult;
}
void Command::readyReadStandardOutput() {
QProcess *p = (QProcess *)sender();
m_commandResult += p->readAllStandardOutput();
// qCritical() << m_commandResult;
}
void Command::readyReadStandardError() {
QProcess *p = (QProcess *)sender();
QByteArray buf = p->readAllStandardError();
qCritical() << buf;
}
void Command::finished(int /*exitCode*/, QProcess::ExitStatus /*exitStatus*/) {
QProcess *p = (QProcess *)sender();
// read all remaining data sent to the process, just in case
QString d = p->readAllStandardOutput();
if (!d.isEmpty()) {
m_commandResult += d;
}
disconnect(p, SIGNAL(finished(int,QProcess::ExitStatus)), this, SLOT(readyReadStandardOutput()));
disconnect(p, SIGNAL(finished(int,QProcess::ExitStatus)), this, SLOT(readyReadStandardError()));
}
bool Command::execute(QString workingDirectory) {
QScopedPointer<QProcess> p(new QProcess(this));
p->setProcessChannelMode(QProcess::MergedChannels);
connect(&(*p), SIGNAL(readyReadStandardOutput()), this, SLOT(readyReadStandardOutput()));
connect(&(*p), SIGNAL(readyReadStandardError()), this, SLOT(readyReadStandardError()));
p->setWorkingDirectory(workingDirectory);
p->start(m_command);
if (p->waitForStarted(m_waitForStartTimeout)) {
if (p->state() == QProcess::ProcessState::Running) {
if (p->waitForFinished(m_waitForFinishTimeout)) {
if (p->exitStatus() == QProcess::NormalExit) {
qInfo() << "EXECUTED" << m_command
<< "with code" << p->exitCode();
// qInfo() << "RESULT" << m_commandResult;
return true;
} else {
qCritical() << "PROCESS" << m_command << "CRASHED with code"
<< p->exitCode();
}
} else {
qCritical() << "PROCESS" << m_command << "DID NOT FINISH";
}
} else {
qCritical() << "WRONG PROCESS STATE" << p->state();
}
} else {
qCritical() << "PROCESS" << m_command << "TIMEOUT AT START";
}
return false;
}

33
process/command.h Normal file
View File

@ -0,0 +1,33 @@
#ifndef COMMAND_H_INCLUDED
#define COMMAND_H_INCLUDED
#endif // COMMAND_H_INCLUDED
#include <QObject>
#include <QCoreApplication>
#include <QString>
#include <QStringList>
#include <QProcess>
class Command : public QObject {
Q_OBJECT
QString m_command;
QString m_commandResult;
int m_waitForStartTimeout;
int m_waitForFinishTimeout;
public:
explicit Command(QString const &command,
int start_timeout = 100000,
int finish_timeout = 100000);
QString getCommandResult() const;
bool execute(QString workingDirectory = QCoreApplication::applicationDirPath());
private slots:
void readyReadStandardOutput();
void readyReadStandardError();
void finished(int exitCode, QProcess::ExitStatus exitStatus);
};