metadata respect search by

This commit is contained in:
jguer 2022-11-13 17:47:19 +01:00
parent 01721c816c
commit 56a46644cc
No known key found for this signature in database
GPG key ID: 6D6CC9BEA8556B35
5 changed files with 60 additions and 27 deletions

View file

@ -7,6 +7,7 @@ import (
"os"
"strconv"
aurc "github.com/Jguer/aur"
"github.com/Jguer/yay/v11/pkg/db"
"github.com/Jguer/yay/v11/pkg/metadata"
aur "github.com/Jguer/yay/v11/pkg/query"
@ -219,7 +220,7 @@ func (g *Grapher) GraphFromAURCache(ctx context.Context,
}
for _, target := range targets {
aurPkgs, _ := g.aurCache.Get(ctx, &metadata.AURQuery{ByName: true, Needles: []string{target}})
aurPkgs, _ := g.aurCache.Get(ctx, &metadata.AURQuery{By: aurc.Name, Needles: []string{target}})
if len(aurPkgs) == 0 {
text.Errorln("No AUR package found for", target)

View file

@ -29,10 +29,8 @@ type AURCache struct {
}
type AURQuery struct {
ByProvides bool // Returns multiple results of different bases
ByBase bool // Returns multiple results of the same base
ByName bool // Returns only 1 or 0 results
Needles []string
Needles []string
By aur.By
}
func NewAURCache(cachePath string) (*AURCache, error) {
@ -136,28 +134,19 @@ func (a *AURCache) FindPackage(ctx context.Context, needle string) ([]*aur.Pkg,
func (a *AURCache) gojqGetBatch(ctx context.Context, query *AURQuery) ([]*aur.Pkg, error) {
pattern := ".[] | select("
for i, searchTerm := range query.Needles {
if i != 0 {
pattern += " or "
}
if query.ByName {
pattern += fmt.Sprintf("(.Name == \"%s\")", searchTerm)
if query.ByBase || query.ByProvides {
bys := toSearchBy(query.By)
for j, by := range bys {
pattern += fmt.Sprintf("(.%s == \"%s\")", by, searchTerm)
if j != len(bys)-1 {
pattern += " or "
}
}
if query.ByBase {
pattern += fmt.Sprintf("(.PackageBase == \"%s\")", searchTerm)
if query.ByProvides {
pattern += " or "
}
}
if query.ByProvides {
pattern += fmt.Sprintf("(.Provides[]? == \"%s\")", searchTerm)
}
}
pattern += ")"
@ -217,7 +206,6 @@ func (a *AURCache) gojqGet(ctx context.Context, searchTerm string) ([]*aur.Pkg,
}
func makeGoJQ() *gojq.Code {
// pattern := ".[] | select((.PackageBase == $x) or (.Name == $x) or (.Provides[]? == ($x)))"
pattern := ".[] | select((.Name == $x) or (.Provides[]? == ($x)))"
query, err := gojq.Parse(pattern)
@ -232,3 +220,26 @@ func makeGoJQ() *gojq.Code {
return compiled
}
func toSearchBy(by aur.By) []string {
switch by {
case aur.Name:
return []string{"Name"}
case aur.NameDesc:
return []string{"Name", "Description"}
case aur.Maintainer:
return []string{"Maintainer"}
case aur.Depends:
return []string{"Depends[]?"}
case aur.MakeDepends:
return []string{"MakeDepends[]?"}
case aur.OptDepends:
return []string{"OptDepends[]?"}
case aur.CheckDepends:
return []string{"CheckDepends[]?"}
case aur.None:
return []string{"Name", "Provides[]?"}
default:
panic("invalid By")
}
}

View file

@ -145,7 +145,7 @@ func (s *MixedSourceQueryBuilder) Execute(ctx context.Context, dbExecutor db.Exe
if s.targetMode.AtLeastAUR() {
var aurResults aurQuery
aurResults, aurErr = queryAUR(ctx, s.aurClient, pkgS, s.searchBy)
aurResults, aurErr = queryAUR(ctx, s.aurClient, nil, pkgS, s.searchBy)
dbName := sourceAUR
for i := range aurResults {

View file

@ -71,7 +71,7 @@ func (s *SourceQueryBuilder) Execute(ctx context.Context,
pkgS = RemoveInvalidTargets(pkgS, s.targetMode)
if s.targetMode.AtLeastAUR() {
s.aurQuery, aurErr = queryAUR(ctx, s.aurClient, pkgS, s.searchBy)
s.aurQuery, aurErr = queryAUR(ctx, s.aurClient, s.aurCache, pkgS, s.searchBy)
s.aurQuery = filterAURResults(pkgS, s.aurQuery)
sort.Sort(aurSortable{aurQuery: s.aurQuery, sortBy: s.sortBy, bottomUp: s.bottomUp})
@ -187,7 +187,10 @@ func filterAURResults(pkgS []string, results []aur.Pkg) []aur.Pkg {
}
// queryAUR searches AUR and narrows based on subarguments.
func queryAUR(ctx context.Context, aurClient aur.ClientInterface, pkgS []string, searchBy string) ([]aur.Pkg, error) {
func queryAUR(ctx context.Context,
aurClient aur.ClientInterface, aurMetadata *metadata.AURCache,
pkgS []string, searchBy string,
) ([]aur.Pkg, error) {
var (
err error
by = getSearchBy(searchBy)
@ -196,9 +199,27 @@ func queryAUR(ctx context.Context, aurClient aur.ClientInterface, pkgS []string,
for _, word := range pkgS {
var r []aur.Pkg
r, err = aurClient.Search(ctx, word, by)
if err == nil {
return r, nil
// if one of the search terms returns a result we start filtering by it
if aurClient != nil {
r, err = aurClient.Search(ctx, word, by)
if err == nil {
return r, nil
}
}
if aurMetadata != nil {
q, err := aurMetadata.Get(ctx, &metadata.AURQuery{
Needles: []string{word},
By: by,
})
for _, pkg := range q {
r = append(r, *pkg)
}
if err == nil {
return r, nil
}
}
}

View file

@ -74,7 +74,7 @@ func upList(ctx context.Context, aurCache *metadata.AURCache,
var _aurdata []*aur.Pkg
if aurCache != nil {
_aurdata, err = aurCache.Get(ctx, &metadata.AURQuery{ByName: true, Needles: remoteNames})
_aurdata, err = aurCache.Get(ctx, &metadata.AURQuery{Needles: remoteNames, By: aur.Name})
} else {
_aurdata, err = query.AURInfo(ctx, config.Runtime.AURClient, remoteNames, warnings, config.RequestSplitN)
}