From 34e81d5d7d3ce4b5653caaef6f98d2055d6c12d1 Mon Sep 17 00:00:00 2001 From: jguer Date: Sun, 10 Oct 2021 23:59:55 +0200 Subject: [PATCH] fix(clean_menu): clean menu errors don't exit yay anymore --- clean.go | 11 ---- clean_menu.go | 123 +++++++++++++++++++++++++++++++++++++++++ install.go | 106 +++-------------------------------- pkg/settings/errors.go | 6 ++ 4 files changed, 138 insertions(+), 108 deletions(-) create mode 100644 clean_menu.go diff --git a/clean.go b/clean.go index 737facf..a25a8fa 100644 --- a/clean.go +++ b/clean.go @@ -219,14 +219,3 @@ func cleanAfter(ctx context.Context, bases []dep.Base) { } } } - -func cleanBuilds(bases []dep.Base) { - for i, base := range bases { - dir := filepath.Join(config.BuildDir, base.Pkgbase()) - text.OperationInfoln(gotext.Get("Deleting (%d/%d): %s", i+1, len(bases), text.Cyan(dir))) - - if err := os.RemoveAll(dir); err != nil { - fmt.Fprintln(os.Stderr, err) - } - } -} diff --git a/clean_menu.go b/clean_menu.go new file mode 100644 index 0000000..c237462 --- /dev/null +++ b/clean_menu.go @@ -0,0 +1,123 @@ +// Clean Build Menu functions +package main + +import ( + "fmt" + "os" + "path/filepath" + + "github.com/leonelquinteros/gotext" + + "github.com/Jguer/yay/v11/pkg/dep" + "github.com/Jguer/yay/v11/pkg/intrange" + "github.com/Jguer/yay/v11/pkg/settings" + "github.com/Jguer/yay/v11/pkg/stringset" + "github.com/Jguer/yay/v11/pkg/text" +) + +func cleanNumberMenu(bases []dep.Base, installed stringset.StringSet, hasClean bool) ([]dep.Base, error) { + toClean := make([]dep.Base, 0) + + if !hasClean { + return toClean, nil + } + + text.Infoln(gotext.Get("Packages to cleanBuild?")) + text.Infoln(gotext.Get("%s [A]ll [Ab]ort [I]nstalled [No]tInstalled or (1 2 3, 1-3, ^4)", text.Cyan(gotext.Get("[N]one")))) + + cleanInput, err := getInput(config.AnswerClean) + if err != nil { + return nil, err + } + + cInclude, cExclude, cOtherInclude, cOtherExclude := intrange.ParseNumberMenu(cleanInput) + cIsInclude := len(cExclude) == 0 && len(cOtherExclude) == 0 + + if cOtherInclude.Get("abort") || cOtherInclude.Get("ab") { + return nil, settings.ErrUserAbort{} + } + + if !cOtherInclude.Get("n") && !cOtherInclude.Get("none") { + for i, base := range bases { + pkg := base.Pkgbase() + anyInstalled := false + + for _, b := range base { + anyInstalled = anyInstalled || installed.Get(b.Name) + } + + dir := filepath.Join(config.BuildDir, pkg) + if _, err := os.Stat(dir); os.IsNotExist(err) { + continue + } + + if !cIsInclude && cExclude.Get(len(bases)-i) { + continue + } + + if anyInstalled && (cOtherInclude.Get("i") || cOtherInclude.Get("installed")) { + toClean = append(toClean, base) + continue + } + + if !anyInstalled && (cOtherInclude.Get("no") || cOtherInclude.Get("notinstalled")) { + toClean = append(toClean, base) + continue + } + + if cOtherInclude.Get("a") || cOtherInclude.Get("all") { + toClean = append(toClean, base) + continue + } + + if cIsInclude && (cInclude.Get(len(bases)-i) || cOtherInclude.Get(pkg)) { + toClean = append(toClean, base) + continue + } + + if !cIsInclude && (!cExclude.Get(len(bases)-i) && !cOtherExclude.Get(pkg)) { + toClean = append(toClean, base) + continue + } + } + } + + return toClean, nil +} + +func anyExistInCache(bases []dep.Base) bool { + for _, base := range bases { + pkg := base.Pkgbase() + dir := filepath.Join(config.BuildDir, pkg) + + if _, err := os.Stat(dir); !os.IsNotExist(err) { + return true + } + } + + return false +} + +func cleanMenu(cleanMenuOption bool, aurBases []dep.Base, installed stringset.StringSet) error { + if !(cleanMenuOption && anyExistInCache(aurBases)) { + return nil + } + + askClean := pkgbuildNumberMenu(aurBases, installed) + + toClean, errClean := cleanNumberMenu(aurBases, installed, askClean) + if errClean != nil { + return errClean + } + + for i, base := range toClean { + dir := filepath.Join(config.BuildDir, base.Pkgbase()) + text.OperationInfoln(gotext.Get("Deleting (%d/%d): %s", i+1, len(toClean), text.Cyan(dir))) + + if err := os.RemoveAll(dir); err != nil { + fmt.Fprintln(os.Stderr, err) + } + } + + return nil +} diff --git a/install.go b/install.go index cbfb76d..946de51 100644 --- a/install.go +++ b/install.go @@ -222,17 +222,12 @@ func install(ctx context.Context, cmdArgs *parser.Arguments, dbExecutor db.Execu } } - if config.CleanMenu { - if anyExistInCache(do.Aur) { - askClean := pkgbuildNumberMenu(do.Aur, remoteNamesCache) - - toClean, errClean := cleanNumberMenu(do.Aur, remoteNamesCache, askClean) - if errClean != nil { - return errClean - } - - cleanBuilds(toClean) + if errCleanMenu := cleanMenu(config.CleanMenu, do.Aur, remoteNamesCache); errCleanMenu != nil { + if errors.As(errCleanMenu, &settings.ErrUserAbort{}) { + return errCleanMenu } + + text.Errorln(errCleanMenu) } toSkip := pkgbuildsToSkip(do.Aur, targets) @@ -282,7 +277,7 @@ func install(ctx context.Context, cmdArgs *parser.Arguments, dbExecutor db.Execu fmt.Println() if !text.ContinueTask(gotext.Get("Proceed with install?"), true, settings.NoConfirm) { - return fmt.Errorf(gotext.Get("aborting due to user")) + return &settings.ErrUserAbort{} } err = updatePkgbuildSeenRef(ctx, toDiff) @@ -325,7 +320,7 @@ func install(ctx context.Context, cmdArgs *parser.Arguments, dbExecutor db.Execu fmt.Println() if !text.ContinueTask(gotext.Get("Proceed with install?"), true, settings.NoConfirm) { - return errors.New(gotext.Get("aborting due to user")) + return &settings.ErrUserAbort{} } settings.NoConfirm = oldValue @@ -520,7 +515,7 @@ nextpkg: fmt.Println() if !text.ContinueTask(gotext.Get("Try to build them anyway?"), true, settings.NoConfirm) { - return nil, errors.New(gotext.Get("aborting due to user")) + return nil, &settings.ErrUserAbort{} } } @@ -560,19 +555,6 @@ func parsePackageList(ctx context.Context, dir string) (pkgdests map[string]stri return pkgdests, pkgVersion, nil } -func anyExistInCache(bases []dep.Base) bool { - for _, base := range bases { - pkg := base.Pkgbase() - dir := filepath.Join(config.BuildDir, pkg) - - if _, err := os.Stat(dir); !os.IsNotExist(err) { - return true - } - } - - return false -} - func pkgbuildNumberMenu(bases []dep.Base, installed stringset.StringSet) bool { toPrint := "" askClean := false @@ -606,76 +588,6 @@ func pkgbuildNumberMenu(bases []dep.Base, installed stringset.StringSet) bool { return askClean } -func cleanNumberMenu(bases []dep.Base, installed stringset.StringSet, hasClean bool) ([]dep.Base, error) { - toClean := make([]dep.Base, 0) - - if !hasClean { - return toClean, nil - } - - text.Infoln(gotext.Get("Packages to cleanBuild?")) - text.Infoln(gotext.Get("%s [A]ll [Ab]ort [I]nstalled [No]tInstalled or (1 2 3, 1-3, ^4)", text.Cyan(gotext.Get("[N]one")))) - - cleanInput, err := getInput(config.AnswerClean) - if err != nil { - return nil, err - } - - cInclude, cExclude, cOtherInclude, cOtherExclude := intrange.ParseNumberMenu(cleanInput) - cIsInclude := len(cExclude) == 0 && len(cOtherExclude) == 0 - - if cOtherInclude.Get("abort") || cOtherInclude.Get("ab") { - return nil, fmt.Errorf(gotext.Get("aborting due to user")) - } - - if !cOtherInclude.Get("n") && !cOtherInclude.Get("none") { - for i, base := range bases { - pkg := base.Pkgbase() - anyInstalled := false - - for _, b := range base { - anyInstalled = anyInstalled || installed.Get(b.Name) - } - - dir := filepath.Join(config.BuildDir, pkg) - if _, err := os.Stat(dir); os.IsNotExist(err) { - continue - } - - if !cIsInclude && cExclude.Get(len(bases)-i) { - continue - } - - if anyInstalled && (cOtherInclude.Get("i") || cOtherInclude.Get("installed")) { - toClean = append(toClean, base) - continue - } - - if !anyInstalled && (cOtherInclude.Get("no") || cOtherInclude.Get("notinstalled")) { - toClean = append(toClean, base) - continue - } - - if cOtherInclude.Get("a") || cOtherInclude.Get("all") { - toClean = append(toClean, base) - continue - } - - if cIsInclude && (cInclude.Get(len(bases)-i) || cOtherInclude.Get(pkg)) { - toClean = append(toClean, base) - continue - } - - if !cIsInclude && (!cExclude.Get(len(bases)-i) && !cOtherExclude.Get(pkg)) { - toClean = append(toClean, base) - continue - } - } - } - - return toClean, nil -} - func editNumberMenu(bases []dep.Base, installed stringset.StringSet) ([]dep.Base, error) { return editDiffNumberMenu(bases, installed, false) } @@ -712,7 +624,7 @@ func editDiffNumberMenu(bases []dep.Base, installed stringset.StringSet, diff bo eIsInclude := len(eExclude) == 0 && len(eOtherExclude) == 0 if eOtherInclude.Get("abort") || eOtherInclude.Get("ab") { - return nil, fmt.Errorf(gotext.Get("aborting due to user")) + return nil, &settings.ErrUserAbort{} } if !eOtherInclude.Get("n") && !eOtherInclude.Get("none") { diff --git a/pkg/settings/errors.go b/pkg/settings/errors.go index 6b3fc24..761885d 100644 --- a/pkg/settings/errors.go +++ b/pkg/settings/errors.go @@ -22,3 +22,9 @@ type ErrRuntimeDir struct { func (e *ErrRuntimeDir) Error() string { return gotext.Get("failed to create directory '%s': %s", e.dir, e.inner) } + +type ErrUserAbort struct{} + +func (e ErrUserAbort) Error() string { + return gotext.Get("aborting due to user") +}