support contains

This commit is contained in:
jguer 2022-11-13 23:53:37 +01:00
parent 56a46644cc
commit 5aeb0d696c
No known key found for this signature in database
GPG Key ID: 6D6CC9BEA8556B35
7 changed files with 41 additions and 22 deletions

View File

@ -96,7 +96,8 @@ func main() {
config.Runtime.QueryBuilder = query.NewSourceQueryBuilder( config.Runtime.QueryBuilder = query.NewSourceQueryBuilder(
config.Runtime.AURClient, config.Runtime.AURCache, config.Runtime.AURClient, config.Runtime.AURCache,
config.SortBy, config.SortBy,
config.Runtime.Mode, config.SearchBy, config.BottomUp, config.SingleLineResults) config.Runtime.Mode, config.SearchBy, config.BottomUp,
config.SingleLineResults, config.NewInstallEngine)
} else { } else {
config.Runtime.QueryBuilder = query.NewMixedSourceQueryBuilder( config.Runtime.QueryBuilder = query.NewMixedSourceQueryBuilder(
config.Runtime.AURClient, config.SortBy, config.Runtime.AURClient, config.SortBy,

View File

@ -29,8 +29,9 @@ type AURCache struct {
} }
type AURQuery struct { type AURQuery struct {
Needles []string Needles []string
By aur.By By aur.By
Contains bool // if true, search for packages containing the needle, not exact matches
} }
func NewAURCache(cachePath string) (*AURCache, error) { func NewAURCache(cachePath string) (*AURCache, error) {
@ -142,15 +143,24 @@ func (a *AURCache) gojqGetBatch(ctx context.Context, query *AURQuery) ([]*aur.Pk
bys := toSearchBy(query.By) bys := toSearchBy(query.By)
for j, by := range bys { for j, by := range bys {
pattern += fmt.Sprintf("(.%s == \"%s\")", by, searchTerm) if query.Contains {
pattern += fmt.Sprintf("(.%s // empty | test(\"%s\"))", by, searchTerm)
} else {
pattern += fmt.Sprintf("(.%s == \"%s\")", by, searchTerm)
}
if j != len(bys)-1 { if j != len(bys)-1 {
pattern += " or " pattern += " , "
} }
} }
} }
pattern += ")" pattern += ")"
if a.DebugLoggerFn != nil {
a.DebugLoggerFn("AUR metadata query", pattern)
}
parsed, err := gojq.Parse(pattern) parsed, err := gojq.Parse(pattern)
if err != nil { if err != nil {
log.Fatalln(err) log.Fatalln(err)
@ -176,7 +186,7 @@ func (a *AURCache) gojqGetBatch(ctx context.Context, query *AURQuery) ([]*aur.Pk
} }
if a.DebugLoggerFn != nil { if a.DebugLoggerFn != nil {
a.DebugLoggerFn("AUR Query", pattern, "Found", len(final)) a.DebugLoggerFn("AUR metadata query found", len(final))
} }
return final, nil return final, nil

View File

@ -32,6 +32,7 @@ func AURInfo(ctx context.Context, aurClient aur.ClientInterface, names []string,
makeRequest := func(n, max int) { makeRequest := func(n, max int) {
defer wg.Done() defer wg.Done()
text.Debugln("AUR RPC:", names[n:max])
tempInfo, requestErr := aurClient.Info(ctx, names[n:max]) tempInfo, requestErr := aurClient.Info(ctx, names[n:max])
if requestErr != nil { if requestErr != nil {
errs.Add(requestErr) errs.Add(requestErr)

View File

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

View File

@ -30,6 +30,8 @@ const (
type SourceQueryBuilder struct { type SourceQueryBuilder struct {
repoQuery repoQuery
aurQuery aurQuery
useAURCache bool
sortBy string sortBy string
searchBy string searchBy string
targetMode parser.TargetMode targetMode parser.TargetMode
@ -48,6 +50,7 @@ func NewSourceQueryBuilder(
searchBy string, searchBy string,
bottomUp, bottomUp,
singleLineResults bool, singleLineResults bool,
useAURCache bool,
) *SourceQueryBuilder { ) *SourceQueryBuilder {
return &SourceQueryBuilder{ return &SourceQueryBuilder{
aurClient: aurClient, aurClient: aurClient,
@ -59,6 +62,7 @@ func NewSourceQueryBuilder(
targetMode: targetMode, targetMode: targetMode,
searchBy: searchBy, searchBy: searchBy,
singleLineResults: singleLineResults, singleLineResults: singleLineResults,
useAURCache: useAURCache,
} }
} }
@ -71,7 +75,7 @@ func (s *SourceQueryBuilder) Execute(ctx context.Context,
pkgS = RemoveInvalidTargets(pkgS, s.targetMode) pkgS = RemoveInvalidTargets(pkgS, s.targetMode)
if s.targetMode.AtLeastAUR() { if s.targetMode.AtLeastAUR() {
s.aurQuery, aurErr = queryAUR(ctx, s.aurClient, s.aurCache, pkgS, s.searchBy) s.aurQuery, aurErr = queryAUR(ctx, s.aurClient, s.aurCache, pkgS, s.searchBy, s.useAURCache)
s.aurQuery = filterAURResults(pkgS, s.aurQuery) s.aurQuery = filterAURResults(pkgS, s.aurQuery)
sort.Sort(aurSortable{aurQuery: s.aurQuery, sortBy: s.sortBy, bottomUp: s.bottomUp}) sort.Sort(aurSortable{aurQuery: s.aurQuery, sortBy: s.sortBy, bottomUp: s.bottomUp})
@ -189,7 +193,7 @@ func filterAURResults(pkgS []string, results []aur.Pkg) []aur.Pkg {
// queryAUR searches AUR and narrows based on subarguments. // queryAUR searches AUR and narrows based on subarguments.
func queryAUR(ctx context.Context, func queryAUR(ctx context.Context,
aurClient aur.ClientInterface, aurMetadata *metadata.AURCache, aurClient aur.ClientInterface, aurMetadata *metadata.AURCache,
pkgS []string, searchBy string, pkgS []string, searchBy string, newEngine bool,
) ([]aur.Pkg, error) { ) ([]aur.Pkg, error) {
var ( var (
err error err error
@ -199,18 +203,11 @@ func queryAUR(ctx context.Context,
for _, word := range pkgS { for _, word := range pkgS {
var r []aur.Pkg var r []aur.Pkg
// if one of the search terms returns a result we start filtering by it if aurMetadata != nil && newEngine {
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{ q, err := aurMetadata.Get(ctx, &metadata.AURQuery{
Needles: []string{word}, Needles: []string{word},
By: by, By: by,
Contains: true,
}) })
for _, pkg := range q { for _, pkg := range q {
@ -219,6 +216,16 @@ func queryAUR(ctx context.Context,
if err == nil { if err == nil {
return r, nil return r, nil
} else {
text.Warnln("AUR Metadata search failed:", err)
}
}
// 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
} }
} }
} }

View File

@ -108,7 +108,7 @@ func TestSourceQueryBuilder(t *testing.T) {
client, err := aur.NewClient(aur.WithHTTPClient(&mockDoer{})) client, err := aur.NewClient(aur.WithHTTPClient(&mockDoer{}))
require.NoError(t, err) require.NoError(t, err)
queryBuilder := NewSourceQueryBuilder(client, nil, "votes", parser.ModeAny, "", tc.bottomUp, false) queryBuilder := NewSourceQueryBuilder(client, nil, "votes", parser.ModeAny, "", tc.bottomUp, false, false)
search := []string{"linux"} search := []string{"linux"}
mockStore := &mockDB{} mockStore := &mockDB{}

View File

@ -28,7 +28,7 @@ func Debugln(a ...interface{}) {
return return
} }
fmt.Fprintln(os.Stdout, append([]interface{}{Bold(yellow("[DEBUG] "))}, a...)...) fmt.Fprintln(os.Stdout, append([]interface{}{Bold(yellow("[DEBUG]"))}, a...)...)
fmt.Fprint(os.Stdout, ResetCode) fmt.Fprint(os.Stdout, ResetCode)
} }