Add dialog menu

- Need to fix static linking

Signed-off-by: Tiago Garcia <tiago.rgarcia@ua.pt>
This commit is contained in:
Tiago Garcia 2025-01-11 03:14:16 +00:00
parent 549444fd47
commit 6688a89cb7
Signed by: TiagoRG
GPG Key ID: DFCD48E3F420DB42
10 changed files with 228 additions and 27 deletions

View File

@ -24,6 +24,9 @@ set(EXECUTABLE_NAME "gh-wh-handler.${ARCH}")
set(CMAKE_CXX_STANDARD 23) set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_STANDARD_REQUIRED True) set(CMAKE_CXX_STANDARD_REQUIRED True)
# Set the compiler flags
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Werror -pedantic")
# Set the output directory # Set the output directory
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/bin) set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/bin)
@ -37,13 +40,11 @@ file(GLOB_RECURSE SOURCES "src/*.cpp")
# Add the executable # Add the executable
add_executable(${EXECUTABLE_NAME} ${SOURCES}) add_executable(${EXECUTABLE_NAME} ${SOURCES})
target_include_directories(${EXECUTABLE_NAME} PUBLIC ${INCLUDE_DIR}) target_include_directories(${EXECUTABLE_NAME} PUBLIC ${INCLUDE_DIR})
target_link_libraries(${EXECUTABLE_NAME} PUBLIC ncurses dialog)
# Add compilation flags
target_compile_options(${EXECUTABLE_NAME} PRIVATE -Wall -Werror)
# Set linker flags for static linking # Set linker flags for static linking
set_target_properties(${EXECUTABLE_NAME} PROPERTIES LINK_FLAGS # set_target_properties(${EXECUTABLE_NAME} PROPERTIES LINK_FLAGS
"-static -static-libgcc -static-libstdc++") # "-static -static-libgcc -static-libstdc++")
# Install the executable # Install the executable
set(SERVICE_EXECUTABLE "/services/gh-wh-handler/${EXECUTABLE_NAME}") set(SERVICE_EXECUTABLE "/services/gh-wh-handler/${EXECUTABLE_NAME}")

View File

@ -7,24 +7,26 @@ class Config {
public: public:
static void create_config(std::string config_file_path); static void create_config(std::string config_file_path);
static nlohmann::json get_config(std::string config_file_path); static nlohmann::json get_config(std::string config_file_path);
static void open_config_menu(); static void open_menu(nlohmann::json config, std::string config_file_path);
private: private:
static void set_port(int port); static void open_terminal_only_menu(nlohmann::json config, std::string config_file_path);
static void add_update_files_repo(std::string repo, std::string branch); static nlohmann::json set_port(nlohmann::json config, int port);
static void add_update_files_file(std::string repo, std::string remote_path, std::string local_path);
static void add_update_files_post_update(std::string repo, std::string command);
static void add_run_scripts_repo(std::string repo, std::string branch);
static void add_run_scripts_script(std::string repo, std::string script_path);
static void add_token(std::string repo, std::string token);
static void remove_update_files_repo(std::string repo); static nlohmann::json add_update_files_repo(nlohmann::json config, std::string repo, std::string branch);
static void remove_update_files_file(std::string repo, std::string remote_path); static nlohmann::json add_update_files_file(nlohmann::json config, std::string repo, std::string remote_path, std::string local_path);
static void remove_update_files_post_update(std::string repo, std::string command); static nlohmann::json add_update_files_post_update(nlohmann::json config, std::string repo, std::string command);
static void remove_run_scripts_repo(std::string repo); static nlohmann::json add_run_scripts_repo(nlohmann::json config, std::string repo, std::string branch);
static void remove_run_scripts_script(std::string repo, std::string script_path); static nlohmann::json add_run_actions_action(nlohmann::json config, std::string repo, std::string script_path);
static void remove_token(std::string repo, std::string token); static nlohmann::json add_token(nlohmann::json config, std::string repo, std::string token);
static nlohmann::json remove_update_files_repo(nlohmann::json config, std::string repo);
static nlohmann::json remove_update_files_file(nlohmann::json config, std::string repo, std::string remote_path);
static nlohmann::json remove_update_files_post_update(nlohmann::json config, std::string repo, std::string command);
static nlohmann::json remove_run_scripts_repo(nlohmann::json config, std::string repo);
static nlohmann::json remove_run_actions_action(nlohmann::json config, std::string repo, std::string script_path);
static nlohmann::json remove_token(nlohmann::json config, std::string repo);
}; };
#endif #endif

View File

@ -0,0 +1,14 @@
#ifndef CONFIG_API_HPP
#define CONFIG_API_HPP
#include <nlohmann/json.hpp>
class ConfigApi {
public:
static bool set_port(nlohmann::json config, int port);
static bool add_token(nlohmann::json config, std::string repo, std::string token);
static bool remove_token(nlohmann::json config, std::string repo);
};
#endif

View File

@ -0,0 +1,11 @@
#ifndef DIALOG_MENU_HPP
#define DIALOG_MENU_HPP
#include <nlohmann/json.hpp>
class DialogMenu {
public:
static void open_menu(nlohmann::json config, std::string config_file_path);
};
#endif

View File

@ -0,0 +1,16 @@
#ifndef TERMINAL_MENU_HPP
#define TERMINAL_MENU_HPP
#include <nlohmann/json.hpp>
class TerminalMenu {
public:
static void open_menu(nlohmann::json config, std::string config_file_path);
private:
static void general_menu(nlohmann::json config, std::string config_file_path);
static void update_files_menu(nlohmann::json config, std::string config_file_path);
static void run_scripts_menu(nlohmann::json config, std::string config_file_path);
};
#endif

View File

@ -2,7 +2,10 @@
#include <filesystem> #include <filesystem>
#include <iostream> #include <iostream>
#include <fstream> #include <fstream>
#include <unistd.h>
#include "config/terminal-menu.hpp"
#include "config/dialog-menu.hpp"
#include "logger.hpp" #include "logger.hpp"
void Config::create_config(std::string config_file_path) { void Config::create_config(std::string config_file_path) {
@ -62,6 +65,19 @@ nlohmann::json Config::get_config(std::string config_file_path) {
return config; return config;
} }
void Config::open_config_menu() { void Config::open_menu(nlohmann::json config, std::string config_file_path) {
Logger::warn("[Config] Config menu not implemented yet"); Logger::info("[Config] Opening config menu");
std::string menu = "";
if (std::system("which dialog > /dev/null") == 0) {
menu = "dialog";
} else {
menu = "terminal";
}
if (menu == "dialog") {
DialogMenu::open_menu(config, config_file_path);
} else {
TerminalMenu::open_menu(config, config_file_path);
}
} }

28
src/config/config-api.cpp Normal file
View File

@ -0,0 +1,28 @@
#include "config/config-api.hpp"
#include "logger.hpp"
bool ConfigApi::set_port(nlohmann::json config, int port) {
Logger::info("[Config] Setting port to " + std::to_string(port));
config["port"] = port;
return true;
}
bool ConfigApi::add_token(nlohmann::json config, std::string repo, std::string token) {
Logger::info("[Config] Adding token for repo " + repo);
if (config["tokens"].find(repo) != config["tokens"].end()) {
Logger::warn("Token already exists for this repo.");
return false;
}
config["tokens"][repo] = token;
return true;
}
bool ConfigApi::remove_token(nlohmann::json config, std::string repo) {
Logger::info("[Config] Removing token for repo " + repo);
if (config["tokens"].find(repo) == config["tokens"].end()) {
Logger::warn("Token does not exist for this repo.");
return false;
}
config["tokens"].erase(repo);
return true;
}

View File

@ -0,0 +1,15 @@
#include <dialog.h>
#include "config/dialog-menu.hpp"
#include "logger.hpp"
void DialogMenu::open_menu(nlohmann::json config, std::string config_file_path) {
Logger::info("Opening dialog menu");
init_dialog(stdin, stdout);
dialog_msgbox(
"GitHub Webhook Handler - Config Menu",
"\nWelcome to the config menu!\n\nThis is a work in progress, please use the terminal menu for now.",
10, 50, 1);
Logger::warn("Dialog menu is a work in progress, please use the terminal menu for now.");
end_dialog();
Logger::info("Dialog menu closed");
}

View File

@ -0,0 +1,98 @@
#include <iostream>
#include <fstream>
#include "config/terminal-menu.hpp"
#include "config/config-api.hpp"
#include "logger.hpp"
void print_menu() {
std::cout << "1. Print menu" << std::endl;
std::cout << "2. Preview current config" << std::endl;
std::cout << "3. Set port" << std::endl;
std::cout << "4. Add token" << std::endl;
std::cout << "5. Remove token" << std::endl;
std::cout << "6. Add run actions repo" << std::endl;
std::cout << "7. Add run actions action" << std::endl;
std::cout << "8. Remove run actions repo" << std::endl;
std::cout << "9. Remove run actions action" << std::endl;
std::cout << "10. Add update files repo" << std::endl;
std::cout << "11. Add update files file" << std::endl;
std::cout << "12. Add update files post-update" << std::endl;
std::cout << "13. Remove update files repo" << std::endl;
std::cout << "14. Remove update files file" << std::endl;
std::cout << "15. Remove update files post-update" << std::endl;
std::cout << "16. Exit and save changes" << std::endl;
std::cout << "17. Exit and discard changes" << std::endl;
}
void TerminalMenu::open_menu(nlohmann::json config, std::string config_file_path) {
Logger::warn("[Config] Config menu (no TUI available, using terminal only)");
print_menu();
while (true) {
std::cout << ">>> ";
int choice;
std::cin >> choice;
switch (choice) {
case 1: {
print_menu();
break;
}
case 2: {
Logger::info("[Config] Current config: ");
Logger::code(config.dump(2));
break;
}
case 3: {
int port;
std::cout << "Enter new port: ";
std::cin >> port;
config = ConfigApi::set_port(config, port);
break;
}
case 4: {
std::string repo, token;
std::cout << "Enter repo name (owner/name): ";
std::cin >> repo;
if (config["tokens"].find(repo) != config["tokens"].end()) {
std::cout << "Token already exists for this repo. Do you want to update it? (y/N): ";
char update;
std::cin >> update;
if (update != 'y') {
break;
}
}
std::cout << "Enter token: ";
std::cin >> token;
config = ConfigApi::add_token(config, repo, token);
break;
}
case 5: {
std::string repo;
std::cout << "Enter repo name (owner/name): ";
std::cin >> repo;
config = ConfigApi::remove_token(config, repo);
break;
}
case 16: {
Logger::info("[Config] Saving changes to config file: " + config_file_path);
Logger::info("[Config] New config: ");
std::ofstream config_file(config_file_path);
Logger::code(config.dump(2));
config_file << config.dump(2);
config_file.close();
return;
}
case 17: {
std::cout << "Exiting without saving changes" << std::endl;
return;
}
default:
std::cout << "Invalid choice" << std::endl;
}
}
}

View File

@ -30,20 +30,20 @@ int main(int argc, char **argv) {
Logger::init(logs_dir); Logger::init(logs_dir);
if (argc == 4 && std::string(argv[3]) == "--config") {
Config::open_config_menu();
return 0;
}
// Check if config file exists // Check if config file exists
if (!std::filesystem::exists(config_file_path)) { if (!std::filesystem::exists(config_file_path)) {
Logger::warn("Config file does not exist, creating..."); Logger::warn("Config file does not exist, creating...");
Config::create_config(config_file_path); Config::create_config(config_file_path);
} }
// Load configuration // Load current configuration
nlohmann::json config = Config::get_config(config_file_path); nlohmann::json config = Config::get_config(config_file_path);
if (argc == 4 && std::string(argv[3]) == "--config") {
Config::open_menu(config, config_file_path);
return 0;
}
std::signal(SIGINT, signal_handler); std::signal(SIGINT, signal_handler);
// Start server // Start server