added JSON support to select the services
This commit is contained in:
59
src/htmx_helper.cpp
Normal file
59
src/htmx_helper.cpp
Normal file
@@ -0,0 +1,59 @@
|
||||
//
|
||||
// Created by lukas on 5/11/25.
|
||||
//
|
||||
#include <array>
|
||||
#include "systemd.h"
|
||||
#include "htmx_helper.h"
|
||||
#include "json_settings.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
HtmxTableRow create_service_table_row(string_view service_name, string_view service_id) {
|
||||
HtmxTableRow row;
|
||||
|
||||
const bool isRunning = systemd::is_service_active(service_id);
|
||||
const bool isEnabled = systemd::is_service_active(service_id);
|
||||
|
||||
const auto running = isRunning ? "Running" : "Stopped";
|
||||
const auto enabled = isEnabled ? "Enabled" : "Disabled";
|
||||
|
||||
row.add(service_name);
|
||||
row.add_status_box(running, isRunning);
|
||||
row.add_status_box(enabled, isEnabled);
|
||||
|
||||
// create buttons
|
||||
row.add_button("/toggle-service", service_name, isRunning ? "Stop" : "Start");
|
||||
row.add_button("/restart-service", service_name, "Restart");
|
||||
row.complete();
|
||||
|
||||
return row;
|
||||
}
|
||||
|
||||
HtmxTableRow create_error_table_row(string_view error) {
|
||||
HtmxTableRow row;
|
||||
row.add("Error");
|
||||
row.add(error);
|
||||
return row;
|
||||
}
|
||||
|
||||
HtmxTable create_service_table() {
|
||||
constexpr array<string_view, 3> cols = {
|
||||
"Service", "Status", "State"
|
||||
};
|
||||
|
||||
HtmxTable table(cols);
|
||||
|
||||
auto settings = AppSettings::loadAppSettings();
|
||||
|
||||
if(settings.has_value()){
|
||||
AppSettings& data = settings.value();
|
||||
for (auto& service : data.services) {
|
||||
table.add_row(create_service_table_row(service.name, service.service));
|
||||
}
|
||||
}
|
||||
else {
|
||||
table.add_row(create_error_table_row(settings.error()));
|
||||
}
|
||||
|
||||
return table;
|
||||
}
|
||||
Reference in New Issue
Block a user