Compare commits

...
4 Commits
4 changed files with 89 additions and 21 deletions
+19 -4
View File
@@ -12,6 +12,19 @@
#include "util/file.h" #include "util/file.h"
Machine::Machine(s) Machine::Machine(s)
{
fetchDistro();
fetchHostname();
fetchUsername();
}
Machine::~Machine()
{
}
// -----------------------------------------
void Machine::fetchDistro()
{ {
Util::File osRelease("/etc/os-release"); Util::File osRelease("/etc/os-release");
std::istringstream stream(osRelease.data()); std::istringstream stream(osRelease.data());
@@ -23,13 +36,19 @@ Machine::Machine(s)
m_distroIdLike = line.substr(8); m_distroIdLike = line.substr(8);
} }
} }
}
void Machine::fetchHostname()
{
char hostname[64] { 0 }; char hostname[64] { 0 };
if (gethostname(hostname, 64) < 0) { if (gethostname(hostname, 64) < 0) {
perror("\033[31;1mError:\033[0m gethostname"); perror("\033[31;1mError:\033[0m gethostname");
} }
m_hostname = hostname; m_hostname = hostname;
}
void Machine::fetchUsername()
{
// Get the username logged in on the controlling terminal of the process // Get the username logged in on the controlling terminal of the process
char username[32] { 0 }; char username[32] { 0 };
if (getlogin_r(username, 32) != 0) { if (getlogin_r(username, 32) != 0) {
@@ -42,7 +61,3 @@ Machine::Machine(s)
perror("\033[31;1mError:\033[0m getpwnam"); perror("\033[31;1mError:\033[0m getpwnam");
} }
} }
Machine::~Machine()
{
}
+4
View File
@@ -27,6 +27,10 @@ public:
uint32_t gid() { return m_passwd->pw_gid; } uint32_t gid() { return m_passwd->pw_gid; }
private: private:
void fetchDistro();
void fetchHostname();
void fetchUsername();
std::string m_distroId; std::string m_distroId;
std::string m_distroIdLike; std::string m_distroIdLike;
std::string m_hostname; std::string m_hostname;
+62 -16
View File
@@ -6,8 +6,10 @@
#include <algorithm> // replace #include <algorithm> // replace
#include <array> #include <array>
#include <cstdio> // fprintf, printf, stderr #include <cstdio> // fprintf, printf, stderr
#include <cstdlib> // system #include <cstdlib> // system
#include <filesystem> // exists
#include <optional>
#include <sstream> // istringstream #include <sstream> // istringstream
#include <string> // getline #include <string> // getline
#include <vector> #include <vector>
@@ -40,7 +42,13 @@ void Package::install()
void Package::list(const std::vector<std::string>& targets) void Package::list(const std::vector<std::string>& targets)
{ {
std::string packages = getPackageList(); auto packagesOrEmpty = getPackageList();
if (!packagesOrEmpty.has_value()) {
return;
}
std::string packages = packagesOrEmpty.value();
if (targets.empty()) { if (targets.empty()) {
printf("%s", packages.c_str()); printf("%s", packages.c_str());
@@ -66,21 +74,57 @@ void Package::list(const std::vector<std::string>& targets)
void Package::store() void Package::store()
{ {
std::string packages = getPackageList(); auto packagesOrEmpty = getPackageList();
if (!packagesOrEmpty.has_value()) {
return;
}
auto packageFile = Util::File::create("./packages"); auto packageFile = Util::File::create("./packages");
packageFile.clear(); packageFile.clear();
packageFile.append(packages); packageFile.append(packagesOrEmpty.value());
packageFile.flush(); packageFile.flush();
} }
// ----------------------------------------- // -----------------------------------------
std::optional<std::string> Package::fetchAurHelper()
{
const std::string helpers[] = {
"yay",
"paru",
"trizen",
};
for(const auto& helper : helpers) {
if (findDependency(helper)) {
return { helper };
}
}
return {};
}
void Package::installOrAurInstall(InstallType type) void Package::installOrAurInstall(InstallType type)
{ {
distroDetect(); distroDetect();
distroDependencies(); distroDependencies();
std::optional<std::string> aurHelper;
if (type == InstallType::AurInstall) {
if (m_distro == Distro::Arch) {
aurHelper = fetchAurHelper();
if (!aurHelper.has_value()) {
fprintf(stderr, "\033[31;1mPackage:\033[0m no supported AUR helper found\n");
return;
}
}
else {
fprintf(stderr, "\033[31;1mPackage:\033[0m AUR is not supported on this distribution\n");
return;
}
}
std::string command = ""; std::string command = "";
Util::System $; Util::System $;
@@ -94,18 +138,13 @@ void Package::installOrAurInstall(InstallType type)
// NOTE: Util::System does not support commands with newlines // NOTE: Util::System does not support commands with newlines
auto aurList = Util::Shell()("cat ./packages | " + aurCommand); auto aurList = Util::Shell()("cat ./packages | " + aurCommand);
command = "trizen -Sy --devel --needed --noconfirm " + aurList.output(); command = aurHelper.value() + " -Sy --devel --needed --noconfirm " + aurList.output();
} }
else { else {
command = "sudo pacman -Sy --needed " + repoList.output(); command = "sudo pacman -Sy --needed " + repoList.output();
} }
} }
else if (m_distro == Distro::Debian) { else if (m_distro == Distro::Debian) {
if (type == InstallType::AurInstall) {
fprintf(stderr, "\033[31;1mPackage:\033[0m AUR is not supported on this distribution\n");
return;
}
// Grab everything off enabled official repositories that is in the list // Grab everything off enabled official repositories that is in the list
auto repoList = $("apt-cache search .").cut(1, ' ') | $("grep -xf ./packages"); auto repoList = $("apt-cache search .").cut(1, ' ') | $("grep -xf ./packages");
command = "sudo apt install " + repoList.output(); command = "sudo apt install " + repoList.output();
@@ -119,6 +158,13 @@ void Package::installOrAurInstall(InstallType type)
system(command.c_str()); system(command.c_str());
} }
bool Package::findDependency(const std::string& search)
{
return std::filesystem::exists("/bin/" + search)
|| std::filesystem::exists("/usr/bin/" + search)
|| std::filesystem::exists("/usr/local/bin/" + search);
}
bool Package::distroDetect() bool Package::distroDetect()
{ {
std::string id = Machine::the().distroId(); std::string id = Machine::the().distroId();
@@ -163,10 +209,9 @@ bool Package::distroDependencies()
dependencies.push_back({ "dpkg-query", "dpkg" }); dependencies.push_back({ "dpkg-query", "dpkg" });
} }
// FIXME: Conglomerate which calls to save multiple external process creation.
Util::System $; Util::System $;
for (const auto& dependency : dependencies) { for (const auto& dependency : dependencies) {
if ($("which " + dependency.at(0))().status() > 0) { if (!findDependency(dependency.at(0))) {
fprintf(stderr, "\033[31;1mPackage:\033[0m required dependency '%s' is missing\n", dependency.at(1).c_str()); fprintf(stderr, "\033[31;1mPackage:\033[0m required dependency '%s' is missing\n", dependency.at(1).c_str());
return false; return false;
} }
@@ -175,10 +220,11 @@ bool Package::distroDependencies()
return true; return true;
} }
std::string Package::getPackageList() std::optional<std::string> Package::getPackageList()
{ {
distroDetect(); if (!distroDetect() || !distroDependencies()) {
distroDependencies(); return {};
}
std::string packages; std::string packages;
+4 -1
View File
@@ -7,6 +7,7 @@
#ifndef PACKAGE_H #ifndef PACKAGE_H
#define PACKAGE_H #define PACKAGE_H
#include <optional>
#include <string> #include <string>
#include <vector> #include <vector>
@@ -32,11 +33,13 @@ public:
void store(); void store();
private: private:
std::optional<std::string> fetchAurHelper();
void installOrAurInstall(InstallType type); void installOrAurInstall(InstallType type);
bool findDependency(const std::string& search);
bool distroDetect(); bool distroDetect();
bool distroDependencies(); bool distroDependencies();
std::string getPackageList(); std::optional<std::string> getPackageList();
Distro m_distro { Distro::Unsupported }; Distro m_distro { Distro::Unsupported };
}; };