From 5aeb0d696cbea4d4e85e9f00c4d25eb2e725f35d Mon Sep 17 00:00:00 2001 From: jguer Date: Sun, 13 Nov 2022 23:53:37 +0100 Subject: [PATCH] support contains --- main.go | 3 ++- pkg/metadata/metadata_aur.go | 20 +++++++++++++++----- pkg/query/aur_info.go | 1 + pkg/query/mixed_sources.go | 2 +- pkg/query/source.go | 33 ++++++++++++++++++++------------- pkg/query/source_test.go | 2 +- pkg/text/print.go | 2 +- 7 files changed, 41 insertions(+), 22 deletions(-) diff --git a/main.go b/main.go index c764957a..a1751b97 100644 --- a/main.go +++ b/main.go @@ -96,7 +96,8 @@ func main() { config.Runtime.QueryBuilder = query.NewSourceQueryBuilder( config.Runtime.AURClient, config.Runtime.AURCache, config.SortBy, - config.Runtime.Mode, config.SearchBy, config.BottomUp, config.SingleLineResults) + config.Runtime.Mode, config.SearchBy, config.BottomUp, + config.SingleLineResults, config.NewInstallEngine) } else { config.Runtime.QueryBuilder = query.NewMixedSourceQueryBuilder( config.Runtime.AURClient, config.SortBy, diff --git a/pkg/metadata/metadata_aur.go b/pkg/metadata/metadata_aur.go index e8f890ab..3f4bb0bf 100644 --- a/pkg/metadata/metadata_aur.go +++ b/pkg/metadata/metadata_aur.go @@ -29,8 +29,9 @@ type AURCache struct { } type AURQuery struct { - Needles []string - By aur.By + Needles []string + By aur.By + Contains bool // if true, search for packages containing the needle, not exact matches } 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) 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 { - pattern += " or " + pattern += " , " } } } pattern += ")" + if a.DebugLoggerFn != nil { + a.DebugLoggerFn("AUR metadata query", pattern) + } + parsed, err := gojq.Parse(pattern) if err != nil { log.Fatalln(err) @@ -176,7 +186,7 @@ func (a *AURCache) gojqGetBatch(ctx context.Context, query *AURQuery) ([]*aur.Pk } if a.DebugLoggerFn != nil { - a.DebugLoggerFn("AUR Query", pattern, "Found", len(final)) + a.DebugLoggerFn("AUR metadata query found", len(final)) } return final, nil diff --git a/pkg/query/aur_info.go b/pkg/query/aur_info.go index 4b81fa7d..9761ee3e 100644 --- a/pkg/query/aur_info.go +++ b/pkg/query/aur_info.go @@ -32,6 +32,7 @@ func AURInfo(ctx context.Context, aurClient aur.ClientInterface, names []string, makeRequest := func(n, max int) { defer wg.Done() + text.Debugln("AUR RPC:", names[n:max]) tempInfo, requestErr := aurClient.Info(ctx, names[n:max]) if requestErr != nil { errs.Add(requestErr) diff --git a/pkg/query/mixed_sources.go b/pkg/query/mixed_sources.go index 5681bb35..702bea09 100644 --- a/pkg/query/mixed_sources.go +++ b/pkg/query/mixed_sources.go @@ -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, nil, pkgS, s.searchBy) + aurResults, aurErr = queryAUR(ctx, s.aurClient, nil, pkgS, s.searchBy, false) dbName := sourceAUR for i := range aurResults { diff --git a/pkg/query/source.go b/pkg/query/source.go index 0c24f9ed..1452374a 100644 --- a/pkg/query/source.go +++ b/pkg/query/source.go @@ -30,6 +30,8 @@ const ( type SourceQueryBuilder struct { repoQuery aurQuery + + useAURCache bool sortBy string searchBy string targetMode parser.TargetMode @@ -48,6 +50,7 @@ func NewSourceQueryBuilder( searchBy string, bottomUp, singleLineResults bool, + useAURCache bool, ) *SourceQueryBuilder { return &SourceQueryBuilder{ aurClient: aurClient, @@ -59,6 +62,7 @@ func NewSourceQueryBuilder( targetMode: targetMode, searchBy: searchBy, singleLineResults: singleLineResults, + useAURCache: useAURCache, } } @@ -71,7 +75,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.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}) @@ -189,7 +193,7 @@ 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, aurMetadata *metadata.AURCache, - pkgS []string, searchBy string, + pkgS []string, searchBy string, newEngine bool, ) ([]aur.Pkg, error) { var ( err error @@ -199,18 +203,11 @@ func queryAUR(ctx context.Context, for _, word := range pkgS { var r []aur.Pkg - // 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 { + if aurMetadata != nil && newEngine { q, err := aurMetadata.Get(ctx, &metadata.AURQuery{ - Needles: []string{word}, - By: by, + Needles: []string{word}, + By: by, + Contains: true, }) for _, pkg := range q { @@ -219,6 +216,16 @@ func queryAUR(ctx context.Context, if err == 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 } } } diff --git a/pkg/query/source_test.go b/pkg/query/source_test.go index 74aa0d6a..0203c52b 100644 --- a/pkg/query/source_test.go +++ b/pkg/query/source_test.go @@ -108,7 +108,7 @@ func TestSourceQueryBuilder(t *testing.T) { client, err := aur.NewClient(aur.WithHTTPClient(&mockDoer{})) 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"} mockStore := &mockDB{} diff --git a/pkg/text/print.go b/pkg/text/print.go index c87067c7..3f1b5daa 100644 --- a/pkg/text/print.go +++ b/pkg/text/print.go @@ -28,7 +28,7 @@ func Debugln(a ...interface{}) { 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) }