From 6688a89cb7fad4683b24a028f6b4f18a2b953eb1 Mon Sep 17 00:00:00 2001 From: Tiago Garcia Date: Sat, 11 Jan 2025 03:14:16 +0000 Subject: [PATCH] Add dialog menu - Need to fix static linking Signed-off-by: Tiago Garcia --- CMakeLists.txt | 11 ++-- include/config.hpp | 30 +++++----- include/config/config-api.hpp | 14 +++++ include/config/dialog-menu.hpp | 11 ++++ include/config/terminal-menu.hpp | 16 ++++++ src/config.cpp | 20 ++++++- src/config/config-api.cpp | 28 +++++++++ src/config/dialog-menu.cpp | 15 +++++ src/config/terminal-menu.cpp | 98 ++++++++++++++++++++++++++++++++ src/main.cpp | 12 ++-- 10 files changed, 228 insertions(+), 27 deletions(-) create mode 100644 include/config/config-api.hpp create mode 100644 include/config/dialog-menu.hpp create mode 100644 include/config/terminal-menu.hpp create mode 100644 src/config/config-api.cpp create mode 100644 src/config/dialog-menu.cpp create mode 100644 src/config/terminal-menu.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 61cf806..b977eae 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -24,6 +24,9 @@ set(EXECUTABLE_NAME "gh-wh-handler.${ARCH}") set(CMAKE_CXX_STANDARD 23) 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(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/bin) @@ -37,13 +40,11 @@ file(GLOB_RECURSE SOURCES "src/*.cpp") # Add the executable add_executable(${EXECUTABLE_NAME} ${SOURCES}) target_include_directories(${EXECUTABLE_NAME} PUBLIC ${INCLUDE_DIR}) - -# Add compilation flags -target_compile_options(${EXECUTABLE_NAME} PRIVATE -Wall -Werror) +target_link_libraries(${EXECUTABLE_NAME} PUBLIC ncurses dialog) # Set linker flags for static linking -set_target_properties(${EXECUTABLE_NAME} PROPERTIES LINK_FLAGS - "-static -static-libgcc -static-libstdc++") +# set_target_properties(${EXECUTABLE_NAME} PROPERTIES LINK_FLAGS +# "-static -static-libgcc -static-libstdc++") # Install the executable set(SERVICE_EXECUTABLE "/services/gh-wh-handler/${EXECUTABLE_NAME}") diff --git a/include/config.hpp b/include/config.hpp index 396d086..081acfb 100644 --- a/include/config.hpp +++ b/include/config.hpp @@ -7,24 +7,26 @@ class Config { public: static void create_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: - 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 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 nlohmann::json set_port(nlohmann::json config, int port); - static void remove_update_files_repo(std::string repo); - static void remove_update_files_file(std::string repo, std::string remote_path); - static void remove_update_files_post_update(std::string repo, std::string command); - static void remove_run_scripts_repo(std::string repo); - static void remove_run_scripts_script(std::string repo, std::string script_path); - static void remove_token(std::string repo, std::string token); + static nlohmann::json add_update_files_repo(nlohmann::json config, std::string repo, std::string branch); + static nlohmann::json add_update_files_file(nlohmann::json config, std::string repo, std::string remote_path, std::string local_path); + static nlohmann::json add_update_files_post_update(nlohmann::json config, std::string repo, std::string command); + static nlohmann::json add_run_scripts_repo(nlohmann::json config, std::string repo, std::string branch); + static nlohmann::json add_run_actions_action(nlohmann::json config, std::string repo, std::string script_path); + 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 diff --git a/include/config/config-api.hpp b/include/config/config-api.hpp new file mode 100644 index 0000000..c254c18 --- /dev/null +++ b/include/config/config-api.hpp @@ -0,0 +1,14 @@ +#ifndef CONFIG_API_HPP +#define CONFIG_API_HPP + +#include + +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 + diff --git a/include/config/dialog-menu.hpp b/include/config/dialog-menu.hpp new file mode 100644 index 0000000..0c1b7d7 --- /dev/null +++ b/include/config/dialog-menu.hpp @@ -0,0 +1,11 @@ +#ifndef DIALOG_MENU_HPP +#define DIALOG_MENU_HPP + +#include + +class DialogMenu { + public: + static void open_menu(nlohmann::json config, std::string config_file_path); +}; + +#endif diff --git a/include/config/terminal-menu.hpp b/include/config/terminal-menu.hpp new file mode 100644 index 0000000..61b3362 --- /dev/null +++ b/include/config/terminal-menu.hpp @@ -0,0 +1,16 @@ +#ifndef TERMINAL_MENU_HPP +#define TERMINAL_MENU_HPP + +#include + +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 diff --git a/src/config.cpp b/src/config.cpp index c5d76fb..ed6d849 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -2,7 +2,10 @@ #include #include #include +#include +#include "config/terminal-menu.hpp" +#include "config/dialog-menu.hpp" #include "logger.hpp" 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; } -void Config::open_config_menu() { - Logger::warn("[Config] Config menu not implemented yet"); +void Config::open_menu(nlohmann::json config, std::string config_file_path) { + 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); + } } + diff --git a/src/config/config-api.cpp b/src/config/config-api.cpp new file mode 100644 index 0000000..afa348d --- /dev/null +++ b/src/config/config-api.cpp @@ -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; +} diff --git a/src/config/dialog-menu.cpp b/src/config/dialog-menu.cpp new file mode 100644 index 0000000..da92a50 --- /dev/null +++ b/src/config/dialog-menu.cpp @@ -0,0 +1,15 @@ +#include +#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"); +} diff --git a/src/config/terminal-menu.cpp b/src/config/terminal-menu.cpp new file mode 100644 index 0000000..d07991b --- /dev/null +++ b/src/config/terminal-menu.cpp @@ -0,0 +1,98 @@ +#include +#include +#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; + } + + } +} + diff --git a/src/main.cpp b/src/main.cpp index c29f5ab..df2bcc2 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -30,20 +30,20 @@ int main(int argc, char **argv) { Logger::init(logs_dir); - if (argc == 4 && std::string(argv[3]) == "--config") { - Config::open_config_menu(); - return 0; - } - // Check if config file exists if (!std::filesystem::exists(config_file_path)) { Logger::warn("Config file does not exist, creating..."); Config::create_config(config_file_path); } - // Load configuration + // Load current configuration 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); // Start server