diff --git a/src/bpm/main.go b/src/bpm/main.go index 6622479..ee7dba3 100644 --- a/src/bpm/main.go +++ b/src/bpm/main.go @@ -162,6 +162,11 @@ func main() { setupFlagsAndHelp(currentFlagSet, fmt.Sprintf("bpm %s ", 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 ", 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 [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) { diff --git a/src/bpmlib/general.go b/src/bpmlib/general.go index 3c83a2d..44822bd 100644 --- a/src/bpmlib/general.go +++ b/src/bpmlib/general.go @@ -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), diff --git a/src/bpmlib/operations.go b/src/bpmlib/operations.go index 75edc2e..d4b7e58 100644 --- a/src/bpmlib/operations.go +++ b/src/bpmlib/operations.go @@ -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 { diff --git a/src/bpmlib/packages.go b/src/bpmlib/packages.go index ab16b65..95d84f2 100644 --- a/src/bpmlib/packages.go +++ b/src/bpmlib/packages.go @@ -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) diff --git a/src/bpmlib/utils.go b/src/bpmlib/utils.go index 3bb99be..5c7027d 100644 --- a/src/bpmlib/utils.go +++ b/src/bpmlib/utils.go @@ -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) +}