Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(yay): move cfg inside of runtime #2259

Merged
merged 19 commits into from
Aug 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ linters:
# inverted configuration with `enable-all` and `disable` is not scalable during updates of golangci-lint
disable-all: true
enable:
- forbidigo
- bodyclose
- dogsled
- dupl
Expand Down Expand Up @@ -80,8 +81,12 @@ linters:
- whitespace

run:
go: "1.18"
go: "1.20"
timeout: "10m"
forbidigo:
forbid:
- p: ^fmt\.Print.*$
msg: Do not commit print statements.

issues:
exclude-rules:
Expand Down
28 changes: 1 addition & 27 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,17 +111,6 @@ pacman -S --needed git base-devel yay
Make sure you have the `Color` option in your `/etc/pacman.conf`
(see issue [#123](https://github.com/Jguer/yay/issues/123)).

- **Yay is not prompting to skip packages during system upgrade.**

The default behavior was changed after
[v8.918](https://github.com/Jguer/yay/releases/tag/v8.918)
(see [3bdb534](https://github.com/Jguer/yay/commit/3bdb5343218d99d40f8a449b887348611f6bdbfc)
and issue [#554](https://github.com/Jguer/yay/issues/554)).
To restore the package-skip behavior use `--combinedupgrade` (make
it permanent by appending `--save`). Note: skipping packages will leave your
system in a
[partially-upgraded state](https://wiki.archlinux.org/index.php/System_maintenance#Partial_upgrades_are_unsupported).

- **Sometimes diffs are printed to the terminal, and other times they are paged via less. How do I fix this?**

Yay uses `git diff` to display diffs, which by default tells less not to
Expand All @@ -137,7 +126,7 @@ pacman -S --needed git base-devel yay
`yay -{OPERATION} --aur`
`yay -{OPERATION} --repo`

- **An `Out Of Date AUR Packages` message is displayed. Why doesn't Yay update them?**
- **A `Flagged Out Of Date AUR Packages` message is displayed. Why doesn't Yay update them?**

This message does not mean that updated AUR packages are available. It means
the packages have been flagged out of date on the AUR, but
Expand All @@ -159,21 +148,6 @@ pacman -S --needed git base-devel yay

Check [CONTRIBUTING.md](./CONTRIBUTING.md) for more information.

- **What settings do you use?**

```sh
yay -Y --devel --combinedupgrade --batchinstall --save
```

Pacman conf options:

```conf
UseSyslog
Color
CheckSpace
VerbosePkgLists
```

## Support

All support related to Yay should be requested via GitHub issues. Since Yay is not
Expand Down
80 changes: 26 additions & 54 deletions clean.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"context"
"fmt"
"os"
"path/filepath"

Expand All @@ -11,10 +10,10 @@ import (
"github.com/leonelquinteros/gotext"

"github.com/Jguer/yay/v12/pkg/db"
"github.com/Jguer/yay/v12/pkg/runtime"
"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/text"
)

// CleanDependencies removes all dangling dependencies in system.
Expand Down Expand Up @@ -49,28 +48,28 @@ func cleanRemove(ctx context.Context, cfg *settings.Configuration,
arguments, cfg.Mode, settings.NoConfirm))
}

func syncClean(ctx context.Context, cfg *settings.Configuration, cmdArgs *parser.Arguments, dbExecutor db.Executor) error {
func syncClean(ctx context.Context, run *runtime.Runtime, cmdArgs *parser.Arguments, dbExecutor db.Executor) error {
keepInstalled := false
keepCurrent := false

_, removeAll, _ := cmdArgs.GetArg("c", "clean")

for _, v := range cfg.Runtime.PacmanConf.CleanMethod {
for _, v := range run.PacmanConf.CleanMethod {
if v == "KeepInstalled" {
keepInstalled = true
} else if v == "KeepCurrent" {
keepCurrent = true
}
}

if cfg.Mode.AtLeastRepo() {
if err := cfg.Runtime.CmdBuilder.Show(cfg.Runtime.CmdBuilder.BuildPacmanCmd(ctx,
cmdArgs, cfg.Mode, settings.NoConfirm)); err != nil {
if run.Cfg.Mode.AtLeastRepo() {
if err := run.CmdBuilder.Show(run.CmdBuilder.BuildPacmanCmd(ctx,
cmdArgs, run.Cfg.Mode, settings.NoConfirm)); err != nil {
return err
}
}

if !cfg.Mode.AtLeastAUR() {
if !run.Cfg.Mode.AtLeastAUR() {
return nil
}

Expand All @@ -81,10 +80,10 @@ func syncClean(ctx context.Context, cfg *settings.Configuration, cmdArgs *parser
question = gotext.Get("Do you want to remove all other AUR packages from cache?")
}

fmt.Println(gotext.Get("\nBuild directory:"), cfg.BuildDir)
run.Logger.Println(gotext.Get("\nBuild directory:"), run.Cfg.BuildDir)

if text.ContinueTask(os.Stdin, question, true, settings.NoConfirm) {
if err := cleanAUR(ctx, cfg, keepInstalled, keepCurrent, removeAll, dbExecutor); err != nil {
if run.Logger.ContinueTask(question, true, settings.NoConfirm) {
if err := cleanAUR(ctx, run, keepInstalled, keepCurrent, removeAll, dbExecutor); err != nil {
return err
}
}
Expand All @@ -93,24 +92,24 @@ func syncClean(ctx context.Context, cfg *settings.Configuration, cmdArgs *parser
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)
if run.Logger.ContinueTask(gotext.Get("Do you want to remove ALL untracked AUR files?"), true, settings.NoConfirm) {
return cleanUntracked(ctx, run)
}

return nil
}

func cleanAUR(ctx context.Context, cfg *settings.Configuration,
func cleanAUR(ctx context.Context, run *runtime.Runtime,
keepInstalled, keepCurrent, removeAll bool, dbExecutor db.Executor,
) error {
cfg.Runtime.Logger.Println(gotext.Get("removing AUR packages from cache..."))
run.Logger.Println(gotext.Get("removing AUR packages from cache..."))

installedBases := mapset.NewThreadUnsafeSet[string]()
inAURBases := mapset.NewThreadUnsafeSet[string]()

remotePackages := dbExecutor.InstalledRemotePackages()

files, err := os.ReadDir(cfg.BuildDir)
files, err := os.ReadDir(run.Cfg.BuildDir)
if err != nil {
return err
}
Expand All @@ -130,7 +129,7 @@ func cleanAUR(ctx context.Context, cfg *settings.Configuration,
// Querying the AUR is slow and needs internet so don't do it if we
// don't need to.
if keepCurrent {
info, errInfo := cfg.Runtime.AURClient.Get(ctx, &aur.Query{
info, errInfo := run.AURClient.Get(ctx, &aur.Query{
Needles: cachedPackages,
})
if errInfo != nil {
Expand Down Expand Up @@ -165,20 +164,20 @@ func cleanAUR(ctx context.Context, cfg *settings.Configuration,
}
}

dir := filepath.Join(cfg.BuildDir, file.Name())
cfg.Runtime.Logger.Debugln("removing", dir)
dir := filepath.Join(run.Cfg.BuildDir, file.Name())
run.Logger.Debugln("removing", dir)
if err = os.RemoveAll(dir); err != nil {
cfg.Runtime.Logger.Warnln(gotext.Get("Unable to remove %s: %s", dir, err))
run.Logger.Warnln(gotext.Get("Unable to remove %s: %s", dir, err))
}
}

return nil
}

func cleanUntracked(ctx context.Context, cfg *settings.Configuration) error {
cfg.Runtime.Logger.Println(gotext.Get("removing untracked AUR files from cache..."))
func cleanUntracked(ctx context.Context, run *runtime.Runtime) error {
run.Logger.Println(gotext.Get("removing untracked AUR files from cache..."))

files, err := os.ReadDir(cfg.BuildDir)
files, err := os.ReadDir(run.Cfg.BuildDir)
if err != nil {
return err
}
Expand All @@ -188,12 +187,11 @@ func cleanUntracked(ctx context.Context, cfg *settings.Configuration) error {
continue
}

dir := filepath.Join(cfg.BuildDir, file.Name())
cfg.Runtime.Logger.Debugln("cleaning", dir)
dir := filepath.Join(run.Cfg.BuildDir, file.Name())
run.Logger.Debugln("cleaning", dir)
if isGitRepository(dir) {
if err := cfg.Runtime.CmdBuilder.Show(cfg.Runtime.CmdBuilder.BuildGitCmd(ctx, dir, "clean", "-fx")); err != nil {
cfg.Runtime.Logger.Warnln(gotext.Get("Unable to clean:"), dir)

if err := run.CmdBuilder.Show(run.CmdBuilder.BuildGitCmd(ctx, dir, "clean", "-fx")); err != nil {
run.Logger.Warnln(gotext.Get("Unable to clean:"), dir)
return err
}
}
Expand All @@ -206,29 +204,3 @@ 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,
) {
fmt.Println(gotext.Get("removing untracked AUR files from cache..."))

i := 0
for _, dir := range pkgbuildDirs {
text.OperationInfoln(gotext.Get("Cleaning (%d/%d): %s", i+1, len(pkgbuildDirs), text.Cyan(dir)))

_, stderr, err := cmdBuilder.Capture(
cmdBuilder.BuildGitCmd(
ctx, dir, "reset", "--hard", "HEAD"))
if err != nil {
text.Errorln(gotext.Get("error resetting %s: %s", dir, stderr))
}

if err := config.Runtime.CmdBuilder.Show(
config.Runtime.CmdBuilder.BuildGitCmd(
ctx, dir, "clean", "-fx", "--exclude", "*.pkg.*")); err != nil {
fmt.Fprintln(os.Stderr, err)
}

i++
}
}
7 changes: 3 additions & 4 deletions clean_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/stretchr/testify/require"

"github.com/Jguer/yay/v12/pkg/db/mock"
"github.com/Jguer/yay/v12/pkg/runtime"
"github.com/Jguer/yay/v12/pkg/settings"
"github.com/Jguer/yay/v12/pkg/settings/exe"
"github.com/Jguer/yay/v12/pkg/settings/parser"
Expand Down Expand Up @@ -90,15 +91,13 @@ func TestCleanHanging(t *testing.T) {
Runner: mockRunner,
SudoLoopEnabled: false,
}
cfg := &settings.Configuration{
Runtime: &settings.Runtime{CmdBuilder: cmdBuilder},
}

run := &runtime.Runtime{CmdBuilder: cmdBuilder, Cfg: &settings.Configuration{}}
cmdArgs := parser.MakeArguments()
cmdArgs.AddArg(tc.args...)

err := handleCmd(context.Background(),
cfg, cmdArgs, dbExc,
run, cmdArgs, dbExc,
)

require.NoError(t, err)
Expand Down
Loading
Loading