Skip to content

Commit

Permalink
Improve the check for installed versions
Browse files Browse the repository at this point in the history
  • Loading branch information
andyone committed Oct 11, 2023
1 parent 1db1804 commit c94fd8c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 84 deletions.
2 changes: 1 addition & 1 deletion cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down
96 changes: 13 additions & 83 deletions rpm/rpm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

// ////////////////////////////////////////////////////////////////////////////////// //

0 comments on commit c94fd8c

Please sign in to comment.