Added --cleanup flag to the remove subcommand

This commit is contained in:
EnumDev 2024-11-04 19:38:54 +02:00
parent 6e2bb86ef0
commit b625fe97ef
2 changed files with 75 additions and 37 deletions

50
main.go
View File

@ -37,6 +37,7 @@ var reinstallAll = false
var noOptional = false var noOptional = false
var nosync = true var nosync = true
var removeUnused = false var removeUnused = false
var doCleanup = false
func main() { func main() {
utils.ReadConfig() utils.ReadConfig()
@ -406,6 +407,14 @@ func resolveCommand() {
} }
} }
// Do package cleanup
if doCleanup {
err := operation.Cleanup(verbose)
if err != nil {
log.Fatalf("Error: could not perform cleanup for operation: %s\n", err)
}
}
// Show operation summary // Show operation summary
operation.ShowOperationSummary() operation.ShowOperationSummary()
@ -436,44 +445,10 @@ func resolveCommand() {
RootDir: rootDir, RootDir: rootDir,
} }
// Get all installed packages // Do package cleanup
installedPackageNames, err := utils.GetInstalledPackages(operation.RootDir) err := operation.Cleanup(verbose)
if err != nil { if err != nil {
log.Fatalf("Error: could not get installed packages: %s\n", err) log.Fatalf("Error: could not perform cleanup for operation: %s\n", err)
}
installedPackages := make([]*utils.PackageInfo, len(installedPackageNames))
for i, value := range installedPackageNames {
bpmpkg := utils.GetPackage(value, operation.RootDir)
if bpmpkg == nil {
log.Fatalf("Error: could not find installed package (%s)", value)
}
installedPackages[i] = bpmpkg.PkgInfo
}
// Get manually installed packages, resolve all their dependencies and add them to the keepPackages list
keepPackages := make([]string, 0)
for _, pkg := range slices.Clone(installedPackages) {
if utils.GetInstallationReason(pkg.Name, rootDir) != utils.Manual {
continue
}
keepPackages = append(keepPackages, pkg.Name)
resolved, _ := pkg.ResolveDependencies(&[]string{}, &[]string{}, false, true, false, verbose, rootDir)
for _, value := range resolved {
if !slices.Contains(keepPackages, value) && slices.Contains(installedPackageNames, value) {
keepPackages = append(keepPackages, value)
}
}
}
// Get all installed packages that are not in the keepPackages slice and add them to the BPM operation
for _, pkg := range installedPackageNames {
if !slices.Contains(keepPackages, pkg) {
bpmpkg := utils.GetPackage(pkg, rootDir)
if bpmpkg == nil {
log.Fatalf("Error: could not find installed package (%s)", pkg)
}
operation.Actions = append(operation.Actions, &utils.RemovePackageAction{BpmPackage: bpmpkg})
}
} }
// Show operation summary // Show operation summary
@ -646,6 +621,7 @@ func resolveFlags() {
removeFlagSet.BoolVar(&verbose, "v", false, "Show additional information about what BPM is doing") removeFlagSet.BoolVar(&verbose, "v", false, "Show additional information about what BPM is doing")
removeFlagSet.BoolVar(&yesAll, "y", false, "Skip confirmation prompts") removeFlagSet.BoolVar(&yesAll, "y", false, "Skip confirmation prompts")
removeFlagSet.BoolVar(&removeUnused, "unused", false, "Removes only packages that aren't required as dependencies by other packages") removeFlagSet.BoolVar(&removeUnused, "unused", false, "Removes only packages that aren't required as dependencies by other packages")
removeFlagSet.BoolVar(&doCleanup, "cleanup", false, "Perform a dependency cleanup ")
removeFlagSet.Usage = printHelp removeFlagSet.Usage = printHelp
// Cleanup flags // Cleanup flags
cleanupFlagSet := flag.NewFlagSet("Cleanup flags", flag.ExitOnError) cleanupFlagSet := flag.NewFlagSet("Cleanup flags", flag.ExitOnError)

View File

@ -165,6 +165,68 @@ func (operation *BPMOperation) RemoveNeededPackages() error {
return nil return nil
} }
func (operation *BPMOperation) Cleanup(verbose bool) error {
// Get all installed packages
installedPackageNames, err := GetInstalledPackages(operation.RootDir)
if err != nil {
log.Fatalf("Error: could not get installed packages: %s\n", err)
}
installedPackages := make([]*PackageInfo, len(installedPackageNames))
for i, value := range installedPackageNames {
bpmpkg := GetPackage(value, operation.RootDir)
if bpmpkg == nil {
return errors.New("could not find installed package (" + value + ")")
}
installedPackages[i] = bpmpkg.PkgInfo
}
// Get packages to remove
removeActions := make(map[string]*RemovePackageAction)
for _, action := range slices.Clone(operation.Actions) {
if action.GetActionType() == "remove" {
removeActions[action.(*RemovePackageAction).BpmPackage.PkgInfo.Name] = action.(*RemovePackageAction)
}
}
// Get manually installed packages, resolve all their dependencies and add them to the keepPackages slice
keepPackages := make([]string, 0)
for _, pkg := range slices.Clone(installedPackages) {
if GetInstallationReason(pkg.Name, operation.RootDir) != Manual {
continue
}
// Do not resolve dependencies or add package to keepPackages slice if package removal action exists for it
if _, ok := removeActions[pkg.Name]; ok {
continue
}
keepPackages = append(keepPackages, pkg.Name)
resolved, _ := pkg.ResolveDependencies(&[]string{}, &[]string{}, false, true, false, verbose, operation.RootDir)
for _, value := range resolved {
if !slices.Contains(keepPackages, value) && slices.Contains(installedPackageNames, value) {
keepPackages = append(keepPackages, value)
}
}
}
// Get all installed packages that are not in the keepPackages slice and add them to the BPM operation
for _, pkg := range installedPackageNames {
// Do not add package removal action if there already is one
if _, ok := removeActions[pkg]; ok {
continue
}
if !slices.Contains(keepPackages, pkg) {
bpmpkg := GetPackage(pkg, operation.RootDir)
if bpmpkg == nil {
return errors.New("Error: could not find installed package (" + pkg + ")")
}
operation.Actions = append(operation.Actions, &RemovePackageAction{BpmPackage: bpmpkg})
}
}
return nil
}
func (operation *BPMOperation) CheckForConflicts() (map[string][]string, error) { func (operation *BPMOperation) CheckForConflicts() (map[string][]string, error) {
conflicts := make(map[string][]string) conflicts := make(map[string][]string)
installedPackages, err := GetInstalledPackages(operation.RootDir) installedPackages, err := GetInstalledPackages(operation.RootDir)