From 640f385a50014cb9c7925020144943f79a1d9a69 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A2=D0=BE=D0=B2=D0=B0=D1=80=D0=B8=D1=89=20=D0=BF=D1=80?= =?UTF-8?q?=D0=BE=D0=B3=D1=80=D0=B0=D0=BC=D0=BC=D0=B8=D1=81=D1=82?= <2962928213@qq.com> Date: Mon, 26 Jun 2023 23:56:15 +0800 Subject: [PATCH] feat: implement service table --- src/commandrunner.cpp | 2 +- src/serviceview.cpp | 123 ++++++++++++++++++++++++++++++++++++++---- src/serviceview.h | 10 +++- src/window.cpp | 2 +- 4 files changed, 124 insertions(+), 13 deletions(-) diff --git a/src/commandrunner.cpp b/src/commandrunner.cpp index a5d7596..7df1b11 100644 --- a/src/commandrunner.cpp +++ b/src/commandrunner.cpp @@ -317,7 +317,7 @@ void CommandRunner::requestClusters() void CommandRunner::requestServiceList(QString pName) { m_command = "service"; - QStringList args = { "-p", pName, "service", "list" }; + QStringList args = { "-p", pName, "service", "list", "-o", "json" }; executeMinikubeCommand(args); } diff --git a/src/serviceview.cpp b/src/serviceview.cpp index 4c2b38b..be57c5e 100644 --- a/src/serviceview.cpp +++ b/src/serviceview.cpp @@ -19,29 +19,132 @@ limitations under the License. #include #include #include +#include +#include +#include +#include +#include +#include +#include +#ifndef QT_NO_TERMWIDGET +#include "qtermwidget.h" +#include +#endif -ServiceView::ServiceView(QDialog *parent, QIcon icon) +ServiceView::ServiceView(QDialog *parent, QIcon icon, CommandRunner *runner, Settings *settings) { m_parent = parent; m_icon = icon; + m_commandRunner = runner; + m_settings = settings; } void ServiceView::displayTable(QString svcCmdOutput) { m_dialog = new QDialog(m_parent); + m_dialog->setWindowFlags(m_dialog->windowFlags()); m_dialog->setWindowTitle(tr("Service List")); m_dialog->setWindowIcon(m_icon); - m_dialog->setFixedWidth(600); + m_dialog->setMinimumWidth(800); m_dialog->setModal(true); - QFormLayout form(m_dialog); - QLabel *tableLbl = new QLabel(); - tableLbl->setOpenExternalLinks(true); - tableLbl->setWordWrap(true); - tableLbl->setText(svcCmdOutput); - form.addRow(tableLbl); - QDialogButtonBox buttonBox(Qt::Horizontal, m_dialog); + QBoxLayout layout(QBoxLayout::TopToBottom, m_dialog); + QJsonParseError error; + QJsonDocument json = QJsonDocument::fromJson(svcCmdOutput.toUtf8(), &error); + if (json.isNull() || !json.isArray()) { + // cannot deseralize the output, print it to the dialog + qDebug() << error.errorString(); + QLabel *tableLbl = new QLabel(); + tableLbl->setOpenExternalLinks(true); + tableLbl->setWordWrap(true); + tableLbl->setText(svcCmdOutput); + layout.addWidget(tableLbl); + } else { + // can deseralize the output, generate a table + QJsonArray serviceArray = json.array(); + QTableWidget *table = new QTableWidget(serviceArray.size(), 5); + table->resizeColumnsToContents(); + QStringList headers({ "Namespace", "Name", "Target Port", "URL", "Actions" }); + table->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents); + table->horizontalHeader()->setStretchLastSection(true); + table->setHorizontalHeaderLabels(headers); + for (int i = 0; i < serviceArray.size(); i++) { + QJsonObject obj = serviceArray[i].toObject(); + QString nameSpace, serviceName; + if (obj.contains("Namespace")) { + nameSpace = obj["Namespace"].toString(); + QTableWidgetItem *namespaceItem = new QTableWidgetItem(nameSpace); + table->setItem(i, 0, namespaceItem); + } + if (obj.contains("Name")) { + serviceName = obj["Name"].toString(); + QTableWidgetItem *nameItem = new QTableWidgetItem(serviceName); + table->setItem(i, 1, nameItem); + } + if (obj.contains("PortNames")) { + QString listString = + QJsonDocument(obj["PortNames"].toArray()).toJson(QJsonDocument::Compact); + QTableWidgetItem *portNamesItem = new QTableWidgetItem(listString); + table->setItem(i, 2, portNamesItem); + } + if (obj.contains("URLs")) { + QString listString = + QJsonDocument(obj["URLs"].toArray()).toJson(QJsonDocument::Compact); + QTableWidgetItem *URLItem = new QTableWidgetItem(listString); + table->setItem(i, 3, URLItem); + } + QPushButton *button = new QPushButton(); + button->setText("Connect"); + table->setCellWidget(i, 4, button); + connect(button, &QPushButton::clicked, this, [this, serviceName, nameSpace]() -> void { + qDebug() << "here"; + this->runMinikubeService(nameSpace, serviceName); + }); + } + layout.addWidget(table); + } + + QDialogButtonBox buttonBox(Qt::Horizontal); buttonBox.addButton(QString(tr("OK")), QDialogButtonBox::AcceptRole); connect(&buttonBox, &QDialogButtonBox::accepted, m_dialog, &QDialog::accept); - form.addRow(&buttonBox); + layout.addWidget(&buttonBox); m_dialog->exec(); } + +void ServiceView::runMinikubeService(QString nameSpace, QString serviceName) +{ + QStringList commandArgs = { "service", "-n", nameSpace, serviceName }; + QString command = m_settings->minikubePath() + " " + commandArgs.join(" "); + +#ifndef QT_NO_TERMWIDGET + int startnow = 0; // set shell program first + + QTermWidget *console = new QTermWidget(startnow); + + QFont font = QApplication::font(); + font.setFamily("Monospace"); + font.setPointSize(10); + + console->setTerminalFont(font); + console->setColorScheme("Tango"); + console->setShellProgram("eval"); + console->setArgs({ commandArgs }); + console->startShellProgram(); + +#elif __APPLE__ + QStringList arguments = { "-e", "tell app \"Terminal\"", + "-e", "do script \"" + command + "\"", + "-e", "activate", + "-e", "end tell" }; + m_commandRunner->executeCommand("/usr/bin/osascript", arguments); +#else + QString terminal = qEnvironmentVariable("TERMINAL"); + if (terminal.isEmpty()) { + terminal = "x-terminal-emulator"; + if (QStandardPaths::findExecutable(terminal).isEmpty()) { + terminal = "xterm"; + } + } + + m_commandRunner->executeCommand(QStandardPaths::findExecutable(terminal), { "-e", command }); +#endif +} diff --git a/src/serviceview.h b/src/serviceview.h index f9b7f86..13129ec 100644 --- a/src/serviceview.h +++ b/src/serviceview.h @@ -19,20 +19,28 @@ limitations under the License. #include #include +#include +#include +#include "commandrunner.h" class ServiceView : public QObject { Q_OBJECT public: - explicit ServiceView(QDialog *parent, QIcon icon); + explicit ServiceView(QDialog *parent, QIcon icon, CommandRunner *commandRunner, + Settings *settings); void displayTable(QString); private: + void runMinikubeService(QString nameSpace, QString serviceName); + QDialog *m_dialog; QIcon m_icon; QDialog *m_parent; + CommandRunner *m_commandRunner; + Settings *m_settings; }; #endif // SERVICEVIEW_H diff --git a/src/window.cpp b/src/window.cpp index 6cbeec1..37f3861 100644 --- a/src/window.cpp +++ b/src/window.cpp @@ -45,7 +45,7 @@ Window::Window() settings = new Settings(); commandRunner = new CommandRunner(this, logger, settings); basicView = new BasicView(*trayIconIcon, version); - serviceView = new ServiceView(this, *trayIconIcon); + serviceView = new ServiceView(this, *trayIconIcon, commandRunner, settings); addonsView = new AddonsView(*trayIconIcon); advancedView = new AdvancedView(*trayIconIcon); errorMessage = new ErrorMessage(this, *trayIconIcon);