mirror of
https://github.com/CachyOS/New-Cli-Installer.git
synced 2025-01-23 22:42:31 +08:00
👷 use unordered_map for config intries
This commit is contained in:
parent
fa290acf85
commit
efeb31a9c0
8
.idea/.gitignore
vendored
8
.idea/.gitignore
vendored
@ -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/
|
|
@ -1 +0,0 @@
|
|||||||
CachyOS-Installer
|
|
@ -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>
|
|
@ -1,4 +0,0 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<project version="4">
|
|
||||||
<component name="CMakeWorkspace" PROJECT_DIR="$PROJECT_DIR$" />
|
|
||||||
</project>
|
|
@ -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>
|
|
@ -1,2 +0,0 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<module classpath="CMake" type="CPP_MODULE" version="4" />
|
|
@ -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>
|
|
@ -56,6 +56,7 @@ target_compile_features(project_options INTERFACE cxx_std_20)
|
|||||||
##
|
##
|
||||||
add_executable(${PROJECT_NAME}
|
add_executable(${PROJECT_NAME}
|
||||||
src/definitions.hpp
|
src/definitions.hpp
|
||||||
|
src/config.cpp src/config.hpp
|
||||||
src/utils.cpp src/utils.hpp
|
src/utils.cpp src/utils.hpp
|
||||||
src/tui.cpp src/tui.hpp
|
src/tui.cpp src/tui.hpp
|
||||||
src/main.cpp
|
src/main.cpp
|
||||||
|
@ -29,6 +29,7 @@ nlohmann_json = dependency('nlohmann_json', version : ['>=3.10.4'], fallback : [
|
|||||||
|
|
||||||
src_files = files(
|
src_files = files(
|
||||||
'src/definitions.hpp',
|
'src/definitions.hpp',
|
||||||
|
'src/config.cpp', 'src/config.hpp',
|
||||||
'src/utils.cpp', 'src/utils.hpp',
|
'src/utils.cpp', 'src/utils.hpp',
|
||||||
'src/tui.cpp', 'src/tui.hpp',
|
'src/tui.cpp', 'src/tui.hpp',
|
||||||
'src/main.cpp',
|
'src/main.cpp',
|
||||||
|
24
src/config.cpp
Normal file
24
src/config.cpp
Normal 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
34
src/config.hpp
Normal 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
|
@ -1,5 +1,6 @@
|
|||||||
#include "definitions.hpp"
|
#include "definitions.hpp"
|
||||||
#include "tui.hpp"
|
#include "tui.hpp"
|
||||||
|
#include "config.hpp"
|
||||||
#include "utils.hpp"
|
#include "utils.hpp"
|
||||||
|
|
||||||
#include <regex>
|
#include <regex>
|
||||||
@ -19,6 +20,10 @@ int main() {
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
if (!Config::initialize()) {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
utils::id_system();
|
utils::id_system();
|
||||||
|
|
||||||
if (!utils::handle_connection()) {
|
if (!utils::handle_connection()) {
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
#include "utils.hpp"
|
#include "utils.hpp"
|
||||||
|
#include "config.hpp"
|
||||||
|
|
||||||
#include <sys/mount.h>
|
#include <sys/mount.h>
|
||||||
|
|
||||||
@ -11,13 +12,9 @@
|
|||||||
|
|
||||||
namespace fs = std::filesystem;
|
namespace fs = std::filesystem;
|
||||||
|
|
||||||
static std::string_view H_INIT{"openrc"};
|
namespace utils {
|
||||||
static std::string_view SYSTEM{"BIOS"};
|
|
||||||
static std::string_view NW_CMD{};
|
|
||||||
|
|
||||||
static constexpr int32_t CONNECTION_TIMEOUT = 15;
|
static constexpr int32_t CONNECTION_TIMEOUT = 15;
|
||||||
|
|
||||||
namespace utils {
|
|
||||||
bool is_connected() noexcept {
|
bool is_connected() noexcept {
|
||||||
return gethostbyname("google.com");
|
return gethostbyname("google.com");
|
||||||
}
|
}
|
||||||
@ -74,6 +71,9 @@ bool prompt_char(const char* prompt, const char* color, char* read) noexcept {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void id_system() noexcept {
|
void id_system() noexcept {
|
||||||
|
auto* config_instance = Config::instance();
|
||||||
|
auto& config_data = config_instance->data();
|
||||||
|
|
||||||
// Apple System Detection
|
// Apple System Detection
|
||||||
const auto& sys_vendor = utils::exec("cat /sys/class/dmi/id/sys_vendor");
|
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"))
|
if ((sys_vendor == "Apple Inc.\n") || (sys_vendor == "Apple Computer, Inc.\n"))
|
||||||
@ -92,17 +92,17 @@ void id_system() noexcept {
|
|||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
SYSTEM = "UEFI";
|
config_data["SYSTEM"] = "UEFI";
|
||||||
}
|
}
|
||||||
|
|
||||||
// init system
|
// init system
|
||||||
const auto& init_sys = utils::exec("cat /proc/1/comm");
|
const auto& init_sys = utils::exec("cat /proc/1/comm");
|
||||||
if (init_sys == "systemd\n")
|
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
|
// 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")
|
if (config_data["H_INIT"] == "systemd" && utils::exec("systemctl is-active NetworkManager") == "active\n")
|
||||||
NW_CMD = "nmtui";
|
config_data["NW_CMD"] = "nmtui";
|
||||||
}
|
}
|
||||||
|
|
||||||
bool handle_connection() noexcept {
|
bool handle_connection() noexcept {
|
||||||
|
Loading…
Reference in New Issue
Block a user