From 6e11f937a66a389c88a315558c42b8847402e1be Mon Sep 17 00:00:00 2001 From: EnumDev Date: Wed, 23 Oct 2024 16:10:42 +0300 Subject: [PATCH] Added conflict checking --- main.go | 19 ++++++++++++++++++ utils/operations.go | 47 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+) diff --git a/main.go b/main.go index c5b69d8..fbeeedc 100644 --- a/main.go +++ b/main.go @@ -222,6 +222,25 @@ func resolveCommand() { } } + // Check for conflicts + conflicts, err := operation.CheckForConflicts() + if err != nil { + log.Fatalf("Error: could not complete package conflict check: %s\n", err) + } + if len(conflicts) > 0 { + if !force { + log.Println("Error: conflicting packages found") + } else { + log.Fatalf("Warning: conflicting packages found") + } + for pkg, conflict := range conflicts { + fmt.Printf("%s is in conflict with the following packages: %s\n", pkg, strings.Join(conflict, ", ")) + } + if !force { + os.Exit(0) + } + } + // Show operation summary operation.ShowOperationSummary() diff --git a/utils/operations.go b/utils/operations.go index 57772d3..2035fad 100644 --- a/utils/operations.go +++ b/utils/operations.go @@ -122,6 +122,53 @@ func (operation *BPMOperation) ResolveDependencies(reinstallDependencies, instal return nil } +func (operation *BPMOperation) CheckForConflicts() (map[string][]string, error) { + conflicts := make(map[string][]string) + installedPackages, err := GetInstalledPackages(operation.RootDir) + if err != nil { + return nil, err + } + allPackages := make([]*PackageInfo, len(installedPackages)) + for i, value := range installedPackages { + bpmpkg := GetPackage(value, operation.RootDir) + if bpmpkg == nil { + return nil, errors.New(fmt.Sprintf("could not find installed package (%s)", value)) + } + allPackages[i] = bpmpkg.PkgInfo + } + + // Add all new packages to the allPackages slice + for _, value := range slices.Clone(operation.Actions) { + if value.GetActionType() == "install" { + action := value.(*InstallPackageAction) + pkgInfo := action.BpmPackage.PkgInfo + allPackages = append(allPackages, pkgInfo) + } else if value.GetActionType() == "fetch" { + action := value.(*FetchPackageAction) + pkgInfo := action.RepositoryEntry.Info + allPackages = append(allPackages, pkgInfo) + } else if value.GetActionType() == "remove" { + action := value.(*RemovePackageAction) + pkgInfo := action.BpmPackage.PkgInfo + slices.DeleteFunc(allPackages, func(info *PackageInfo) bool { + return info.Name == pkgInfo.Name + }) + } + } + + for _, value := range allPackages { + for _, conflict := range value.Conflicts { + if slices.ContainsFunc(allPackages, func(info *PackageInfo) bool { + return info.Name == conflict + }) { + conflicts[value.Name] = append(conflicts[value.Name], conflict) + } + } + } + + return conflicts, nil +} + func (operation *BPMOperation) ShowOperationSummary() { if len(operation.Actions) == 0 { fmt.Println("All packages are up to date!")