2023-10-22 16:11:13 +02:00
|
|
|
//go:build linux
|
|
|
|
|
|
|
|
package zypper
|
|
|
|
|
|
|
|
import (
|
|
|
|
"git.jmbit.de/jmb/patchman/client/common"
|
|
|
|
"git.jmbit.de/jmb/patchman/client/utils"
|
|
|
|
"log"
|
|
|
|
"os/exec"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
|
|
|
// GetInstalledList Because Zypper sadly can't give both the upgradeable and installed packages at the same time,
|
|
|
|
// concurrently get both a list of all installed packages and all upgradeable and merge them
|
|
|
|
func GetInstalledList() []common.ListPackage {
|
|
|
|
var allPackages []common.ListPackage
|
|
|
|
var upgradeablePackages []common.ListPackage
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(2)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
allPackages = getAllPackagesList()
|
2023-10-22 17:46:46 +02:00
|
|
|
log.Println("Got Zypper Package list")
|
2023-10-22 16:11:13 +02:00
|
|
|
}()
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
upgradeablePackages = getUpgradeableList()
|
2023-10-22 17:46:46 +02:00
|
|
|
log.Println("Got Zypper upgradable Package list")
|
2023-10-22 16:11:13 +02:00
|
|
|
}()
|
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
return utils.DedupAndMergePackageList(allPackages, upgradeablePackages)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func getAllPackagesList() []common.ListPackage {
|
|
|
|
var packageList []common.ListPackage
|
|
|
|
// List of all upgradeable packages
|
|
|
|
cmd := exec.Command("/usr/bin/zypper", "se", "-si")
|
|
|
|
out, err := cmd.Output()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Could not get zypper packages %w", err)
|
|
|
|
}
|
|
|
|
// Convert Output into text and split it into newlines
|
|
|
|
lines := strings.Split(string(out[:]), "\n")
|
|
|
|
// Parse lines
|
|
|
|
//TODO parallelize this
|
2023-10-22 17:46:46 +02:00
|
|
|
for i, line := range lines {
|
|
|
|
if i < 6 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-10-22 16:11:13 +02:00
|
|
|
sections := strings.Split(line, "|")
|
2023-10-22 17:46:46 +02:00
|
|
|
if len(sections) != 6 {
|
|
|
|
log.Print(line)
|
|
|
|
continue
|
|
|
|
}
|
2023-10-22 16:11:13 +02:00
|
|
|
name := strings.TrimSpace(sections[1])
|
|
|
|
source := strings.TrimSpace(sections[5])
|
|
|
|
version := strings.TrimSpace(sections[3])
|
|
|
|
architecture := strings.TrimSpace(sections[4])
|
|
|
|
|
|
|
|
listPackage := common.ListPackage{
|
|
|
|
Name: name,
|
|
|
|
Source: source,
|
|
|
|
Version: version,
|
|
|
|
Architecture: architecture,
|
|
|
|
PackageManager: "zypper",
|
|
|
|
}
|
|
|
|
packageList = append(packageList, listPackage)
|
|
|
|
}
|
2023-10-22 17:46:46 +02:00
|
|
|
return packageList
|
2023-10-22 16:11:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func getUpgradeableList() []common.ListPackage {
|
|
|
|
var packageList []common.ListPackage
|
|
|
|
// List of all upgradeable packages
|
|
|
|
cmd := exec.Command("/usr/bin/zypper", "list-updates", "--all")
|
|
|
|
out, err := cmd.Output()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Could not get zypper packages %w", err)
|
|
|
|
}
|
|
|
|
// Convert Output into text and split it into newlines
|
|
|
|
lines := strings.Split(string(out[:]), "\n")
|
|
|
|
// Parse lines
|
|
|
|
//TODO parallelize this
|
2023-10-22 17:46:46 +02:00
|
|
|
for i, line := range lines {
|
|
|
|
if i < 6 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-10-22 16:11:13 +02:00
|
|
|
sections := strings.Split(line, "|")
|
2023-10-22 17:46:46 +02:00
|
|
|
|
|
|
|
if len(sections) != 6 {
|
|
|
|
log.Print(line)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
name := strings.TrimSpace(sections[2])
|
|
|
|
source := strings.TrimSpace(sections[1])
|
|
|
|
version := strings.TrimSpace(sections[3])
|
|
|
|
upgradeableTo := strings.TrimSpace(sections[4])
|
2023-10-22 16:11:13 +02:00
|
|
|
architecture := strings.TrimSpace(sections[5])
|
|
|
|
|
|
|
|
listPackage := common.ListPackage{
|
|
|
|
Name: name,
|
|
|
|
Source: source,
|
|
|
|
Version: version,
|
|
|
|
Architecture: architecture,
|
2023-10-22 17:46:46 +02:00
|
|
|
Upgradeable: true,
|
2023-10-22 16:11:13 +02:00
|
|
|
UpgradeableTo: upgradeableTo,
|
|
|
|
PackageManager: "zypper",
|
|
|
|
}
|
|
|
|
packageList = append(packageList, listPackage)
|
|
|
|
}
|
|
|
|
|
|
|
|
return packageList
|
|
|
|
}
|