yay/clean.go

227 lines
5.5 KiB
Go
Raw Normal View History

package main
import (
2021-08-12 18:56:23 +02:00
"context"
"fmt"
"os"
"path/filepath"
"github.com/leonelquinteros/gotext"
"github.com/Jguer/yay/v12/pkg/db"
"github.com/Jguer/yay/v12/pkg/query"
"github.com/Jguer/yay/v12/pkg/settings"
"github.com/Jguer/yay/v12/pkg/settings/exe"
"github.com/Jguer/yay/v12/pkg/settings/parser"
"github.com/Jguer/yay/v12/pkg/stringset"
"github.com/Jguer/yay/v12/pkg/text"
)
2021-08-11 20:13:28 +02:00
// CleanDependencies removes all dangling dependencies in system.
func cleanDependencies(ctx context.Context, cfg *settings.Configuration,
cmdArgs *parser.Arguments, dbExecutor db.Executor, removeOptional bool,
) error {
2020-08-07 18:55:19 +02:00
hanging := hangingPackages(removeOptional, dbExecutor)
if len(hanging) != 0 {
return cleanRemove(ctx, cfg, cmdArgs, hanging)
}
2018-07-31 10:42:17 +02:00
return nil
}
2021-08-11 20:13:28 +02:00
// CleanRemove sends a full removal command to pacman with the pkgName slice.
func cleanRemove(ctx context.Context, config *settings.Configuration, cmdArgs *parser.Arguments, pkgNames []string) error {
if len(pkgNames) == 0 {
return nil
}
2018-01-19 15:51:18 +01:00
2020-07-05 02:45:23 +02:00
arguments := cmdArgs.CopyGlobal()
_ = arguments.AddArg("R")
arguments.AddTarget(pkgNames...)
2018-07-31 10:42:17 +02:00
return config.Runtime.CmdBuilder.Show(
2021-08-12 18:56:23 +02:00
config.Runtime.CmdBuilder.BuildPacmanCmd(ctx,
arguments, config.Runtime.Mode, settings.NoConfirm))
}
func syncClean(ctx context.Context, cfg *settings.Configuration, cmdArgs *parser.Arguments, dbExecutor db.Executor) error {
keepInstalled := false
keepCurrent := false
2020-07-08 03:22:01 +02:00
_, removeAll, _ := cmdArgs.GetArg("c", "clean")
for _, v := range cfg.Runtime.PacmanConf.CleanMethod {
if v == "KeepInstalled" {
keepInstalled = true
} else if v == "KeepCurrent" {
keepCurrent = true
}
}
if cfg.Runtime.Mode.AtLeastRepo() {
if err := cfg.Runtime.CmdBuilder.Show(cfg.Runtime.CmdBuilder.BuildPacmanCmd(ctx,
cmdArgs, cfg.Runtime.Mode, settings.NoConfirm)); err != nil {
2018-07-24 03:49:45 +02:00
return err
}
}
if !cfg.Runtime.Mode.AtLeastAUR() {
2018-07-24 03:49:45 +02:00
return nil
}
var question string
if removeAll {
question = gotext.Get("Do you want to remove ALL AUR packages from cache?")
} else {
question = gotext.Get("Do you want to remove all other AUR packages from cache?")
}
fmt.Println(gotext.Get("\nBuild directory:"), cfg.BuildDir)
if text.ContinueTask(os.Stdin, question, true, settings.NoConfirm) {
if err := cleanAUR(ctx, cfg, keepInstalled, keepCurrent, removeAll, dbExecutor); err != nil {
return err
}
}
if removeAll {
return nil
}
if text.ContinueTask(os.Stdin, gotext.Get("Do you want to remove ALL untracked AUR files?"), true, settings.NoConfirm) {
return cleanUntracked(ctx, cfg)
}
2018-07-31 10:42:17 +02:00
return nil
}
func cleanAUR(ctx context.Context, config *settings.Configuration,
keepInstalled, keepCurrent, removeAll bool, dbExecutor db.Executor,
) error {
fmt.Println(gotext.Get("removing AUR packages from cache..."))
installedBases := make(stringset.StringSet)
inAURBases := make(stringset.StringSet)
remotePackages := dbExecutor.InstalledRemotePackages()
files, err := os.ReadDir(config.BuildDir)
if err != nil {
return err
}
cachedPackages := make([]string, 0, len(files))
2021-08-11 20:13:28 +02:00
for _, file := range files {
if !file.IsDir() {
continue
}
cachedPackages = append(cachedPackages, file.Name())
}
// Most people probably don't use keep current and that is the only
// case where this is needed.
2018-05-12 18:16:48 +02:00
// Querying the AUR is slow and needs internet so don't do it if we
// don't need to.
if keepCurrent {
2021-08-12 18:56:23 +02:00
info, errInfo := query.AURInfo(ctx, config.Runtime.AURClient, cachedPackages, &query.AURWarnings{}, config.RequestSplitN)
if errInfo != nil {
return errInfo
}
for i := range info {
inAURBases.Set(info[i].PackageBase)
}
}
for _, pkg := range remotePackages {
if pkg.Base() != "" {
installedBases.Set(pkg.Base())
} else {
installedBases.Set(pkg.Name())
}
}
for _, file := range files {
if !file.IsDir() {
continue
}
if !removeAll {
if keepInstalled && installedBases.Get(file.Name()) {
continue
}
if keepCurrent && inAURBases.Get(file.Name()) {
continue
}
}
dir := filepath.Join(config.BuildDir, file.Name())
err = os.RemoveAll(dir)
if err != nil {
text.Warnln(gotext.Get("Unable to remove %s: %s", dir, err))
}
}
return nil
}
func cleanUntracked(ctx context.Context, cfg *settings.Configuration) error {
fmt.Println(gotext.Get("removing untracked AUR files from cache..."))
files, err := os.ReadDir(cfg.BuildDir)
if err != nil {
return err
}
for _, file := range files {
if !file.IsDir() {
continue
}
dir := filepath.Join(cfg.BuildDir, file.Name())
if isGitRepository(dir) {
if err := cfg.Runtime.CmdBuilder.Show(cfg.Runtime.CmdBuilder.BuildGitCmd(ctx, dir, "clean", "-fx")); err != nil {
text.Warnln(gotext.Get("Unable to clean:"), dir)
2021-08-11 20:13:28 +02:00
return err
}
}
}
2021-08-11 20:13:28 +02:00
return nil
}
2018-09-27 15:10:36 +02:00
func isGitRepository(dir string) bool {
_, err := os.Stat(filepath.Join(dir, ".git"))
return !os.IsNotExist(err)
}
func cleanAfter(ctx context.Context, config *settings.Configuration,
cmdBuilder exe.ICmdBuilder, pkgbuildDirs map[string]string,
) {
2020-05-08 18:13:51 +02:00
fmt.Println(gotext.Get("removing untracked AUR files from cache..."))
2018-09-27 15:10:36 +02:00
i := 0
for _, dir := range pkgbuildDirs {
2022-11-14 01:14:13 +01:00
text.OperationInfoln(gotext.Get("Cleaning (%d/%d): %s", i+1, len(pkgbuildDirs), text.Cyan(dir)))
2018-09-27 15:10:36 +02:00
2022-11-14 01:14:13 +01:00
_, stderr, err := cmdBuilder.Capture(
cmdBuilder.BuildGitCmd(
2021-08-12 18:56:23 +02:00
ctx, dir, "reset", "--hard", "HEAD"))
if err != nil {
2022-11-14 01:14:13 +01:00
text.Errorln(gotext.Get("error resetting %s: %s", dir, stderr))
}
2018-09-27 15:10:36 +02:00
2021-08-12 18:56:23 +02:00
if err := config.Runtime.CmdBuilder.Show(
config.Runtime.CmdBuilder.BuildGitCmd(
ctx, dir, "clean", "-fx", "--exclude", "*.pkg.*")); err != nil {
fmt.Fprintln(os.Stderr, err)
2018-09-27 15:10:36 +02:00
}
i++
2018-09-27 15:10:36 +02:00
}
}