Utils: Use Result instead expected_str<void> in some places

Change-Id: Iac691355ea51fc03d275606a2111074d898be58d
Reviewed-by: Marcus Tillmanns <marcus.tillmanns@qt.io>
This commit is contained in:
hjk
2024-10-09 15:31:26 +02:00
parent ab03449fb9
commit c54ab4d80d
3 changed files with 21 additions and 22 deletions

View File

@@ -162,7 +162,7 @@ CommandLine DeviceShell::createFallbackCommand(const CommandLine &cmd)
* *
* \note You have to call this function when deriving from DeviceShell. Current implementations call the function from their constructor. * \note You have to call this function when deriving from DeviceShell. Current implementations call the function from their constructor.
*/ */
expected_str<void> DeviceShell::start() Result DeviceShell::start()
{ {
m_shellProcess = std::make_unique<Process>(); m_shellProcess = std::make_unique<Process>();
connect(m_shellProcess.get(), &Process::done, m_shellProcess.get(), connect(m_shellProcess.get(), &Process::done, m_shellProcess.get(),
@@ -250,11 +250,11 @@ expected_str<QByteArray> DeviceShell::checkCommand(const QByteArray &command)
return out; return out;
} }
expected_str<void> DeviceShell::installShellScript() Result DeviceShell::installShellScript()
{ {
if (m_forceFailScriptInstallation) { if (m_forceFailScriptInstallation) {
m_shellScriptState = State::Failed; m_shellScriptState = State::Failed;
return make_unexpected(Tr::tr("Script installation was forced to fail.")); return Result::Error(Tr::tr("Script installation was forced to fail."));
} }
static const QList<QByteArray> requiredCommands static const QList<QByteArray> requiredCommands
@@ -263,7 +263,7 @@ expected_str<void> DeviceShell::installShellScript()
for (const QByteArray &command : requiredCommands) { for (const QByteArray &command : requiredCommands) {
auto checkResult = checkCommand(command); auto checkResult = checkCommand(command);
if (!checkResult) if (!checkResult)
return make_unexpected(checkResult.error()); return Result::Error(checkResult.error());
} }
const static QByteArray shellScriptBase64 = FilePath(":/utils/scripts/deviceshell.sh") const static QByteArray shellScriptBase64 = FilePath(":/utils/scripts/deviceshell.sh")
@@ -279,18 +279,17 @@ expected_str<void> DeviceShell::installShellScript()
m_shellProcess->writeRaw(scriptCmd); m_shellProcess->writeRaw(scriptCmd);
while (m_shellScriptState == State::Unknown) { while (m_shellScriptState == State::Unknown) {
if (!m_shellProcess->waitForReadyRead(5s)) { if (!m_shellProcess->waitForReadyRead(5s))
return make_unexpected(Tr::tr("Timeout while waiting for shell script installation.")); return Result::Error(Tr::tr("Timeout while waiting for shell script installation."));
}
QByteArray out = m_shellProcess->readAllRawStandardError(); QByteArray out = m_shellProcess->readAllRawStandardError();
if (out.contains("SCRIPT_INSTALLED") && !out.contains("ERROR_INSTALL_SCRIPT")) { if (out.contains("SCRIPT_INSTALLED") && !out.contains("ERROR_INSTALL_SCRIPT")) {
m_shellScriptState = State::Succeeded; m_shellScriptState = State::Succeeded;
return {}; return Result::Ok;
} }
if (out.contains("ERROR_INSTALL_SCRIPT")) { if (out.contains("ERROR_INSTALL_SCRIPT")) {
m_shellScriptState = State::Failed; m_shellScriptState = State::Failed;
return make_unexpected( return Result::Error(
Tr::tr("Failed to install shell script: %1").arg(QString::fromUtf8(out))); Tr::tr("Failed to install shell script: %1").arg(QString::fromUtf8(out)));
} }
if (!out.isEmpty()) { if (!out.isEmpty()) {
@@ -299,7 +298,7 @@ expected_str<void> DeviceShell::installShellScript()
} }
} }
return {}; return Result::Ok;
} }
void DeviceShell::closeShellProcess() void DeviceShell::closeShellProcess()

View File

@@ -38,7 +38,7 @@ public:
DeviceShell(bool forceFailScriptInstallation = false); DeviceShell(bool forceFailScriptInstallation = false);
virtual ~DeviceShell(); virtual ~DeviceShell();
expected_str<void> start(); Result start();
RunResult runInShell(const CommandLine &cmd, const QByteArray &stdInData = {}); RunResult runInShell(const CommandLine &cmd, const QByteArray &stdInData = {});
@@ -58,7 +58,7 @@ private:
virtual void setupShellProcess(Process *shellProcess); virtual void setupShellProcess(Process *shellProcess);
virtual CommandLine createFallbackCommand(const CommandLine &cmdLine); virtual CommandLine createFallbackCommand(const CommandLine &cmdLine);
expected_str<void> installShellScript(); Result installShellScript();
void closeShellProcess(); void closeShellProcess();
void onReadyRead(); void onReadyRead();

View File

@@ -311,7 +311,7 @@ public:
explicit LinuxDevicePrivate(LinuxDevice *parent); explicit LinuxDevicePrivate(LinuxDevice *parent);
~LinuxDevicePrivate(); ~LinuxDevicePrivate();
bool setupShell(const SshParameters &sshParameters, bool announce); Result setupShell(const SshParameters &sshParameters, bool announce);
RunResult runInShell(const CommandLine &cmd, const QByteArray &stdInData = {}); RunResult runInShell(const CommandLine &cmd, const QByteArray &stdInData = {});
void announceConnectionAttempt(); void announceConnectionAttempt();
void unannounceConnectionAttempt(); void unannounceConnectionAttempt();
@@ -896,7 +896,7 @@ public:
} }
// Call me with shell mutex locked // Call me with shell mutex locked
bool start(const SshParameters &parameters) Result start(const SshParameters &parameters)
{ {
closeShell(); closeShell();
setSshParameters(parameters); setSshParameters(parameters);
@@ -913,12 +913,12 @@ public:
connect(m_shell.get(), &DeviceShell::done, this, [this] { connect(m_shell.get(), &DeviceShell::done, this, [this] {
closeShell(); closeShell();
}); });
auto result = m_shell->start(); Result result = m_shell->start();
if (!result) { if (!result) {
qCWarning(linuxDeviceLog) << "Failed to start shell for:" << parameters.userAtHostAndPort() qCWarning(linuxDeviceLog) << "Failed to start shell for:" << parameters.userAtHostAndPort()
<< ", " << result.error(); << ", " << result.error();
} }
return result.has_value(); return result;
} }
// Call me with shell mutex locked // Call me with shell mutex locked
@@ -1186,11 +1186,11 @@ void LinuxDevicePrivate::checkOsType()
} }
// Call me with shell mutex locked // Call me with shell mutex locked
bool LinuxDevicePrivate::setupShell(const SshParameters &sshParameters, bool announce) Result LinuxDevicePrivate::setupShell(const SshParameters &sshParameters, bool announce)
{ {
if (m_handler->isRunning(sshParameters)) { if (m_handler->isRunning(sshParameters)) {
setDisconnected(false); setDisconnected(false);
return true; return Result::Ok;
} }
invalidateEnvironmentCache(); invalidateEnvironmentCache();
@@ -1198,22 +1198,22 @@ bool LinuxDevicePrivate::setupShell(const SshParameters &sshParameters, bool ann
if (announce) if (announce)
announceConnectionAttempt(); announceConnectionAttempt();
bool ok = false; Result result = Result::Error("setupShell failed");
QMetaObject::invokeMethod(m_handler, [this, sshParameters] { QMetaObject::invokeMethod(m_handler, [this, sshParameters] {
return m_handler->start(sshParameters); return m_handler->start(sshParameters);
}, Qt::BlockingQueuedConnection, &ok); }, Qt::BlockingQueuedConnection, &result);
if (announce) if (announce)
unannounceConnectionAttempt(); unannounceConnectionAttempt();
if (ok) { if (result) {
setDisconnected(false); setDisconnected(false);
queryOsType([this](const CommandLine &cmd) { return m_handler->runInShell(cmd); }); queryOsType([this](const CommandLine &cmd) { return m_handler->runInShell(cmd); });
} else { } else {
setDisconnected(true); setDisconnected(true);
} }
return ok; return result;
} }
RunResult LinuxDevicePrivate::runInShell(const CommandLine &cmd, const QByteArray &data) RunResult LinuxDevicePrivate::runInShell(const CommandLine &cmd, const QByteArray &data)