👷 use cpr for making requests

Disable `-Werror` due to deps warnings
Use of subprocess barebones implementation
This commit is contained in:
Vladislav Nepogodin 2021-11-30 03:51:27 +04:00
parent efeb31a9c0
commit dc5f12e92d
No known key found for this signature in database
GPG Key ID: B62C3D10C54D5DA9
8 changed files with 95 additions and 25 deletions

View File

@ -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}
)

View File

@ -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

View File

@ -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)

View File

@ -1,9 +1,11 @@
#include "config.hpp"
#include "definitions.hpp"
#include "tui.hpp"
#include "config.hpp"
#include "utils.hpp"
#include <chrono>
#include <regex>
#include <thread>
int main() {
const auto& tty = utils::exec("tty");

View File

@ -2,13 +2,17 @@
#include "config.hpp"
#include <sys/mount.h>
#include <sys/wait.h>
#include <filesystem>
#include <iostream>
#include <string>
#include <thread>
#include <netdb.h>
#include <cpr/api.h>
#include <cpr/response.h>
#include <cpr/timeout.h>
#include <cpr/status_codes.h>
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<char*>(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<char, 128> 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;
}
}

View File

@ -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;

8
subprojects/cpr.wrap Normal file
View File

@ -0,0 +1,8 @@
[wrap-git]
url = https://github.com/libcpr/cpr.git
revision = bcb7729dd6d930820fb858f63c97ad559241b789
patch_directory = cpr
[provide]
cpr = cpr_dep

View File

@ -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]
)