Skip to content

Commit f53d0b9

Browse files
committed
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.
1 parent 0d7233b commit f53d0b9

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

apt.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,23 @@ func List() ([]*Package, error) {
4444
return Search("*")
4545
}
4646

47+
// ListInstalled returns a list of packages installed on the system.
48+
func ListInstalled() ([]*Package, error) {
49+
return Search("")
50+
}
51+
4752
// Search list packages available in the system that match the search
4853
// pattern
4954
func Search(pattern string) ([]*Package, error) {
50-
cmd := exec.Command("dpkg-query", "-W", "-f=${Package}\t${Architecture}\t${db:Status-Status}\t${Version}\t${Installed-Size}\t${Binary:summary}\n", pattern)
55+
args := []string{
56+
"-W",
57+
"-f=${Package}\t${Architecture}\t${db:Status-Status}\t${Version}\t${Installed-Size}\t${Binary:summary}\n",
58+
}
59+
if pattern != "" {
60+
args = append(args, pattern)
61+
}
62+
63+
cmd := exec.Command("dpkg-query", args...)
5164

5265
out, err := cmd.CombinedOutput()
5366
if err != nil {

0 commit comments

Comments
 (0)