diff --git a/CMakeLists.txt b/CMakeLists.txt index 895bb2a..52e2594 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -38,6 +38,12 @@ FetchContent_Declare(nlohmann_json ) FetchContent_MakeAvailable(nlohmann_json) +FetchContent_Declare(cpr + GIT_REPOSITORY "https://github.com/libcpr/cpr.git" + GIT_TAG "bcb7729dd6d930820fb858f63c97ad559241b789" +) +FetchContent_MakeAvailable(cpr) + ## ## CONFIGURATION ## @@ -68,7 +74,7 @@ set_project_warnings(project_warnings) include_directories(${CMAKE_SOURCE_DIR}/src) -target_link_libraries(${PROJECT_NAME} PRIVATE project_warnings project_options fmt::fmt ftxui::screen ftxui::dom ftxui::component nlohmann_json::nlohmann_json) +target_link_libraries(${PROJECT_NAME} PRIVATE project_warnings project_options fmt::fmt ftxui::screen ftxui::dom ftxui::component nlohmann_json::nlohmann_json cpr::cpr) option(ENABLE_UNITY "Enable Unity builds of projects" OFF) if(ENABLE_UNITY) @@ -80,10 +86,3 @@ install( TARGETS ${PROJECT_NAME} RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} ) - - -# uninstall -add_custom_target(uninstall - COMMAND cat ${PROJECT_BINARY_DIR}/install_manifest.txt | xargs rm - WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} -) diff --git a/cmake/CompilerWarnings.cmake b/cmake/CompilerWarnings.cmake index 65834c5..be991f6 100644 --- a/cmake/CompilerWarnings.cmake +++ b/cmake/CompilerWarnings.cmake @@ -1,5 +1,5 @@ function(set_project_warnings project_name) - option(WARNINGS_AS_ERRORS "Treat compiler warnings as error" ON) + option(WARNINGS_AS_ERRORS "Treat compiler warnings as error" OFF) set(MSVC_WARNINGS /W4 # Base diff --git a/meson.build b/meson.build index 0bb6fbb..5f687e1 100644 --- a/meson.build +++ b/meson.build @@ -5,7 +5,7 @@ project('cachyos-installer', 'cpp', default_options: ['cpp_std=c++20', 'buildtype=debugoptimized', 'warning_level=3', - 'werror=true', + 'werror=false', 'b_ndebug=if-release']) is_debug_build = get_option('buildtype').startswith('debug') @@ -26,6 +26,7 @@ endif fmt = dependency('fmt', version : ['>=8.0.0'], fallback : ['fmt', 'fmt_dep']) ftxui = dependency('ftxui', modules : ['ftxui::screen', 'ftxui::dom', 'ftxui::component'], fallback : ['ftxui', 'ftxui_dep']) nlohmann_json = dependency('nlohmann_json', version : ['>=3.10.4'], fallback : ['nlohmann_json', 'nlohmann_json_dep']) +cpr = dependency('cpr', version : ['>=1.7.0'], fallback : ['cpr', 'cpr_dep']) src_files = files( 'src/definitions.hpp', @@ -84,7 +85,7 @@ add_project_arguments(cc.get_supported_arguments(possible_cc_flags), language : executable( 'cachyos-installer', src_files, - dependencies: [fmt, ftxui, nlohmann_json], + dependencies: [fmt, ftxui, nlohmann_json, cpr], include_directories: [include_directories('src')], install: true) diff --git a/src/main.cpp b/src/main.cpp index e4bb8cf..e1c3e9f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,9 +1,11 @@ +#include "config.hpp" #include "definitions.hpp" #include "tui.hpp" -#include "config.hpp" #include "utils.hpp" +#include #include +#include int main() { const auto& tty = utils::exec("tty"); diff --git a/src/utils.cpp b/src/utils.cpp index f81365c..faa28d6 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -2,13 +2,17 @@ #include "config.hpp" #include +#include #include #include #include #include -#include +#include +#include +#include +#include namespace fs = std::filesystem; @@ -16,7 +20,11 @@ namespace utils { static constexpr int32_t CONNECTION_TIMEOUT = 15; bool is_connected() noexcept { - return gethostbyname("google.com"); + /* clang-format off */ + auto r = cpr::Get(cpr::Url{"https://www.google.com"}, + cpr::Timeout{1000}); + /* clang-format on */ + return cpr::status::is_success(r.status_code); } bool check_root() noexcept { @@ -28,19 +36,32 @@ void clear_screen() noexcept { write(STDOUT_FILENO, CLEAR_SCREEN_ANSI, 11); } -std::string exec(const std::string_view& command) noexcept { - char buffer[128]; - std::string result; - - FILE* pipe = popen(command.data(), "r"); - +std::string exec(const std::string_view& command, bool capture_output) noexcept { + if (!capture_output) { + int32_t status{}; + auto pid = fork(); + if (pid == 0) { + /* clang-format off */ + char* args[2] = { const_cast(command.data()), NULL }; + /* clang-format on */ + execvp(args[0], args); + } else { + do { + waitpid(pid, &status, 0); + } while ((!WIFEXITED(status)) && (!WIFSIGNALED(status))); + } + return ""; + } + auto* pipe = popen(command.data(), "r"); if (!pipe) { return "popen failed!"; } + std::string result{}; + std::array buffer{}; while (!feof(pipe)) { - if (fgets(buffer, 128, pipe) != nullptr) { - result += buffer; + if (fgets(buffer.data(), buffer.size(), pipe) != nullptr) { + result += buffer.data(); } } @@ -72,7 +93,7 @@ bool prompt_char(const char* prompt, const char* color, char* read) noexcept { void id_system() noexcept { auto* config_instance = Config::instance(); - auto& config_data = config_instance->data(); + auto& config_data = config_instance->data(); // Apple System Detection const auto& sys_vendor = utils::exec("cat /sys/class/dmi/id/sys_vendor"); @@ -149,7 +170,7 @@ void show_iwctl() noexcept { info("6 - type `exit`\n"); while (utils::prompt_char("Press a key to continue...", CYAN)) { - utils::exec("iwctl"); + utils::exec("iwctl", false); break; } } diff --git a/src/utils.hpp b/src/utils.hpp index 43ac1b0..d0b3b3e 100644 --- a/src/utils.hpp +++ b/src/utils.hpp @@ -11,7 +11,7 @@ void print_banner() noexcept; bool prompt_char(const char* prompt, const char* color = RESET, char* read = nullptr) noexcept; void clear_screen() noexcept; -auto exec(const std::string_view& command) noexcept -> std::string; +auto exec(const std::string_view& command, bool capture_output = true) noexcept -> std::string; [[nodiscard]] bool check_root() noexcept; void id_system() noexcept; [[nodiscard]] bool handle_connection() noexcept; diff --git a/subprojects/cpr.wrap b/subprojects/cpr.wrap new file mode 100644 index 0000000..80fde24 --- /dev/null +++ b/subprojects/cpr.wrap @@ -0,0 +1,8 @@ +[wrap-git] +url = https://github.com/libcpr/cpr.git +revision = bcb7729dd6d930820fb858f63c97ad559241b789 + +patch_directory = cpr + +[provide] +cpr = cpr_dep diff --git a/subprojects/packagefiles/cpr/meson.build b/subprojects/packagefiles/cpr/meson.build new file mode 100644 index 0000000..b662614 --- /dev/null +++ b/subprojects/packagefiles/cpr/meson.build @@ -0,0 +1,39 @@ +project('cpr', 'cpp', + version: '1.7.0', + license : 'MIT', + default_options: ['cpp_std=c++11'] +) + +curl_dep = dependency('libcurl', required : true) + +src_inc = include_directories('.') +cpr_inc = include_directories('include') +cpr_lib = static_library('cpr', + sources : [ + 'cpr/auth.cpp', + 'cpr/bearer.cpp', + 'cpr/cookies.cpp', + 'cpr/cprtypes.cpp', + 'cpr/curl_container.cpp', + 'cpr/curlholder.cpp', + 'cpr/error.cpp', + 'cpr/multipart.cpp', + 'cpr/parameters.cpp', + 'cpr/payload.cpp', + 'cpr/proxies.cpp', + 'cpr/proxyauth.cpp', + 'cpr/session.cpp', + 'cpr/timeout.cpp', + 'cpr/unix_socket.cpp', + 'cpr/util.cpp', + 'cpr/response.cpp', + 'cpr/redirect.cpp' + ], + include_directories : [cpr_inc, src_inc], + dependencies : [curl_dep] +) + +cpr_dep = declare_dependency( + include_directories : [cpr_inc, src_inc], + link_with : [cpr_lib] +)