From f53d0b9686adf37aa865844142c4fad36c594568 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micka=C3=ABl=20Carl?= Date: Tue, 28 Oct 2025 23:24:24 +0000 Subject: [PATCH] Add a ListInstalled function I've needed at times to list the packages that are currently listed on a system. Search("") is equivalent to that, but since we have List() which is equivalent to Search("*"), I figured this might be handy to have as well. --- apt.go | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/apt.go b/apt.go index 5f0c591..76a704f 100644 --- a/apt.go +++ b/apt.go @@ -44,10 +44,23 @@ func List() ([]*Package, error) { return Search("*") } +// ListInstalled returns a list of packages installed on the system. +func ListInstalled() ([]*Package, error) { + return Search("") +} + // Search list packages available in the system that match the search // pattern func Search(pattern string) ([]*Package, error) { - cmd := exec.Command("dpkg-query", "-W", "-f=${Package}\t${Architecture}\t${db:Status-Status}\t${Version}\t${Installed-Size}\t${Binary:summary}\n", pattern) + args := []string{ + "-W", + "-f=${Package}\t${Architecture}\t${db:Status-Status}\t${Version}\t${Installed-Size}\t${Binary:summary}\n", + } + if pattern != "" { + args = append(args, pattern) + } + + cmd := exec.Command("dpkg-query", args...) out, err := cmd.CombinedOutput() if err != nil {