👷 use unordered_map for config intries

This commit is contained in:
Vladislav Nepogodin 2021-11-28 23:55:26 +04:00
parent fa290acf85
commit efeb31a9c0
No known key found for this signature in database
GPG Key ID: B62C3D10C54D5DA9
13 changed files with 74 additions and 45 deletions

8
.idea/.gitignore vendored
View File

@ -1,8 +0,0 @@
# Default ignored files
/shelf/
/workspace.xml
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml
# Editor-based HTTP Client requests
/httpRequests/

View File

@ -1 +0,0 @@
CachyOS-Installer

View File

@ -1,7 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="DiscordProjectSettings">
<option name="show" value="APPLICATION" />
<option name="description" value="" />
</component>
</project>

View File

@ -1,4 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CMakeWorkspace" PROJECT_DIR="$PROJECT_DIR$" />
</project>

View File

@ -1,8 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/new-installer.iml" filepath="$PROJECT_DIR$/.idea/new-installer.iml" />
</modules>
</component>
</project>

View File

@ -1,2 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<module classpath="CMake" type="CPP_MODULE" version="4" />

View File

@ -1,6 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

View File

@ -56,6 +56,7 @@ target_compile_features(project_options INTERFACE cxx_std_20)
##
add_executable(${PROJECT_NAME}
src/definitions.hpp
src/config.cpp src/config.hpp
src/utils.cpp src/utils.hpp
src/tui.cpp src/tui.hpp
src/main.cpp

View File

@ -29,6 +29,7 @@ nlohmann_json = dependency('nlohmann_json', version : ['>=3.10.4'], fallback : [
src_files = files(
'src/definitions.hpp',
'src/config.cpp', 'src/config.hpp',
'src/utils.cpp', 'src/utils.hpp',
'src/tui.cpp', 'src/tui.hpp',
'src/main.cpp',

24
src/config.cpp Normal file
View File

@ -0,0 +1,24 @@
#include "config.hpp"
#include "definitions.hpp"
#include <memory>
static std::unique_ptr<Config> s_config = nullptr;
bool Config::initialize() noexcept {
if (s_config != nullptr) {
error("You should only initialize it once!\n");
return false;
}
s_config = std::make_unique<Config>();
if (!s_config) {
s_config->m_data["H_INIT"] = "openrc";
s_config->m_data["SYSTEM"] = "BIOS";
}
return s_config.get();
}
auto Config::instance() -> Config* {
return s_config.get();
}

34
src/config.hpp Normal file
View File

@ -0,0 +1,34 @@
#ifndef CONFIG_HPP
#define CONFIG_HPP
#include <string>
#include <string_view>
#include <unordered_map>
class Config final {
public:
using value_type = std::unordered_map<std::string_view, std::string>;
using reference = value_type&;
using const_reference = const value_type&;
Config() = default;
virtual ~Config() = default;
static bool initialize() noexcept;
static Config* instance();
/* clang-format off */
// Element access.
auto data() noexcept -> reference
{ return m_data; }
auto data() const noexcept -> const_reference
{ return m_data; }
/* clang-format on */
private:
value_type m_data{};
};
#endif // CONFIG_HPP

View File

@ -1,5 +1,6 @@
#include "definitions.hpp"
#include "tui.hpp"
#include "config.hpp"
#include "utils.hpp"
#include <regex>
@ -19,6 +20,10 @@ int main() {
}
#endif
if (!Config::initialize()) {
return 1;
}
utils::id_system();
if (!utils::handle_connection()) {

View File

@ -1,4 +1,5 @@
#include "utils.hpp"
#include "config.hpp"
#include <sys/mount.h>
@ -11,13 +12,9 @@
namespace fs = std::filesystem;
static std::string_view H_INIT{"openrc"};
static std::string_view SYSTEM{"BIOS"};
static std::string_view NW_CMD{};
namespace utils {
static constexpr int32_t CONNECTION_TIMEOUT = 15;
namespace utils {
bool is_connected() noexcept {
return gethostbyname("google.com");
}
@ -74,6 +71,9 @@ 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();
// Apple System Detection
const auto& sys_vendor = utils::exec("cat /sys/class/dmi/id/sys_vendor");
if ((sys_vendor == "Apple Inc.\n") || (sys_vendor == "Apple Computer, Inc.\n"))
@ -92,17 +92,17 @@ void id_system() noexcept {
exit(1);
}
}
SYSTEM = "UEFI";
config_data["SYSTEM"] = "UEFI";
}
// init system
const auto& init_sys = utils::exec("cat /proc/1/comm");
if (init_sys == "systemd\n")
H_INIT = "systemd";
config_data["H_INIT"] = "systemd";
// TODO: Test which nw-client is available, including if the service according to $H_INIT is running
if (H_INIT == "systemd" && utils::exec("systemctl is-active NetworkManager") == "active\n")
NW_CMD = "nmtui";
if (config_data["H_INIT"] == "systemd" && utils::exec("systemctl is-active NetworkManager") == "active\n")
config_data["NW_CMD"] = "nmtui";
}
bool handle_connection() noexcept {