Prechádzať zdrojové kódy

fix(query_builder): use correct aur client for mixed query builder (#2041)

* use same repo search as pacman

use logger child from runtime

* use common interface for aur clients
Jo 2 rokov pred
rodič
commit
5d1c54413c

+ 5 - 5
main.go

@@ -117,16 +117,16 @@ func main() {
 
 	if cfg.SeparateSources {
 		cfg.Runtime.QueryBuilder = query.NewSourceQueryBuilder(
-			cfg.Runtime.AURClient, cfg.Runtime.AURCache,
+			cfg.Runtime.AURCache,
 			cfg.Runtime.Logger.Child("querybuilder"), cfg.SortBy,
 			cfg.Mode, cfg.SearchBy, cfg.BottomUp,
-			cfg.SingleLineResults, cfg.NewInstallEngine)
+			cfg.SingleLineResults)
 	} else {
 		cfg.Runtime.QueryBuilder = query.NewMixedSourceQueryBuilder(
-			cfg.Runtime.AURClient, cfg.Runtime.AURCache,
+			cfg.Runtime.AURCache,
 			cfg.Runtime.Logger.Child("mixed.querybuilder"), cfg.SortBy,
 			cfg.Mode, cfg.SearchBy,
-			cfg.BottomUp, cfg.SingleLineResults, cfg.NewInstallEngine)
+			cfg.BottomUp, cfg.SingleLineResults)
 	}
 
 	var useColor bool
@@ -146,7 +146,7 @@ func main() {
 
 	text.UseColor = useColor
 
-	dbExecutor, err := ialpm.NewExecutor(cfg.Runtime.PacmanConf)
+	dbExecutor, err := ialpm.NewExecutor(cfg.Runtime.PacmanConf, runtime.Logger.Child("db"))
 	if err != nil {
 		if str := err.Error(); str != "" {
 			text.Errorln(str)

+ 1 - 1
pkg/cmd/graph/main.go

@@ -33,7 +33,7 @@ func handleCmd() error {
 		return err
 	}
 
-	dbExecutor, err := ialpm.NewExecutor(pacmanConf)
+	dbExecutor, err := ialpm.NewExecutor(pacmanConf, text.GlobalLogger)
 	if err != nil {
 		return err
 	}

+ 20 - 16
pkg/db/ialpm/alpm.go

@@ -22,19 +22,21 @@ type AlpmExecutor struct {
 	syncDB       alpm.IDBList
 	syncDBsCache []alpm.IDB
 	conf         *pacmanconf.Config
+	log          *text.Logger
 
 	installedRemotePkgNames []string
 	installedRemotePkgMap   map[string]alpm.IPackage
 	installedSyncPkgNames   []string
 }
 
-func NewExecutor(pacmanConf *pacmanconf.Config) (*AlpmExecutor, error) {
+func NewExecutor(pacmanConf *pacmanconf.Config, logger *text.Logger) (*AlpmExecutor, error) {
 	ae := &AlpmExecutor{
 		handle:                  nil,
 		localDB:                 nil,
 		syncDB:                  nil,
 		syncDBsCache:            []alpm.IDB{},
 		conf:                    pacmanConf,
+		log:                     logger,
 		installedRemotePkgNames: nil,
 		installedRemotePkgMap:   map[string]alpm.IPackage{},
 		installedSyncPkgNames:   nil,
@@ -141,12 +143,14 @@ func configureAlpm(pacmanConf *pacmanconf.Config, alpmHandle *alpm.Handle) error
 	return alpmHandle.SetCheckSpace(pacmanConf.CheckSpace)
 }
 
-func logCallback(level alpm.LogLevel, str string) {
-	switch level {
-	case alpm.LogWarning:
-		text.Warn(str)
-	case alpm.LogError:
-		text.Error(str)
+func (ae *AlpmExecutor) logCallback() func(level alpm.LogLevel, str string) {
+	return func(level alpm.LogLevel, str string) {
+		switch level {
+		case alpm.LogWarning:
+			ae.log.Warn(str)
+		case alpm.LogError:
+			ae.log.Error(str)
+		}
 	}
 }
 
@@ -184,28 +188,28 @@ func (ae *AlpmExecutor) questionCallback() func(question alpm.QuestionAny) {
 			if dbName != thisDB {
 				dbName = thisDB
 				str += "\n"
-				str += text.SprintOperationInfo(gotext.Get("Repository"), " ", dbName, "\n    ")
+				str += ae.log.SprintOperationInfo(gotext.Get("Repository"), " ", dbName, "\n    ")
 			}
 			str += fmt.Sprintf("%d) %s ", size, pkg.Name())
 			size++
 			return nil
 		})
 
-		text.OperationInfoln(str)
+		ae.log.OperationInfoln(str)
 
 		for {
-			fmt.Println(gotext.Get("\nEnter a number (default=1): "))
+			ae.log.Println(gotext.Get("\nEnter a number (default=1): "))
 
 			// TODO: reenable noconfirm
 			if settings.NoConfirm {
-				fmt.Println()
+				ae.log.Println()
 
 				break
 			}
 
-			numberBuf, err := text.GetInput(os.Stdin, "", false)
+			numberBuf, err := ae.log.GetInput("", false)
 			if err != nil {
-				text.Errorln(err)
+				ae.log.Errorln(err)
 				break
 			}
 
@@ -215,12 +219,12 @@ func (ae *AlpmExecutor) questionCallback() func(question alpm.QuestionAny) {
 
 			num, err := strconv.Atoi(numberBuf)
 			if err != nil {
-				text.Errorln(gotext.Get("invalid number: %s", numberBuf))
+				ae.log.Errorln(gotext.Get("invalid number: %s", numberBuf))
 				continue
 			}
 
 			if num < 1 || num > size {
-				text.Errorln(gotext.Get("invalid value: %d is not between %d and %d", num, 1, size))
+				ae.log.Errorln(gotext.Get("invalid value: %d is not between %d and %d", num, 1, size))
 				continue
 			}
 
@@ -248,7 +252,7 @@ func (ae *AlpmExecutor) RefreshHandle() error {
 	}
 
 	alpmSetQuestionCallback(alpmHandle, ae.questionCallback())
-	alpmSetLogCallback(alpmHandle, logCallback)
+	alpmSetLogCallback(alpmHandle, ae.logCallback())
 	ae.handle = alpmHandle
 	ae.syncDBsCache = nil
 

+ 5 - 1
pkg/db/ialpm/alpm_test.go

@@ -1,11 +1,15 @@
 package ialpm
 
 import (
+	"io"
+	"strings"
 	"testing"
 
 	alpm "github.com/Jguer/go-alpm/v2"
 	"github.com/Morganamilo/go-pacmanconf"
 	"github.com/stretchr/testify/assert"
+
+	"github.com/Jguer/yay/v12/pkg/text"
 )
 
 func TestAlpmExecutor(t *testing.T) {
@@ -41,7 +45,7 @@ func TestAlpmExecutor(t *testing.T) {
 		},
 	}
 
-	aExec, err := NewExecutor(pacmanConf)
+	aExec, err := NewExecutor(pacmanConf, text.NewLogger(io.Discard, strings.NewReader(""), false, "test"))
 	assert.NoError(t, err)
 
 	assert.NotNil(t, aExec.conf)

+ 1 - 3
pkg/db/ialpm/high_level.go

@@ -2,8 +2,6 @@ package ialpm
 
 import (
 	alpm "github.com/Jguer/go-alpm/v2"
-
-	"github.com/Jguer/yay/v12/pkg/text"
 )
 
 // GetPackageNamesBySource returns package names with and without correspondence in SyncDBS respectively.
@@ -18,7 +16,7 @@ func (ae *AlpmExecutor) getPackageNamesBySource() {
 		}
 	}
 
-	text.Debugln("populating db executor package caches.",
+	ae.log.Debugln("populating db executor package caches.",
 		"sync_len", len(ae.installedSyncPkgNames), "remote_len", len(ae.installedRemotePkgNames))
 }
 

+ 1 - 1
pkg/dep/dep_graph.go

@@ -136,7 +136,7 @@ func (g *Grapher) GraphFromTargets(ctx context.Context,
 
 		switch target.DB {
 		case "": // unspecified db
-			if pkg := g.dbExecutor.SyncPackage(target.Name); pkg != nil {
+			if pkg := g.dbExecutor.SyncSatisfier(target.Name); pkg != nil {
 				dbName := pkg.DB().Name()
 				graph.AddNode(pkg.Name())
 				g.ValidateAndSetNodeInfo(graph, pkg.Name(), &topo.NodeInfo[*InstallInfo]{

+ 1 - 8
pkg/query/mixed_sources.go

@@ -9,7 +9,6 @@ import (
 	"strings"
 
 	"github.com/Jguer/aur"
-	"github.com/Jguer/aur/rpc"
 	"github.com/Jguer/go-alpm/v2"
 	"github.com/adrg/strutil"
 	"github.com/adrg/strutil/metrics"
@@ -39,15 +38,12 @@ type MixedSourceQueryBuilder struct {
 	queryMap          map[string]map[string]interface{}
 	bottomUp          bool
 	singleLineResults bool
-	newEngine         bool
 
 	aurClient aur.QueryClient
-	rpcClient rpc.ClientInterface
 	logger    *text.Logger
 }
 
 func NewMixedSourceQueryBuilder(
-	rpcClient rpc.ClientInterface,
 	aurClient aur.QueryClient,
 	logger *text.Logger,
 	sortBy string,
@@ -55,10 +51,8 @@ func NewMixedSourceQueryBuilder(
 	searchBy string,
 	bottomUp,
 	singleLineResults bool,
-	newEngine bool,
 ) *MixedSourceQueryBuilder {
 	return &MixedSourceQueryBuilder{
-		rpcClient:         rpcClient,
 		aurClient:         aurClient,
 		logger:            logger,
 		bottomUp:          bottomUp,
@@ -68,7 +62,6 @@ func NewMixedSourceQueryBuilder(
 		singleLineResults: singleLineResults,
 		queryMap:          map[string]map[string]interface{}{},
 		results:           make([]abstractResult, 0, 100),
-		newEngine:         newEngine,
 	}
 }
 
@@ -155,7 +148,7 @@ func (s *MixedSourceQueryBuilder) Execute(ctx context.Context, dbExecutor db.Exe
 
 	if s.targetMode.AtLeastAUR() {
 		var aurResults aurQuery
-		aurResults, aurErr = queryAUR(ctx, s.rpcClient, s.aurClient, pkgS, s.searchBy, false)
+		aurResults, aurErr = queryAUR(ctx, s.aurClient, pkgS, s.searchBy)
 		dbName := sourceAUR
 
 		for i := range aurResults {

+ 2 - 2
pkg/query/mixed_sources_test.go

@@ -35,9 +35,9 @@ func TestMixedSourceQueryBuilder(t *testing.T) {
 			client, err := rpc.NewClient(rpc.WithHTTPClient(&mockDoer{}))
 
 			w := &strings.Builder{}
-			queryBuilder := NewMixedSourceQueryBuilder(client, client,
+			queryBuilder := NewMixedSourceQueryBuilder(client,
 				text.NewLogger(w, strings.NewReader(""), false, "test"),
-				"votes", parser.ModeAny, "", tc.bottomUp, false, false)
+				"votes", parser.ModeAny, "", tc.bottomUp, false)
 			search := []string{"linux"}
 			mockStore := &mockDB{}
 

+ 6 - 19
pkg/query/source.go

@@ -17,8 +17,6 @@ import (
 	"github.com/Jguer/yay/v12/pkg/settings/parser"
 	"github.com/Jguer/yay/v12/pkg/stringset"
 	"github.com/Jguer/yay/v12/pkg/text"
-
-	"github.com/Jguer/aur/rpc"
 )
 
 type SearchVerbosity int
@@ -37,17 +35,14 @@ type SourceQueryBuilder struct {
 	sortBy            string
 	searchBy          string
 	targetMode        parser.TargetMode
-	useAURCache       bool
 	bottomUp          bool
 	singleLineResults bool
 
-	aurClient rpc.ClientInterface
-	aurCache  aur.QueryClient
-	logger    *text.Logger
+	aurCache aur.QueryClient
+	logger   *text.Logger
 }
 
 func NewSourceQueryBuilder(
-	aurClient rpc.ClientInterface,
 	aurCache aur.QueryClient,
 	logger *text.Logger,
 	sortBy string,
@@ -55,10 +50,8 @@ func NewSourceQueryBuilder(
 	searchBy string,
 	bottomUp,
 	singleLineResults bool,
-	useAURCache bool,
 ) *SourceQueryBuilder {
 	return &SourceQueryBuilder{
-		aurClient:         aurClient,
 		aurCache:          aurCache,
 		logger:            logger,
 		repoQuery:         []alpm.IPackage{},
@@ -68,7 +61,6 @@ func NewSourceQueryBuilder(
 		targetMode:        targetMode,
 		searchBy:          searchBy,
 		singleLineResults: singleLineResults,
-		useAURCache:       useAURCache,
 	}
 }
 
@@ -81,7 +73,7 @@ func (s *SourceQueryBuilder) Execute(ctx context.Context,
 	pkgS = RemoveInvalidTargets(pkgS, s.targetMode)
 
 	if s.targetMode.AtLeastAUR() {
-		s.aurQuery, aurErr = queryAUR(ctx, s.aurClient, s.aurCache, pkgS, s.searchBy, s.useAURCache)
+		s.aurQuery, aurErr = queryAUR(ctx, s.aurCache, pkgS, s.searchBy)
 		s.aurQuery = filterAURResults(pkgS, s.aurQuery)
 
 		sort.Sort(aurSortable{aurQuery: s.aurQuery, sortBy: s.sortBy, bottomUp: s.bottomUp})
@@ -200,21 +192,16 @@ func filterAURResults(pkgS []string, results []aur.Pkg) []aur.Pkg {
 
 // queryAUR searches AUR and narrows based on subarguments.
 func queryAUR(ctx context.Context,
-	rpcClient rpc.ClientInterface, aurClient aur.QueryClient,
-	pkgS []string, searchBy string, newEngine bool,
+	aurClient aur.QueryClient,
+	pkgS []string, searchBy string,
 ) ([]aur.Pkg, error) {
 	var (
 		err error
 		by  = getSearchBy(searchBy)
 	)
 
-	queryClient := aurClient
-	if !newEngine {
-		queryClient = rpcClient
-	}
-
 	for _, word := range pkgS {
-		r, errM := queryClient.Get(ctx, &aur.Query{
+		r, errM := aurClient.Get(ctx, &aur.Query{
 			Needles:  []string{word},
 			By:       by,
 			Contains: true,

+ 2 - 2
pkg/query/source_test.go

@@ -110,9 +110,9 @@ func TestSourceQueryBuilder(t *testing.T) {
 			client, err := rpc.NewClient(rpc.WithHTTPClient(&mockDoer{}))
 			require.NoError(t, err)
 
-			queryBuilder := NewSourceQueryBuilder(client, client,
+			queryBuilder := NewSourceQueryBuilder(client,
 				text.NewLogger(io.Discard, bytes.NewBufferString(""), false, "test"),
-				"votes", parser.ModeAny, "", tc.bottomUp, false, false)
+				"votes", parser.ModeAny, "", tc.bottomUp, false)
 			search := []string{"linux"}
 			mockStore := &mockDB{}