File indexing completed on 2024-12-22 04:48:18
0001 // SPDX-License-Identifier: GPL-2.0-or-later 0002 // SPDX-FileCopyrightText: 2024 Louis Schul <schul9louis@gmail.com> 0003 0004 #include "cliHelper.h" 0005 0006 #include <QProcess> 0007 #include <QStandardPaths> 0008 0009 QString CLIHelper::findExecutable(const QString &command) 0010 { 0011 return QStandardPaths::findExecutable(command); 0012 } 0013 0014 QString CLIHelper::execCommand(const QString &input) 0015 { 0016 QProcess process; 0017 0018 const QString sh = findExecutable(QStringLiteral("sh")); 0019 if (sh.isEmpty()) { 0020 return {}; 0021 } 0022 0023 // We let sh handle the pipes and all the args, easier than using multiple QProcess 0024 process.start(sh, QStringList() << QStringLiteral("-c") << input); 0025 0026 if (!process.waitForStarted()) { 0027 return {}; 0028 } 0029 0030 process.waitForFinished(5000); // if it takes more then 5 secs, there's a problem ! 0031 0032 return process.exitCode() == 0 ? QString::fromUtf8(process.readAllStandardOutput()) : QLatin1String(); 0033 } 0034 0035 bool CLIHelper::commandExists(const QString &command) 0036 { 0037 return !findExecutable(command).isEmpty(); 0038 }