summaryrefslogtreecommitdiff
path: root/dpkgQuery.go
diff options
context:
space:
mode:
authorJeff Carr <[email protected]>2024-11-15 01:22:47 -0600
committerJeff Carr <[email protected]>2024-11-15 01:22:47 -0600
commit6c920e0925c5b9db4354c31a693140adf024d88b (patch)
tree670d66ed9e8fd6dab6efa092e6775513eff5518a /dpkgQuery.go
stub'd outv0.0.1
Diffstat (limited to 'dpkgQuery.go')
-rw-r--r--dpkgQuery.go41
1 files changed, 41 insertions, 0 deletions
diff --git a/dpkgQuery.go b/dpkgQuery.go
new file mode 100644
index 0000000..7369d62
--- /dev/null
+++ b/dpkgQuery.go
@@ -0,0 +1,41 @@
+package main
+
+import (
+ "bufio"
+ "os/exec"
+ "strings"
+)
+
+func getInstalledPackages() (map[string]string, error) {
+ // Run the dpkg-query command to list installed packages and versions
+ cmd := exec.Command("dpkg-query", "-W", "-f=${Package} ${Version}\n")
+ stdout, err := cmd.StdoutPipe()
+ if err != nil {
+ return nil, err
+ }
+
+ // Start the command execution
+ if err := cmd.Start(); err != nil {
+ return nil, err
+ }
+ defer cmd.Wait()
+
+ // Create a map to store package names and versions
+ installedPackages := make(map[string]string)
+
+ // Use a scanner to read the output of the command line by line
+ scanner := bufio.NewScanner(stdout)
+ for scanner.Scan() {
+ line := scanner.Text()
+ // Split each line into package name and version
+ parts := strings.SplitN(line, " ", 2)
+ if len(parts) == 2 {
+ packageName := parts[0]
+ version := parts[1]
+ installedPackages[packageName] = version
+ }
+ }
+
+ // Return the map with package names and versions
+ return installedPackages, scanner.Err()
+}