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"
Machine::Machine(s)
{
fetchDistro();
fetchHostname();
fetchUsername();
}
Machine::~Machine()
{
}
// -----------------------------------------
void Machine::fetchDistro()
{
Util::File osRelease("/etc/os-release");
std::istringstream stream(osRelease.data());
@@ -23,13 +36,19 @@ Machine::Machine(s)
m_distroIdLike = line.substr(8);
}
}
}
void Machine::fetchHostname()
{
char hostname[64] { 0 };
if (gethostname(hostname, 64) < 0) {
perror("\033[31;1mError:\033[0m gethostname");
}
m_hostname = hostname;
}
void Machine::fetchUsername()
{
// Get the username logged in on the controlling terminal of the process
char username[32] { 0 };
if (getlogin_r(username, 32) != 0) {
@@ -42,7 +61,3 @@ Machine::Machine(s)
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; }
private:
void fetchDistro();
void fetchHostname();
void fetchUsername();
std::string m_distroId;
std::string m_distroIdLike;
std::string m_hostname;
+60 -14
View File
@@ -8,6 +8,8 @@
#include <array>
#include <cstdio> // fprintf, printf, stderr
#include <cstdlib> // system
#include <filesystem> // exists
#include <optional>
#include <sstream> // istringstream
#include <string> // getline
#include <vector>
@@ -40,7 +42,13 @@ void Package::install()
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()) {
printf("%s", packages.c_str());
@@ -66,21 +74,57 @@ void Package::list(const std::vector<std::string>& targets)
void Package::store()
{
std::string packages = getPackageList();
auto packagesOrEmpty = getPackageList();
if (!packagesOrEmpty.has_value()) {
return;
}
auto packageFile = Util::File::create("./packages");
packageFile.clear();
packageFile.append(packages);
packageFile.append(packagesOrEmpty.value());
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)
{
distroDetect();
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 = "";
Util::System $;
@@ -94,18 +138,13 @@ void Package::installOrAurInstall(InstallType type)
// NOTE: Util::System does not support commands with newlines
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 {
command = "sudo pacman -Sy --needed " + repoList.output();
}
}
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
auto repoList = $("apt-cache search .").cut(1, ' ') | $("grep -xf ./packages");
command = "sudo apt install " + repoList.output();
@@ -119,6 +158,13 @@ void Package::installOrAurInstall(InstallType type)
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()
{
std::string id = Machine::the().distroId();
@@ -163,10 +209,9 @@ bool Package::distroDependencies()
dependencies.push_back({ "dpkg-query", "dpkg" });
}
// FIXME: Conglomerate which calls to save multiple external process creation.
Util::System $;
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());
return false;
}
@@ -175,10 +220,11 @@ bool Package::distroDependencies()
return true;
}
std::string Package::getPackageList()
std::optional<std::string> Package::getPackageList()
{
distroDetect();
distroDependencies();
if (!distroDetect() || !distroDependencies()) {
return {};
}
std::string packages;
+4 -1
View File
@@ -7,6 +7,7 @@
#ifndef PACKAGE_H
#define PACKAGE_H
#include <optional>
#include <string>
#include <vector>
@@ -32,11 +33,13 @@ public:
void store();
private:
std::optional<std::string> fetchAurHelper();
void installOrAurInstall(InstallType type);
bool findDependency(const std::string& search);
bool distroDetect();
bool distroDependencies();
std::string getPackageList();
std::optional<std::string> getPackageList();
Distro m_distro { Distro::Unsupported };
};