From aa702b79b954f8bcacb618a9d4fbf37342d619f4 Mon Sep 17 00:00:00 2001 From: Riyyi Date: Sun, 9 Jan 2022 15:47:03 +0100 Subject: [PATCH] Manager: Move package list grabbing to separate function --- src/package.cpp | 65 ++++++++++++++++++++++++++++++------------------- src/package.h | 11 ++++++--- 2 files changed, 48 insertions(+), 28 deletions(-) diff --git a/src/package.cpp b/src/package.cpp index e1294b4..6319253 100644 --- a/src/package.cpp +++ b/src/package.cpp @@ -14,6 +14,8 @@ #include "util/file.h" #include "util/system.h" +std::string Package::m_hostname; + Package::Package() { } @@ -24,38 +26,17 @@ Package::~Package() // ----------------------------------------- -void Package::aurInstall(const std::vector& targets) +void Package::aurInstall() { } -void Package::install(const std::vector& targets) +void Package::install() { } void Package::list(const std::vector& targets) { - distroDetect(); - distroDependencies(); - - std::string packages; - - Util::System $; - if (m_distro == Distro::Arch) { - auto basePackages = $("pactree -u base").tail(2, true); - auto develPackages = $("pacman -Qqg base-devel"); - auto filterList = (basePackages + develPackages).sort(true); - auto packageList = ($("pacman -Qqe") | $("grep -xv " + filterList.output())).sort(); - packages = packageList.output(); - } - else if (m_distro == Distro::Debian) { - auto installedList = $("dpkg-query --show --showformat=${Package}\\t${Priority}\\n"); - auto filterList = (installedList | $("grep -E required|important|standard")).cut(1); - installedList = installedList.cut(1); - auto installedManuallyList = $("awk '/Commandline:.* install / && !/APT::/ { print $NF }' /var/log/apt/history.log"); - installedManuallyList = (installedManuallyList + $("apt-mark showmanual")).sort(true); - auto packageList = installedManuallyList | $("grep -x " + installedList.output()) | $("grep -xv " + filterList.output()); - packages = packageList.output(); - } + std::string packages = getPackageList(); if (targets.empty()) { printf("%s", packages.c_str()); @@ -79,8 +60,14 @@ void Package::list(const std::vector& targets) printf("%s", packages.c_str()); } -void Package::store(const std::vector& targets) +void Package::store() { + std::string packages = getPackageList(); + + auto packageFile = Util::File("./packages"); + packageFile.clear(); + packageFile.append(packages); + packageFile.flush(); } // ----------------------------------------- @@ -152,3 +139,31 @@ bool Package::distroDependencies() return true; } + +std::string Package::getPackageList() +{ + distroDetect(); + distroDependencies(); + + std::string packages; + + Util::System $; + if (m_distro == Distro::Arch) { + auto basePackages = $("pactree -u base").tail(2, true); + auto develPackages = $("pacman -Qqg base-devel"); + auto filterList = (basePackages + develPackages).sort(true); + auto packageList = ($("pacman -Qqe") | $("grep -xv " + filterList.output())).sort(); + packages = packageList.output(); + } + else if (m_distro == Distro::Debian) { + auto installedList = $("dpkg-query --show --showformat=${Package}\\t${Priority}\\n"); + auto filterList = (installedList | $("grep -E required|important|standard")).cut(1); + installedList = installedList.cut(1); + auto installedManuallyList = $("awk '/Commandline:.* install / && !/APT::/ { print $NF }' /var/log/apt/history.log"); + installedManuallyList = (installedManuallyList + $("apt-mark showmanual")).sort(true); + auto packageList = installedManuallyList | $("grep -x " + installedList.output()) | $("grep -xv " + filterList.output()); + packages = packageList.output(); + } + + return packages; +} diff --git a/src/package.h b/src/package.h index fc749a0..411aef7 100644 --- a/src/package.h +++ b/src/package.h @@ -21,14 +21,19 @@ public: Debian, }; - void aurInstall(const std::vector& targets = {}); - void install(const std::vector& targets = {}); + void aurInstall(); + void install(); void list(const std::vector& targets = {}); - void store(const std::vector& targets = {}); + void store(); + + static void setHostname(const std::string& hostname) { m_hostname = hostname; } private: bool distroDetect(); bool distroDependencies(); + std::string getPackageList(); + + static std::string m_hostname; Distro m_distro { Distro::Unsupported }; };