Added conflict checking

This commit is contained in:
EnumDev 2024-10-23 16:10:42 +03:00
parent db6a776763
commit 6e11f937a6
2 changed files with 66 additions and 0 deletions

19
main.go
View File

@ -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()

View File

@ -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!")