yay/pkg/query/source.go

236 lines
5.3 KiB
Go
Raw Normal View History

package query
import (
"context"
"io"
"sort"
"strings"
"github.com/Jguer/aur"
"github.com/Jguer/go-alpm/v2"
"github.com/leonelquinteros/gotext"
"github.com/Jguer/yay/v11/pkg/db"
"github.com/Jguer/yay/v11/pkg/intrange"
"github.com/Jguer/yay/v11/pkg/settings/parser"
"github.com/Jguer/yay/v11/pkg/stringset"
"github.com/Jguer/yay/v11/pkg/text"
2022-11-16 00:25:17 +01:00
"github.com/Jguer/aur/metadata"
)
type SearchVerbosity int
// Verbosity settings for search.
const (
NumberMenu SearchVerbosity = iota
Detailed
Minimal
)
type SourceQueryBuilder struct {
repoQuery
aurQuery
2022-11-13 23:53:37 +01:00
useAURCache bool
sortBy string
searchBy string
targetMode parser.TargetMode
bottomUp bool
singleLineResults bool
aurClient aur.ClientInterface
2022-11-16 00:25:17 +01:00
aurCache *metadata.Client
}
func NewSourceQueryBuilder(
aurClient aur.ClientInterface,
2022-11-16 00:25:17 +01:00
aurCache *metadata.Client,
sortBy string,
targetMode parser.TargetMode,
searchBy string,
bottomUp,
singleLineResults bool,
2022-11-13 23:53:37 +01:00
useAURCache bool,
) *SourceQueryBuilder {
return &SourceQueryBuilder{
aurClient: aurClient,
aurCache: aurCache,
repoQuery: []alpm.IPackage{},
aurQuery: []aur.Pkg{},
bottomUp: bottomUp,
sortBy: sortBy,
targetMode: targetMode,
searchBy: searchBy,
singleLineResults: singleLineResults,
2022-11-13 23:53:37 +01:00
useAURCache: useAURCache,
}
}
func (s *SourceQueryBuilder) Execute(ctx context.Context,
dbExecutor db.Executor,
pkgS []string,
) {
var aurErr error
pkgS = RemoveInvalidTargets(pkgS, s.targetMode)
if s.targetMode.AtLeastAUR() {
2022-11-13 23:53:37 +01:00
s.aurQuery, aurErr = queryAUR(ctx, s.aurClient, s.aurCache, pkgS, s.searchBy, s.useAURCache)
s.aurQuery = filterAURResults(pkgS, s.aurQuery)
sort.Sort(aurSortable{aurQuery: s.aurQuery, sortBy: s.sortBy, bottomUp: s.bottomUp})
}
if s.targetMode.AtLeastRepo() {
s.repoQuery = repoQuery(dbExecutor.SyncPackages(pkgS...))
if s.bottomUp {
s.Reverse()
}
}
if aurErr != nil && len(s.repoQuery) != 0 {
text.Errorln(ErrAURSearch{inner: aurErr})
text.Warnln(gotext.Get("Showing repo packages only"))
}
}
func (s *SourceQueryBuilder) Results(w io.Writer, dbExecutor db.Executor, verboseSearch SearchVerbosity) error {
if s.aurQuery == nil || s.repoQuery == nil {
return ErrNoQuery{}
}
if s.bottomUp {
if s.targetMode.AtLeastAUR() {
s.aurQuery.printSearch(w, len(s.repoQuery)+1, dbExecutor, verboseSearch, s.bottomUp, s.singleLineResults)
}
if s.targetMode.AtLeastRepo() {
s.repoQuery.printSearch(w, dbExecutor, verboseSearch, s.bottomUp, s.singleLineResults)
}
} else {
if s.targetMode.AtLeastRepo() {
s.repoQuery.printSearch(w, dbExecutor, verboseSearch, s.bottomUp, s.singleLineResults)
}
if s.targetMode.AtLeastAUR() {
s.aurQuery.printSearch(w, len(s.repoQuery)+1, dbExecutor, verboseSearch, s.bottomUp, s.singleLineResults)
}
}
return nil
}
func (s *SourceQueryBuilder) Len() int {
return len(s.repoQuery) + len(s.aurQuery)
}
func (s *SourceQueryBuilder) GetTargets(include, exclude intrange.IntRanges,
otherExclude stringset.StringSet,
) ([]string, error) {
isInclude := len(exclude) == 0 && len(otherExclude) == 0
var targets []string
for i, pkg := range s.repoQuery {
var target int
if s.bottomUp {
target = len(s.repoQuery) - i
} else {
target = i + 1
}
if (isInclude && include.Get(target)) || (!isInclude && !exclude.Get(target)) {
targets = append(targets, pkg.DB().Name()+"/"+pkg.Name())
}
}
for i := range s.aurQuery {
var target int
if s.bottomUp {
target = len(s.aurQuery) - i + len(s.repoQuery)
} else {
target = i + 1 + len(s.repoQuery)
}
if (isInclude && include.Get(target)) || (!isInclude && !exclude.Get(target)) {
targets = append(targets, "aur/"+s.aurQuery[i].Name)
}
}
return targets, nil
}
// filter AUR results to remove strings that don't contain all of the search terms.
func filterAURResults(pkgS []string, results []aur.Pkg) []aur.Pkg {
aurPkgs := make([]aur.Pkg, 0, len(results))
matchesSearchTerms := func(pkg *aur.Pkg, terms []string) bool {
for _, pkgN := range terms {
name := strings.ToLower(pkg.Name)
desc := strings.ToLower(pkg.Description)
targ := strings.ToLower(pkgN)
if !(strings.Contains(name, targ) || strings.Contains(desc, targ)) {
return false
}
}
return true
}
for i := range results {
if matchesSearchTerms(&results[i], pkgS) {
aurPkgs = append(aurPkgs, results[i])
}
}
return aurPkgs
}
// queryAUR searches AUR and narrows based on subarguments.
2022-11-13 17:47:19 +01:00
func queryAUR(ctx context.Context,
2022-11-16 00:25:17 +01:00
aurClient aur.ClientInterface, aurMetadata *metadata.Client,
2022-11-13 23:53:37 +01:00
pkgS []string, searchBy string, newEngine bool,
2022-11-13 17:47:19 +01:00
) ([]aur.Pkg, error) {
var (
err error
by = getSearchBy(searchBy)
)
for _, word := range pkgS {
var r []aur.Pkg
2022-11-13 23:53:37 +01:00
if aurMetadata != nil && newEngine {
2022-11-15 15:44:50 +01:00
q, errM := aurMetadata.Get(ctx, &metadata.AURQuery{
2022-11-13 23:53:37 +01:00
Needles: []string{word},
By: by,
Contains: true,
2022-11-13 17:47:19 +01:00
})
for _, pkg := range q {
r = append(r, *pkg)
}
2022-11-15 15:44:50 +01:00
if errM == nil {
2022-11-13 17:47:19 +01:00
return r, nil
2022-11-13 23:53:37 +01:00
}
2022-11-15 15:44:50 +01:00
text.Warnln("AUR Metadata search failed:", err)
2022-11-13 23:53:37 +01:00
}
// if one of the search terms returns a result we start filtering by it
if aurClient != nil {
text.Debugln("AUR RPC:", by, word)
if r, err = aurClient.Search(ctx, word, by); err == nil {
return r, nil
2022-11-13 17:47:19 +01:00
}
}
}
return nil, err
}