Skip to content

Commit

Permalink
Yes.
Browse files Browse the repository at this point in the history
  • Loading branch information
BurntRanch committed Apr 16, 2024
1 parent 7a7d1f5 commit 60f500e
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 82 deletions.
2 changes: 2 additions & 0 deletions include/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ blue = "#00aaff"
cyan = "#00ffff"
yellow = "#ffff00"
magenta = "#ff11cc"
gray = "#5a5a5a"
# custom DB colors such as extra, aur, etc.
# Unfortunatly toml doesn't support things like `aur = blue`
Expand All @@ -152,6 +153,7 @@ inline string blue = "#00aaff";
inline string cyan = "#00ffff";
inline string yellow = "#ffff00";
inline string magenta = "#ff11cc";
inline string gray = "#5a5a5a";

inline string configfile;
inline string themefile;
Expand Down
6 changes: 4 additions & 2 deletions include/taur.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef GIT_HPP
#define GIT_HPP

#include <alpm_list.h>
#include <fcntl.h>
#include <unistd.h>
#include <optional>
Expand All @@ -24,6 +25,7 @@ struct TaurPkg_t {
string desc;
float popularity = 1; // normal
vector<string> depends;
bool installed = false;
string db_name = "aur";
};

Expand All @@ -34,13 +36,13 @@ class TaurBackend {
// They are different because we found that fetching each AUR pkg is very time consuming, so we store the name and look it up later.
vector<TaurPkg_t> getPkgFromJson(rapidjson::Document& doc, bool useGit);
vector<TaurPkg_t> search_pac(string query);
vector<TaurPkg_t> search(string query, bool useGit);
vector<TaurPkg_t> search(string query, bool useGit);
bool download_tar(string url, string out_path);
bool download_git(string url, string out_path);
bool download_pkg(string url, string out_path);
optional<TaurPkg_t> fetch_pkg(string pkg, bool returnGit);
vector<TaurPkg_t> fetch_pkgs(vector<string> pkgs, bool returnGit);
bool remove_pkg(string name, bool searchForeignPackages);
bool remove_pkg(alpm_list_t *name, bool searchForeignPackages);
bool handle_aur_depends(TaurPkg_t pkg, path out_path, vector<TaurPkg_t> localPkgs, bool useGit);
bool install_pkg(string pkg_name, string extracted_path, bool onlydownload);
bool update_all_pkgs(string cacheDir, bool useGit);
Expand Down
4 changes: 3 additions & 1 deletion include/util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,10 @@ void log_printf(int log, string fmt, Args&&... args) {
// could use fmt::join, but doesn't work with vector<const char*>
template <typename T>
void print_vec(std::vector<T> vec) {
if(!config->debug)
if(!config->debug) {
std::cout << std::endl;
return;
}

for(auto& i : vec)
std::cout << i << " ";
Expand Down
140 changes: 74 additions & 66 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ void test_colors() {
"A customizable and lightweight AUR helper, designed to be simple but powerful.", // desc
10, // Popularity
vector<string>(), // depends
true,
"aur", // db_name
};

Expand Down Expand Up @@ -129,47 +130,46 @@ bool execPacman(int argc, char* argv[]) {
return false;
}

int installPkg(string pkgName) {
int installPkg(alpm_list_t *pkgNames) {
if (!pkgNames)
return false;

bool useGit = config->useGit;
string cacheDir = config->cacheDir;

vector<TaurPkg_t> pkgs = backend->search(pkgName, useGit);
bool returnStatus = true;

if (pkgs.empty() && !op.op_s_upgrade) {
log_printf(LOG_WARN, "No results found, Exiting!\n");
return false;
} else if (pkgs.empty()) {
if (!config->aurOnly) {
vector<const char *> cmd = {config->sudo.c_str(), "pacman", "-S"};
if(op.op_s_sync)
cmd.push_back("-y");
if(op.op_s_upgrade)
cmd.push_back("-u");

if (!taur_exec(cmd))
return false;
for (; pkgNames; pkgNames = pkgNames->next) {
string pkgName = string((char *)(pkgNames->data));
vector<TaurPkg_t> pkgs = backend->search(pkgName, useGit);

if (pkgs.empty()) {
log_printf(LOG_WARN, "No results found!\n");
returnStatus = false;
continue;
}
return backend->update_all_pkgs(cacheDir, useGit);
}

// ./taur -Ss -- list only, don't install.
if (op.op_s_search) {
for (size_t i = 0; i < pkgs.size(); i++)
printPkgInfo(pkgs[i], pkgs[i].db_name);
return true;
}
// ./taur -Ss -- list only, don't install.
if (op.op_s_search) {
for (size_t i = 0; i < pkgs.size(); i++)
printPkgInfo(pkgs[i], pkgs[i].db_name);
returnStatus = false;
continue;
}

optional<vector<TaurPkg_t>> oSelectedPkgs = askUserForPkg(pkgs, *backend, useGit);
optional<vector<TaurPkg_t>> oSelectedPkgs = askUserForPkg(pkgs, *backend, useGit);

if (!oSelectedPkgs)
return false;
if (!oSelectedPkgs) {
returnStatus = false;
continue;
}

vector<TaurPkg_t> selectedPkgs = oSelectedPkgs.value();
vector<TaurPkg_t> selectedPkgs = oSelectedPkgs.value();

for (size_t i = 0; i < selectedPkgs.size(); i++) {
TaurPkg_t pkg = selectedPkgs[i];
for (size_t i = 0; i < selectedPkgs.size(); i++) {
TaurPkg_t pkg = selectedPkgs[i];

string url = pkg.url;
string url = pkg.url;

if (url == "") {
vector<const char *> cmd = {config->sudo.c_str(), "pacman", "-S"};
Expand All @@ -180,58 +180,66 @@ int installPkg(string pkgName) {

cmd.push_back(pkg.name.c_str());

cmd.push_back(NULL);

if (taur_exec(cmd))
if (taur_exec(cmd, false))
continue;
else {
returnStatus = false;
continue;
else
return false;
}
}

string filename = path(cacheDir) / url.substr(url.rfind("/") + 1);
string filename = path(cacheDir) / url.substr(url.rfind("/") + 1);

if (useGit)
filename = filename.substr(0, filename.rfind(".git"));
if (useGit)
filename = filename.substr(0, filename.rfind(".git"));

bool stat = backend->download_pkg(url, filename);
bool stat = backend->download_pkg(url, filename);

if (!stat) {
log_printf(LOG_ERROR, "An error has occurred and we could not download your package.\n");
return false;
}

if (!useGit){
stat = backend->handle_aur_depends(pkg, cacheDir, backend->get_all_local_pkgs(true), useGit);
stat = backend->install_pkg(pkg.name, filename.substr(0, filename.rfind(".tar.gz")), false);
}
else {
stat = backend->handle_aur_depends(pkg, cacheDir, backend->get_all_local_pkgs(true), useGit);
stat = backend->install_pkg(pkg.name, filename, false);
}
if (!stat) {
log_printf(LOG_ERROR, "An error has occurred and we could not download your package.\n");
returnStatus = false;
continue;
}

if (!useGit){
stat = backend->handle_aur_depends(pkg, cacheDir, backend->get_all_local_pkgs(true), useGit);
stat = backend->install_pkg(pkg.name, filename.substr(0, filename.rfind(".tar.gz")), false);
}
else {
stat = backend->handle_aur_depends(pkg, cacheDir, backend->get_all_local_pkgs(true), useGit);
stat = backend->install_pkg(pkg.name, filename, false);
}

if (!stat) {
log_printf(LOG_ERROR, "Building your package has failed.\n");
returnStatus = false;
continue;
}

if (!stat) {
log_printf(LOG_ERROR, "Building your package has failed.\n");
return false;
}
log_printf(LOG_DEBUG, "Installing {}.\n", pkg.name);
if (!taur_exec({config->sudo.c_str(), "pacman", "-U", built_pkg.c_str()}, false)) {
log_printf(LOG_ERROR, "Failed to install {}.\n", pkg.name);
returnStatus = false;
continue;
}

log_printf(LOG_DEBUG, "Installing {}.\n", pkg.name);
if (!taur_exec({config->sudo.c_str(), "pacman", "-U", built_pkg.c_str()})) {
log_printf(LOG_ERROR, "Failed to install {}.\n", pkg.name);
return false;
}

}

if (op.op_s_upgrade) {
log_printf(LOG_INFO, "-u flag specified, upgrading AUR packages.\n");
return backend->update_all_pkgs(cacheDir, useGit);
return backend->update_all_pkgs(cacheDir, useGit) && returnStatus;
}

return true;
return returnStatus;
}

bool removePkg(string pkgName) {
return backend->remove_pkg(pkgName, config->aurOnly);
bool removePkg(alpm_list_t *pkgNames) {
if (!pkgNames)
return false;

return backend->remove_pkg(pkgNames, config->aurOnly);
}

bool updateAll() {
Expand Down Expand Up @@ -432,9 +440,9 @@ int main(int argc, char* argv[]) {

switch (op.op) {
case OP_SYNC:
return (installPkg(taur_targets ? string((const char *)(taur_targets->data)) : "")) ? 0 : 1;
return installPkg(taur_targets.get()) ? 0 : 1;
case OP_REM:
return (removePkg(taur_targets ? string((const char *)(taur_targets->data)) : "")) ? 0 : 1;
return removePkg(taur_targets.get()) ? 0 : 1;
case OP_QUERY:
return (queryPkgs()) ? 0 : 1;
case OP_SYSUPGRADE:
Expand Down
17 changes: 8 additions & 9 deletions src/taur.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ TaurPkg_t parsePkg(rapidjson::Value& pkgJson, bool returnGit = false) {
getUrl(pkgJson, returnGit), // url
pkgJson["Description"].IsString() ? pkgJson["Description"].GetString() : "", // description
pkgJson["Popularity"].GetFloat(), // popularity
depends}; // depends
depends, // depends
alpm_db_get_pkg(alpm_get_localdb(config->handle), pkgJson["Name"].GetString()) != nullptr,
};

return out;
}
Expand Down Expand Up @@ -130,15 +132,10 @@ vector<TaurPkg_t> TaurBackend::fetch_pkgs(vector<string> pkgs, bool returnGit) {
return out;
}

bool TaurBackend::remove_pkg(string pkgName, bool aurOnly) {
alpm_list_smart_pointer list(alpm_list_add(nullptr, (void *)((".*" + pkgName + ".*").c_str())), alpm_list_free);
bool TaurBackend::remove_pkg(alpm_list_t *pkgQueries, bool aurOnly) {
alpm_list_t *temp_ret = nullptr;

alpm_pkg_t *potential_pkg = alpm_db_get_pkg(alpm_get_localdb(config.handle), pkgName.c_str());

if (potential_pkg)
temp_ret = alpm_list_add(temp_ret, potential_pkg);
else if (alpm_db_search(alpm_get_localdb(config.handle), list.get(), &temp_ret) != 0)
if (alpm_db_search(alpm_get_localdb(config.handle), pkgQueries, &temp_ret) != 0)
return false;

alpm_list_smart_pointer ret(temp_ret, alpm_list_free);
Expand Down Expand Up @@ -506,6 +503,7 @@ bool TaurBackend::update_all_pkgs(string cacheDir, bool useGit) {
}

// all AUR local packages
// returns a barebones TaurPkg_t structure, with no description/depends list
vector<TaurPkg_t> TaurBackend::get_all_local_pkgs(bool aurOnly) {
vector<alpm_pkg_t *> pkgs;

Expand All @@ -522,7 +520,7 @@ vector<TaurPkg_t> TaurBackend::get_all_local_pkgs(bool aurOnly) {
vector<TaurPkg_t> out;
for (size_t i = 0; i < pkgs.size(); i++) {
alpm_pkg_t *pkg = pkgs[i];
out.push_back({alpm_pkg_get_name(pkg), alpm_pkg_get_version(pkg), "https://aur.archlinux.org/" + cpr::util::urlEncode(alpm_pkg_get_name(pkg)) + ".git"});
out.push_back({alpm_pkg_get_name(pkg), alpm_pkg_get_version(pkg), "https://aur.archlinux.org/" + cpr::util::urlEncode(alpm_pkg_get_name(pkg)) + ".git", "", 1, {}, true});
}

return out;
Expand Down Expand Up @@ -573,6 +571,7 @@ vector<TaurPkg_t> TaurBackend::search_pac(string query) {
string(alpm_pkg_get_desc(pkg)), // desc
100, // system packages are very trustable
vector<string>(), // depends
alpm_db_get_pkg(alpm_get_localdb(config.handle), alpm_pkg_get_name(pkg)) != nullptr,
string(alpm_db_get_name(alpm_pkg_get_db(pkg))), // db_name
};

Expand Down
10 changes: 6 additions & 4 deletions src/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ bool taur_exec(vector<const char*> cmd, bool exitOnFailure) {
if (WIFEXITED(status) && WEXITSTATUS(status) == 0)
return true;
else {
log_printf(LOG_ERROR, "Failed to execute the command: ");
log_printf(LOG_ERROR, "Failed to execute command: ");
print_vec(cmd); // fmt::join() doesn't work with vector<const char*>
if (exitOnFailure)
exit(-1);
Expand Down Expand Up @@ -312,7 +312,11 @@ void printPkgInfo(TaurPkg_t &pkg, string db_name, int index) {
fmt::print(getColorFromDBName(db_name), "{}/", db_name);
fmt::print(fmt::emphasis::bold, "{} ", pkg.name);
fmt::print(BOLD_TEXT(config->getThemeValue("version", green)), "{} ", pkg.version);
fmt::println(fmt::fg(config->getThemeValue("popularity", cyan)), " Popularity: {} ({})", pkg.popularity, getTitleForPopularity(pkg.popularity));
fmt::print(fmt::fg(config->getThemeValue("popularity", cyan)), " Popularity: {} ({}) ", pkg.popularity, getTitleForPopularity(pkg.popularity));
if (pkg.installed)
fmt::println(fmt::fg(config->getThemeValue("gray", gray)), "[Installed]");
else
fmt::println("");
fmt::println(" {}", pkg.desc);
}

Expand Down Expand Up @@ -343,8 +347,6 @@ optional<vector<TaurPkg_t>> askUserForPkg(vector<TaurPkg_t> pkgs, TaurBackend& b

fmt::print("Choose a package to download: ");
std::getline(std::cin, input);

input.end() = input.end()-1; // remove leading newline
} while (!is_number(input, true));

vector<string> indices = split(input, ' ');
Expand Down

0 comments on commit 60f500e

Please sign in to comment.