package main import ( "bufio" "bytes" "os/exec" "strings" ) func listInstalledPackages() ([]string, error) { // Execute dpkg -l command cmd := exec.Command("dpkg", "-l") var out bytes.Buffer cmd.Stdout = &out if err := cmd.Run(); err != nil { return nil, err } // Initialize slice to hold package names var packages []string scanner := bufio.NewScanner(&out) // Skip the first five lines as they are headers in dpkg -l output for i := 0; i < 5 && scanner.Scan(); i++ { // Ignore header lines } // Process each remaining line for scanner.Scan() { line := scanner.Text() fields := strings.Fields(line) // Ensure the line has enough fields for parsing if len(fields) >= 2 { status := fields[0] packageName := fields[1] // Only add packages with "ii" status (installed) if status == "ii" { packages = append(packages, packageName) } } } if err := scanner.Err(); err != nil { return nil, err } return packages, nil }