Add 'vercmp' subcommand for package version comparisons in scripts

This commit is contained in:
EnumDev committed 2025-12-08 16:08:55 +02:00
1 parent 1895fabfb0
commit 2c91a9332b
5 files changed
+29 -10

No files matched your search

+19
View File
@@ -162,6 +162,11 @@ func main() {
setupFlagsAndHelp(currentFlagSet, fmt.Sprintf("bpm %s <options>", subcommand), "Compile source packages and convert them to binary ones", os.Args[2:])
compilePackage()
case "p", "vercmp":
currentFlagSet = flag.NewFlagSet("compile", flag.ExitOnError)
setupFlagsAndHelp(currentFlagSet, fmt.Sprintf("bpm %s <options>", subcommand), "Compare two version numbers", os.Args[2:])
compareVersions()
default:
listSubcommands()
}
@@ -1268,6 +1273,19 @@ func compilePackage() {
}
}
func compareVersions() {
if currentFlagSet.NArg() < 2 {
log.Printf("Error: vercmp subcommand requires 2 arguments")
exitCode = 1
return
}
v1 := currentFlagSet.Arg(0)
v2 := currentFlagSet.Arg(1)
fmt.Println(bpmlib.CompareVersions(v1, v2))
}
func listSubcommands() {
fmt.Printf("Usage: %s <subcommand> [options]\n", os.Args[0])
fmt.Println("Description: Manage system packages")
@@ -1282,6 +1300,7 @@ func listSubcommands() {
fmt.Println(" u, update Update installed packages")
fmt.Println(" o, owner Show what packages own the specified paths")
fmt.Println(" c, compile Compile source packages and convert them to binary ones")
fmt.Println(" p, vercmp Compare package version numbers")
}
func setupFlagsAndHelp(flagset *flag.FlagSet, usage, desc string, args []string) {
+1 -1
View File
@@ -416,7 +416,7 @@ func UpdatePackages(rootDir string, syncDatabase bool, installOptionalDependenci
if installedInfo == nil {
return nil, fmt.Errorf("could not get package info for package (%s)", pkg)
} else {
comparison := ComparePackageVersions(*entry.Info, *installedInfo)
comparison := CompareVersions(entry.Info.GetFullVersion(), installedInfo.GetFullVersion())
if comparison > 0 {
operation.AppendAction(&FetchPackageAction{
InstallationReason: GetInstallationReason(pkg, rootDir),
+1 -1
View File
@@ -370,7 +370,7 @@ func (operation *BPMOperation) ShowOperationSummary() {
if installedInfo == nil {
fmt.Fprintf(writer, "%s\t%s\t%s\t%s\t%t\n", pkgInfo.Name, pkgInfo.GetFullVersion(), "Install", installationReasonStr, pkgInfo.Type == "source")
} else {
comparison := ComparePackageVersions(*pkgInfo, *installedInfo)
comparison := CompareVersions(pkgInfo.GetFullVersion(), installedInfo.GetFullVersion())
if comparison < 0 {
fmt.Fprintf(writer, "%s\t%s -> %s\t%s\t%s\t%t\n", pkgInfo.Name, installedInfo.GetFullVersion(), pkgInfo.GetFullVersion(), "Downgrade", installationReasonStr, pkgInfo.Type == "source")
} else if comparison > 0 {
-8
View File
@@ -17,7 +17,6 @@ import (
"strings"
"syscall"
version "github.com/knqyf263/go-rpm-version"
"gopkg.in/yaml.v3"
)
@@ -121,13 +120,6 @@ const (
InstallationReasonUnknown InstallationReason = "unknown"
)
func ComparePackageVersions(info1, info2 PackageInfo) int {
v1 := version.NewVersion(info1.GetFullVersion())
v2 := version.NewVersion(info2.GetFullVersion())
return v1.Compare(v2)
}
func GetInstallationReason(pkg, rootDir string) InstallationReason {
installedDir := path.Join(rootDir, "var/lib/bpm/installed/")
pkgDir := path.Join(installedDir, pkg)
+8
View File
@@ -9,6 +9,7 @@ import (
"syscall"
"time"
version "github.com/knqyf263/go-rpm-version"
"github.com/schollz/progressbar/v3"
)
@@ -132,3 +133,10 @@ func bytesToHumanReadable(b int64) string {
}
return fmt.Sprintf("%.1fYiB", bf)
}
func CompareVersions(version1, version2 string) int {
v1 := version.NewVersion(version1)
v2 := version.NewVersion(version2)
return v1.Compare(v2)
}