Add 'vercmp' subcommand for package version comparisons in scripts

This commit is contained in:
2025-12-08 16:08:55 +02:00
parent 1895fabfb0
commit 2c91a9332b
5 changed files with 29 additions and 10 deletions
+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:]) setupFlagsAndHelp(currentFlagSet, fmt.Sprintf("bpm %s <options>", subcommand), "Compile source packages and convert them to binary ones", os.Args[2:])
compilePackage() 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: default:
listSubcommands() 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() { func listSubcommands() {
fmt.Printf("Usage: %s <subcommand> [options]\n", os.Args[0]) fmt.Printf("Usage: %s <subcommand> [options]\n", os.Args[0])
fmt.Println("Description: Manage system packages") fmt.Println("Description: Manage system packages")
@@ -1282,6 +1300,7 @@ func listSubcommands() {
fmt.Println(" u, update Update installed packages") fmt.Println(" u, update Update installed packages")
fmt.Println(" o, owner Show what packages own the specified paths") 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(" 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) { 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 { if installedInfo == nil {
return nil, fmt.Errorf("could not get package info for package (%s)", pkg) return nil, fmt.Errorf("could not get package info for package (%s)", pkg)
} else { } else {
comparison := ComparePackageVersions(*entry.Info, *installedInfo) comparison := CompareVersions(entry.Info.GetFullVersion(), installedInfo.GetFullVersion())
if comparison > 0 { if comparison > 0 {
operation.AppendAction(&FetchPackageAction{ operation.AppendAction(&FetchPackageAction{
InstallationReason: GetInstallationReason(pkg, rootDir), InstallationReason: GetInstallationReason(pkg, rootDir),
+1 -1
View File
@@ -370,7 +370,7 @@ func (operation *BPMOperation) ShowOperationSummary() {
if installedInfo == nil { 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") fmt.Fprintf(writer, "%s\t%s\t%s\t%s\t%t\n", pkgInfo.Name, pkgInfo.GetFullVersion(), "Install", installationReasonStr, pkgInfo.Type == "source")
} else { } else {
comparison := ComparePackageVersions(*pkgInfo, *installedInfo) comparison := CompareVersions(pkgInfo.GetFullVersion(), installedInfo.GetFullVersion())
if comparison < 0 { 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") 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 { } else if comparison > 0 {
-8
View File
@@ -17,7 +17,6 @@ import (
"strings" "strings"
"syscall" "syscall"
version "github.com/knqyf263/go-rpm-version"
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
) )
@@ -121,13 +120,6 @@ const (
InstallationReasonUnknown InstallationReason = "unknown" 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 { func GetInstallationReason(pkg, rootDir string) InstallationReason {
installedDir := path.Join(rootDir, "var/lib/bpm/installed/") installedDir := path.Join(rootDir, "var/lib/bpm/installed/")
pkgDir := path.Join(installedDir, pkg) pkgDir := path.Join(installedDir, pkg)
+8
View File
@@ -9,6 +9,7 @@ import (
"syscall" "syscall"
"time" "time"
version "github.com/knqyf263/go-rpm-version"
"github.com/schollz/progressbar/v3" "github.com/schollz/progressbar/v3"
) )
@@ -132,3 +133,10 @@ func bytesToHumanReadable(b int64) string {
} }
return fmt.Sprintf("%.1fYiB", bf) return fmt.Sprintf("%.1fYiB", bf)
} }
func CompareVersions(version1, version2 string) int {
v1 := version.NewVersion(version1)
v2 := version.NewVersion(version2)
return v1.Compare(v2)
}