From 6141492a139822abde2b734942421aead5fdd47f Mon Sep 17 00:00:00 2001 From: lucas sprouole Date: Mon, 18 Dec 2023 04:56:58 -0500 Subject: [PATCH] changes to uv_watch and cleanup --- include/Frate/Command.hpp | 2 +- memcheck | 34 ------ src/Command/Actions/UvWatch.cpp | 117 ++++++++++++++------- src/Command/RemoteServers/RemoteServer.cpp | 12 +++ 4 files changed, 94 insertions(+), 71 deletions(-) delete mode 100644 memcheck diff --git a/include/Frate/Command.hpp b/include/Frate/Command.hpp index 1ea4ba0..fc1a151 100644 --- a/include/Frate/Command.hpp +++ b/include/Frate/Command.hpp @@ -119,7 +119,7 @@ namespace Frate::Command { std::string homepage{"null"}; std::string bugs{"null"}; std::string lang{"cpp"}; - std::string cmake_version{"3.10"}; + std::string cmake_version{"3.28"}; std::string lang_version{"20"}; std::string compiler{"g++"}; std::string license{""}; diff --git a/memcheck b/memcheck deleted file mode 100644 index ec99019..0000000 --- a/memcheck +++ /dev/null @@ -1,34 +0,0 @@ -==77219== Memcheck, a memory error detector -==77219== Copyright (C) 2002-2022, and GNU GPL'd, by Julian Seward et al. -==77219== Using Valgrind-3.22.0 and LibVEX; rerun with -h for copyright info -==77219== Command: ./build/frate new -d -==77219== Parent PID: 77218 -==77219== - -valgrind: Fatal error at startup: a function redirection -valgrind: which is mandatory for this platform-tool combination -valgrind: cannot be set up. Details of the redirection are: -valgrind: -valgrind: A must-be-redirected function -valgrind: whose name matches the pattern: strlen -valgrind: in an object with soname matching: ld-linux-x86-64.so.2 -valgrind: was not found whilst processing -valgrind: symbols from the object with soname: ld-linux-x86-64.so.2 -valgrind: -valgrind: Possible fixes: (1, short term): install glibc's debuginfo -valgrind: package on this machine. (2, longer term): ask the packagers -valgrind: for your Linux distribution to please in future ship a non- -valgrind: stripped ld.so (or whatever the dynamic linker .so is called) -valgrind: that exports the above-named function using the standard -valgrind: calling conventions for this platform. The package you need -valgrind: to install for fix (1) is called -valgrind: -valgrind: On Debian, Ubuntu: libc6-dbg -valgrind: On SuSE, openSuSE, Fedora, RHEL: glibc-debuginfo -valgrind: -valgrind: Note that if you are debugging a 32 bit process on a -valgrind: 64 bit system, you will need a corresponding 32 bit debuginfo -valgrind: package (e.g. libc6-dbg:i386). -valgrind: -valgrind: Cannot continue -- exiting now. Sorry. - diff --git a/src/Command/Actions/UvWatch.cpp b/src/Command/Actions/UvWatch.cpp index 45dcd55..5da0881 100644 --- a/src/Command/Actions/UvWatch.cpp +++ b/src/Command/Actions/UvWatch.cpp @@ -15,12 +15,35 @@ namespace Frate::Command::UvWatch{ return inter->parse(); } //TODO Move to RemoteServer.hpp + + std::optional parseJsonFile(const std::string& filePath) { + std::ifstream file(filePath); + if (!file) { + std::cerr << "Unable to open file: " << filePath << std::endl; + return std::nullopt; + } + + try { + json j = json::parse(file); + return j; + } catch (const json::parse_error& e) { + std::cerr << "JSON parse error in file " << filePath << ": " << e.what() << std::endl; + return std::nullopt; + } + } + RemoteServer get_current_build_server() { std::string current_build_server = std::string(std::getenv("HOME")) + "/.config/frate/" + "current_build_server.json"; - json current_build_server_json = - json::parse(std::ifstream(current_build_server)); + std::optional data = parseJsonFile(current_build_server); + if (!data.has_value()) { + std::cerr << "No current build server found" << std::endl; + std::cerr << "Please run frate add server to add a server" << std::endl; + std::cerr << "Please run frate set server to set a default a server" << std::endl; + return RemoteServer(); + } + json current_build_server_json = data.value(); if (!current_build_server_json["name"].is_null()) { return RemoteServer( current_build_server_json["name"].get(), @@ -34,36 +57,44 @@ namespace Frate::Command::UvWatch{ return RemoteServer(); } - bool runCommand(Interface* inter){ - //TODO CLEAN UP THIS SHIT - std::string command = "cmake . && make && " + inter->pro->path.string() + "/" + inter->pro->build_dir + "/" +inter->pro->name; -#ifdef DEBUG - command = "cd build && cmake . && make && " + inter->pro->build_dir + "/" +inter->pro->name; -#endif + std::string remote_build_command(std::shared_ptr inter) { + std::string command; + std::string current_build_server = std::string(std::getenv("HOME")) + + "/.config/frate/" + + "current_build_server.json"; + inter->pro->build_server = get_current_build_server(); + std::string sync_files = "rsync -avh --exclude-from='.gitignore' --update -e 'ssh -p " + + std::to_string(inter->pro->build_server.port) + "' --progress . " + + inter->pro->build_server.username + "@" +inter->pro->build_server.ip + + ":/tmp/frate "; - bool build_server =inter->args->operator[]("remote-build").as(); - if (build_server == true) { - std::string current_build_server = std::string(std::getenv("HOME")) + - "/.config/frate/" + - "current_build_server.json"; - inter->pro->build_server = get_current_build_server(); + std::string ssh = "&& ssh -p " + std::to_string(inter->pro->build_server.port) + " " + + inter->pro->build_server.username + "@" +inter->pro->build_server.ip; + + std::string ssh_command = + " 'cd /tmp/frate && cmake . && make -j ${nproc} && " + inter->pro->build_dir + "/" + inter->pro->name + "'"; + + command = sync_files + ssh + ssh_command; + return command; + } - std::string sync_files = "rsync -avh --exclude-from='.gitignore' --update -e 'ssh -p " + - std::to_string(inter->pro->build_server.port) + "' --progress . " + - inter->pro->build_server.username + "@" +inter->pro->build_server.ip + - ":/tmp/frate "; - std::string ssh = "&& ssh -p " + std::to_string(inter->pro->build_server.port) + " " + - inter->pro->build_server.username + "@" +inter->pro->build_server.ip; + bool runCommand(std::shared_ptr inter){ + //TODO CLEAN UP THIS SHIT + std::string command = "cmake . && make && " + inter->pro->path.string() + "/" + inter->pro->build_dir + "/" +inter->pro->name; + #ifdef DEBUG + command = "cd build && cmake . && make && " + inter->pro->build_dir + "/" +inter->pro->name; + #endif - std::string ssh_command = - " 'cd /tmp/frate && cmake . && make -j ${nproc} && " + inter->pro->build_dir + "/" + inter->pro->name + "'"; - command = sync_files + ssh + ssh_command; + bool build_server =inter->args->operator[]("remote-build").as(); + if (build_server == true) { + command = remote_build_command(inter); } if (inter->args->count("args") != 0) { + // historical reasons std::cout << "estamos aqui" << std::endl; std::vector args_vec = inter->args->operator[]("args").as>(); @@ -96,7 +127,8 @@ namespace Frate::Command::UvWatch{ // Ignoring subsequent events return; } - Interface* inter = static_cast(handle->data); + std::shared_ptr inter = *static_cast*>(handle->data); + if (status < 0) { fprintf(stderr, "Filesystem watch error: %s\n", uv_strerror(status)); return; @@ -112,11 +144,12 @@ namespace Frate::Command::UvWatch{ timer->data = handle; uv_timer_start(timer, [](uv_timer_t *timer) { - event_triggered = 0; - uv_close((uv_handle_t *)timer, [](uv_handle_t *handle) { - free(handle); - }); - }, 2000, 0); // 1000 ms delay + event_triggered = 0; + uv_close((uv_handle_t *)timer, [](uv_handle_t *handle) { + free(handle); + }); + delete timer; + }, 2000, 0); // 1000 ms delay } @@ -124,7 +157,7 @@ namespace Frate::Command::UvWatch{ #ifdef __linux__ namespace fs = std::filesystem; - void start_watchers_for_directory(const fs::path& path, uv_loop_t* loop, std::vector& watchers,std::shared_ptr inter) { + void start_watchers_for_directory(const fs::path& path, uv_loop_t* loop, std::vector& watchers, std::shared_ptr inter) { for (const auto& entry : fs::directory_iterator(path)) { if (fs::is_directory(entry)) { uv_fs_event_t* watcher = new uv_fs_event_t{ @@ -140,22 +173,34 @@ namespace Frate::Command::UvWatch{ #endif bool watch(std::shared_ptr inter){ - options(inter); uv_loop_t *loop = uv_default_loop(); - std::vector watchers; + std::vector watchers; uv_fs_event_t fs_event{.data = inter.get()}; uv_fs_event_init(loop, &fs_event); -#ifdef __linux__ - start_watchers_for_directory(inter->pro->src_dir, loop, watchers, inter); -#endif if (uv_fs_event_start(&fs_event, fs_event_callback, (inter->pro->path / inter->pro->src_dir).c_str(), UV_FS_EVENT_RECURSIVE)!=0) { fprintf(stderr, "Error starting filesystem watcher.\n"); return 1; } std::cout << "Watching for changes in " << inter->pro->src_dir << std::endl; - return uv_run(loop, UV_RUN_DEFAULT) == 0; + + if (uv_run(loop, UV_RUN_DEFAULT) != 0) { + std::cerr << "Error running uv loop" << std::endl; + return false; + } + + #ifdef __linux__ + start_watchers_for_directory(inter->pro->src_dir, loop, watchers, inter); + #endif + + int close = uv_loop_close(loop); + if (close != 0) { + std::cerr << "Error closing uv loop" << std::endl; + return false; + } + + return true; }; } diff --git a/src/Command/RemoteServers/RemoteServer.cpp b/src/Command/RemoteServers/RemoteServer.cpp index 4d8eacf..34625f4 100644 --- a/src/Command/RemoteServers/RemoteServer.cpp +++ b/src/Command/RemoteServers/RemoteServer.cpp @@ -76,9 +76,21 @@ namespace Frate::Command::RemoteServers{ } return servers; } + bool validateServerInput(std::string input){ + if(input.empty()){ + return false; + } + if (input.find_first_not_of("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890_") != std::string::npos){ + return false; + } + return true; + } + bool getServerName(std::string& name){ Prompt name_promp("Enter the name of the server: "); + name_promp.setValidator(validateServerInput); name_promp.run(); + auto[valid, _name] = name_promp.get(); if(!valid){ return false;