summaryrefslogtreecommitdiff
path: root/apt_linux.go
diff options
context:
space:
mode:
authorJeff Carr <[email protected]>2024-11-15 18:11:52 -0600
committerJeff Carr <[email protected]>2024-11-15 18:11:52 -0600
commit9e4045f35e7b037f60b0a89a4368b9c4f77aa48d (patch)
tree48a33398f5437ca41f18b2c159af3a30a7a66ef1 /apt_linux.go
parent76db985cf1c3ca4ff792b82f6209e00ba5679b58 (diff)
use zoopb protobufs
Diffstat (limited to 'apt_linux.go')
-rw-r--r--apt_linux.go74
1 files changed, 41 insertions, 33 deletions
diff --git a/apt_linux.go b/apt_linux.go
index 5d6eb16..7e534f0 100644
--- a/apt_linux.go
+++ b/apt_linux.go
@@ -3,47 +3,20 @@ package main
import (
"bufio"
"fmt"
- "os"
"os/exec"
- "runtime"
"strings"
-)
-
-// detectDistro returns the Linux distribution name (if possible)
-func detectDistro() string {
- // Check if we're on Linux
- if runtime.GOOS != "linux" {
- return ""
- }
- // Try to read /etc/os-release to determine the distro
- file, err := os.Open("/etc/os-release")
- if err != nil {
- return ""
- }
- defer file.Close()
-
- scanner := bufio.NewScanner(file)
- for scanner.Scan() {
- line := scanner.Text()
- if strings.HasPrefix(line, "ID=") {
- parts := strings.SplitN(line, "=", 2)
- if len(parts) == 2 {
- return strings.Trim(parts[1], `"`)
- }
- }
- }
- return ""
-}
+ "go.wit.com/log"
+)
// getPackageList returns the list of installed packages based on the distro
-func getPackageList(distro string) ([]string, error) {
+func getPackageList(distro string) (map[string]string, error) {
var cmd *exec.Cmd
// Run the appropriate command based on the detected distribution
switch distro {
case "ubuntu", "debian":
- cmd = exec.Command("dpkg-query", "-W", "-f=${Package} ${Version}\n")
+ return dpkgQuery()
case "fedora", "centos", "rhel":
cmd = exec.Command("rpm", "-qa")
case "arch", "manjaro":
@@ -58,7 +31,42 @@ func getPackageList(distro string) ([]string, error) {
return nil, fmt.Errorf("error running command: %v", err)
}
- // Split the output into lines and return
+ // todo: Split the output into lines and return
lines := strings.Split(string(output), "\n")
- return lines, nil
+ log.Info("output had", len(lines), "lines")
+ return nil, nil
+}
+
+func dpkgQuery() (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()
}