* @brief Spawns a helper process (e.g. a Python script) tied to the connection lifecycle. */
| 59 | * @brief Spawns a helper process (e.g. a Python script) tied to the connection lifecycle. |
| 60 | */ |
| 61 | API::CommandResponse API::Handlers::SystemHandler::exec(const QString& id, |
| 62 | const QJsonObject& params) |
| 63 | { |
| 64 | const auto program = params.value(QStringLiteral("program")).toString(); |
| 65 | if (program.isEmpty()) |
| 66 | return CommandResponse::makeError( |
| 67 | id, ErrorCode::MissingParam, QStringLiteral("Missing 'program'")); |
| 68 | |
| 69 | QStringList arguments; |
| 70 | for (const auto& arg : params.value(QStringLiteral("args")).toArray()) |
| 71 | arguments.append(arg.toString()); |
| 72 | |
| 73 | auto workingDir = params.value(QStringLiteral("workingDir")).toString(); |
| 74 | if (workingDir.isEmpty()) { |
| 75 | const auto& projectPath = DataModel::ProjectModel::instance().jsonFilePath(); |
| 76 | if (!projectPath.isEmpty()) |
| 77 | workingDir = QFileInfo(projectPath).absolutePath(); |
| 78 | } |
| 79 | |
| 80 | QString error; |
| 81 | const int processId = ProcessLauncher::instance().launch(program, arguments, workingDir, error); |
| 82 | if (processId < 0) |
| 83 | return CommandResponse::makeError(id, ErrorCode::OperationFailed, error); |
| 84 | |
| 85 | QJsonObject result; |
| 86 | result[QStringLiteral("processId")] = processId; |
| 87 | return CommandResponse::makeSuccess(id, result); |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * @brief Terminates a single managed helper process by id. |
no test coverage detected