From c94fd8cf09ab3e3c6307fdf98b899a5f19f6f7cc Mon Sep 17 00:00:00 2001 From: Anton Novojilov Date: Wed, 11 Oct 2023 13:32:11 +0300 Subject: [PATCH] Improve the check for installed versions --- cli/cli.go | 2 +- rpm/rpm.go | 96 ++++++++---------------------------------------------- 2 files changed, 14 insertions(+), 84 deletions(-) diff --git a/cli/cli.go b/cli/cli.go index f7d52b1..89bec7a 100644 --- a/cli/cli.go +++ b/cli/cli.go @@ -31,7 +31,7 @@ import ( // Basic utility info const ( APP = "spec-builddep" - VER = "0.1.2" + VER = "0.1.3" DESC = "Utility for installing dependencies for building an RPM package" ) diff --git a/rpm/rpm.go b/rpm/rpm.go index 869511f..64ec7a0 100644 --- a/rpm/rpm.go +++ b/rpm/rpm.go @@ -11,106 +11,36 @@ import ( "bytes" "os/exec" "strings" - - "github.com/essentialkaos/ek/v12/sliceutil" ) // ////////////////////////////////////////////////////////////////////////////////// // -type pkgIndex struct { - ByName map[string]string - ByProvides map[string]string -} - -// ////////////////////////////////////////////////////////////////////////////////// // - // Versions returns map with versions of installed packages func Versions(packages []string) map[string]string { - packages, index := normalizePackageNames(packages) - return getVersions(packages, index) -} - -// ////////////////////////////////////////////////////////////////////////////////// // - -// normalizePackageNames normalizes packages names -func normalizePackageNames(packages []string) ([]string, *pkgIndex) { - var result []string - - index := getIndex(packages) - - for _, p := range packages { - if index.ByName[p] != "" { - result = append(result, index.ByName[p]) - } else { - result = append(result, p) - } - } - - return result, index -} - -// getIndex returns index with given packages names and provided packages names -func getIndex(packages []string) *pkgIndex { - index := &pkgIndex{ - ByName: make(map[string]string), - ByProvides: make(map[string]string), - } + versions := make(map[string]string) - cmd := exec.Command("rpm", "-q", "--whatprovides", "--qf", "%{name}\n") - cmd.Args = append(cmd.Args, packages...) - data, _ := cmd.CombinedOutput() + for _, pkg := range packages { + cmd := exec.Command("rpm", "-q", "--whatprovides", "--qf", "%{version}\n", pkg) + data, _ := cmd.CombinedOutput() - if len(data) == 0 { - return index - } - - lines := sliceutil.Deduplicate(strings.Split(string(data), "\n")) - - for i := 0; i < len(packages); i++ { - if strings.Contains(lines[i], " ") { + if len(data) == 0 { continue } - index.ByName[packages[i]] = lines[i] - index.ByProvides[lines[i]] = packages[i] - } - - return index -} - -// getVersions returns map with versions info for installed packages -func getVersions(packages []string, index *pkgIndex) map[string]string { - result := map[string]string{} - - cmd := exec.Command("rpm", "-q", "--qf", "%{name} %{version}\n") - cmd.Args = append(cmd.Args, packages...) - data, _ := cmd.CombinedOutput() + nlIndex := bytes.IndexRune(data, '\n') - if len(data) == 0 { - return result - } - - buf := bytes.NewBuffer(data) - - for { - line, err := buf.ReadString('\n') - - if err != nil { - break - } - - if strings.Count(line, " ") > 1 { + if nlIndex == -1 { continue } - name, version, _ := strings.Cut(line, " ") + version := string(data[:nlIndex]) - if index.ByProvides[name] == "" { - result[name] = strings.Trim(version, "\n\r") - } else { - result[index.ByProvides[name]] = strings.Trim(version, "\n\r") + if !strings.Contains(version, " ") { + versions[pkg] = version } } - return result + return versions } + +// ////////////////////////////////////////////////////////////////////////////////// //