mirror of
https://github.com/CachyOS/New-Cli-Installer.git
synced 2025-01-23 14:32:22 +08:00
🚧 add some features
run only with privileges identify the system we are running on
This commit is contained in:
parent
76a926f1af
commit
ae8737ccdb
@ -1,28 +0,0 @@
|
|||||||
#ifndef DEFINITIONS_H
|
|
||||||
#define DEFINITIONS_H
|
|
||||||
|
|
||||||
#define RESET "\033[0m"
|
|
||||||
#define BLACK "\033[30m" /* Black */
|
|
||||||
#define RED "\033[31m" /* Red */
|
|
||||||
#define GREEN "\033[32m" /* Green */
|
|
||||||
#define YELLOW "\033[33m" /* Yellow */
|
|
||||||
#define BLUE "\033[34m" /* Blue */
|
|
||||||
#define MAGENTA "\033[35m" /* Magenta */
|
|
||||||
#define CYAN "\033[36m" /* Cyan */
|
|
||||||
#define WHITE "\033[37m" /* White */
|
|
||||||
#define BOLDBLACK "\033[1m\033[30m" /* Bold Black */
|
|
||||||
#define BOLDRED "\033[1m\033[31m" /* Bold Red */
|
|
||||||
#define BOLDGREEN "\033[1m\033[32m" /* Bold Green */
|
|
||||||
#define BOLDYELLOW "\033[1m\033[33m" /* Bold Yellow */
|
|
||||||
#define BOLDBLUE "\033[1m\033[34m" /* Bold Blue */
|
|
||||||
#define BOLDMAGENTA "\033[1m\033[35m" /* Bold Magenta */
|
|
||||||
#define BOLDCYAN "\033[1m\033[36m" /* Bold Cyan */
|
|
||||||
#define BOLDWHITE "\033[1m\033[37m" /* Bold White */
|
|
||||||
|
|
||||||
#define output(value) cout << value << endl
|
|
||||||
#define error(errorString) cout << RED << errorString << RESET << endl
|
|
||||||
#define warning(warningString) cout << YELLOW << warningString << RESET << endl
|
|
||||||
#define info(infoString) cout << CYAN << infoString << RESET << endl
|
|
||||||
#define success(successString) cout << GREEN << successString << RESET << endl
|
|
||||||
|
|
||||||
#endif
|
|
47
src/main.cpp
47
src/main.cpp
@ -1,32 +1,51 @@
|
|||||||
#include "definitions.hpp"
|
#include "definitions.hpp"
|
||||||
#include "utils.hpp"
|
#include "utils.hpp"
|
||||||
|
|
||||||
|
#include <regex>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
|
|
||||||
static constexpr int32_t SLEEP_TIMEOUT = 15;
|
static constexpr int32_t SLEEP_TIMEOUT = 15;
|
||||||
|
|
||||||
|
void show_iwctl() {
|
||||||
|
info("\nInstructions to connect to wifi using iwclt:\n");
|
||||||
|
info("1 - Find your wifi device name ex: wlan0\n");
|
||||||
|
info("2 - type `station wlan0 scan`, and wait couple seconds\n");
|
||||||
|
info("3 - type `station wlan0 get-networks` (find your wifi Network name ex. my_wifi)\n");
|
||||||
|
info("4 - type `station wlan0 connect my_wifi` (don't forget to press TAB for auto completion!\n");
|
||||||
|
info("5 - type `station wlan0 show` (status should be connected)\n");
|
||||||
|
info("6 - type `exit`\n");
|
||||||
|
while (utils::prompt_char("Press a key to continue...", CYAN)) {
|
||||||
|
utils::exec("iwctl");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
|
const auto& tty = utils::exec("tty");
|
||||||
|
const std::regex tty_regex("/dev/tty[0-9]*");
|
||||||
|
if (std::regex_search(tty, tty_regex)) {
|
||||||
|
utils::exec("setterm -blank 0 -powersave off");
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifdef NDEBUG
|
||||||
|
if (!utils::check_root()) {
|
||||||
|
std::this_thread::sleep_for(std::chrono::seconds(3));
|
||||||
|
utils::clear_screen();
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
utils::id_system();
|
||||||
if (!utils::is_connected()) {
|
if (!utils::is_connected()) {
|
||||||
warning("It seems you are not connected, waiting for 15s before retrying...\n");
|
warning("It seems you are not connected, waiting for 15s before trying again...\n");
|
||||||
std::this_thread::sleep_for(std::chrono::seconds(SLEEP_TIMEOUT));
|
std::this_thread::sleep_for(std::chrono::seconds(SLEEP_TIMEOUT));
|
||||||
|
|
||||||
if (!utils::is_connected()) {
|
if (!utils::is_connected()) {
|
||||||
char type = '\0';
|
char type = '\0';
|
||||||
|
|
||||||
while (utils::prompt_char("An internet connection could not be detected, do you want to connect to a wifi network? [y/n]", type, RED)) {
|
while (utils::prompt_char("An internet connection could not be detected, do you want to connect to a wifi network? [y/n]", RED, &type)) {
|
||||||
if (type != 'n') {
|
if (type != 'n') {
|
||||||
|
show_iwctl();
|
||||||
info("\nInstructions to connect to wifi using iwclt:\n");
|
|
||||||
info("1 - Find your wifi device name ex: wlan0\n");
|
|
||||||
info("2 - type `station wlan0 scan`, and wait couple seconds\n");
|
|
||||||
info("3 - type `station wlan0 get-networks` (find your wifi Network name ex. my_wifi)\n");
|
|
||||||
info("4 - type `station wlan0 connect my_wifi` (don't forget to press TAB for auto completion!\n");
|
|
||||||
info("5 - type `station wlan0 show` (status should be connected)\n");
|
|
||||||
info("6 - type `exit`\n");
|
|
||||||
while (utils::prompt_char("Press a key to continue...", type, CYAN)) {
|
|
||||||
utils::exec("iwctl");
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
@ -1,15 +1,33 @@
|
|||||||
#include "utils.hpp"
|
#include "utils.hpp"
|
||||||
|
|
||||||
|
#include <sys/mount.h>
|
||||||
|
|
||||||
|
#include <filesystem>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
#include <netdb.h>
|
#include <netdb.h>
|
||||||
|
|
||||||
|
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 {
|
namespace utils {
|
||||||
bool is_connected() noexcept {
|
bool is_connected() noexcept {
|
||||||
return gethostbyname("google.com");
|
return gethostbyname("google.com");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool check_root() noexcept {
|
||||||
|
return (utils::exec("whoami") == "root\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
void clear_screen() noexcept {
|
||||||
|
static constexpr auto CLEAR_SCREEN_ANSI = "\033[1;1H\033[2J";
|
||||||
|
write(STDOUT_FILENO, CLEAR_SCREEN_ANSI, 11);
|
||||||
|
}
|
||||||
|
|
||||||
std::string exec(const std::string_view& command) noexcept {
|
std::string exec(const std::string_view& command) noexcept {
|
||||||
char buffer[128];
|
char buffer[128];
|
||||||
std::string result;
|
std::string result;
|
||||||
@ -31,20 +49,57 @@ std::string exec(const std::string_view& command) noexcept {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool prompt_char(const char* prompt, char& read, const char* color) noexcept {
|
bool prompt_char(const char* prompt, const char* color, char* read) noexcept {
|
||||||
fmt::print("{}{}{}\n", color, prompt, RESET);
|
fmt::print("{}{}{}\n", color, prompt, RESET);
|
||||||
|
|
||||||
std::string tmp;
|
std::string tmp;
|
||||||
if (std::getline(std::cin, tmp)) {
|
if (std::getline(std::cin, tmp)) {
|
||||||
|
char read_char{};
|
||||||
if (tmp.length() == 1) {
|
if (tmp.length() == 1) {
|
||||||
read = tmp[0];
|
read_char = tmp[0];
|
||||||
} else {
|
} else {
|
||||||
read = '\0';
|
read_char = '\0';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (read != nullptr)
|
||||||
|
*read = read_char;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void id_system() noexcept {
|
||||||
|
// 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"))
|
||||||
|
utils::exec("modprobe -r -q efivars || true"); // if MAC
|
||||||
|
else
|
||||||
|
utils::exec("modprobe -q efivarfs"); // all others
|
||||||
|
|
||||||
|
// BIOS or UEFI Detection
|
||||||
|
static constexpr auto efi_path = "/sys/firmware/efi/";
|
||||||
|
if (fs::exists(efi_path) && fs::is_directory(efi_path)) {
|
||||||
|
// Mount efivarfs if it is not already mounted
|
||||||
|
const auto& mount_out = utils::exec("mount | grep /sys/firmware/efi/efivars");
|
||||||
|
if (mount_out == "\n") {
|
||||||
|
if (mount("efivarfs", "/sys/firmware/efi/efivars", "efivarfs", 0, "") != 0) {
|
||||||
|
perror("utils::id_system");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
SYSTEM = "UEFI";
|
||||||
|
}
|
||||||
|
|
||||||
|
// init system
|
||||||
|
const auto& init_sys = utils::exec("cat /proc/1/comm");
|
||||||
|
if (init_sys == "systemd\n")
|
||||||
|
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";
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace utils
|
} // namespace utils
|
||||||
|
21
src/utils.h
21
src/utils.h
@ -1,21 +0,0 @@
|
|||||||
#ifndef UTILS_H
|
|
||||||
#define UTILS_H
|
|
||||||
|
|
||||||
#include "definitions.h"
|
|
||||||
#include <iostream>
|
|
||||||
|
|
||||||
using namespace std;
|
|
||||||
|
|
||||||
|
|
||||||
class Utils
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
static void printBanner();
|
|
||||||
static bool isConnected();
|
|
||||||
static std::string exec(const string &command);
|
|
||||||
static bool promptForChar(const char *prompt, char &read, const char *colour = RESET);
|
|
||||||
|
|
||||||
private:
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif//UTILS_H
|
|
@ -8,9 +8,12 @@
|
|||||||
namespace utils {
|
namespace utils {
|
||||||
void print_banner() noexcept;
|
void print_banner() noexcept;
|
||||||
bool is_connected() noexcept;
|
bool is_connected() noexcept;
|
||||||
bool prompt_char(const char* prompt, char& read, const char* color = RESET) noexcept;
|
bool prompt_char(const char* prompt, const char* color = RESET, char* read = nullptr) noexcept;
|
||||||
|
void clear_screen() noexcept;
|
||||||
|
|
||||||
std::string exec(const std::string_view& command) noexcept;
|
auto exec(const std::string_view& command) noexcept -> std::string;
|
||||||
|
[[nodiscard]] bool check_root() noexcept;
|
||||||
|
void id_system() noexcept;
|
||||||
} // namespace utils
|
} // namespace utils
|
||||||
|
|
||||||
#endif // UTILS_HPP
|
#endif // UTILS_HPP
|
||||||
|
Loading…
Reference in New Issue
Block a user