Skip to content

Commit

Permalink
refactor query builder to include AURClient
Browse files Browse the repository at this point in the history
  • Loading branch information
Jguer committed Nov 13, 2022
1 parent cc8c0a2 commit 01721c8
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 19 deletions.
4 changes: 2 additions & 2 deletions cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ func handleSync(ctx context.Context, cmdArgs *parser.Arguments, dbExecutor db.Ex

switch {
case cmdArgs.ExistsArg("s", "search"):
return syncSearch(ctx, targets, config.Runtime.AURClient, dbExecutor, config.Runtime.QueryBuilder, !cmdArgs.ExistsArg("q", "quiet"))
return syncSearch(ctx, targets, dbExecutor, config.Runtime.QueryBuilder, !cmdArgs.ExistsArg("q", "quiet"))
case cmdArgs.ExistsArg("p", "print", "print-format"):
return config.Runtime.CmdBuilder.Show(config.Runtime.CmdBuilder.BuildPacmanCmd(ctx,
cmdArgs, config.Runtime.Mode, settings.NoConfirm))
Expand Down Expand Up @@ -383,7 +383,7 @@ func handleRemove(ctx context.Context, cmdArgs *parser.Arguments, localCache *vc
func displayNumberMenu(ctx context.Context, pkgS []string, dbExecutor db.Executor,
queryBuilder query.Builder, cmdArgs *parser.Arguments,
) error {
queryBuilder.Execute(ctx, dbExecutor, config.Runtime.AURClient, pkgS)
queryBuilder.Execute(ctx, dbExecutor, pkgS)

if err := queryBuilder.Results(os.Stdout, dbExecutor, query.NumberMenu); err != nil {
return err
Expand Down
7 changes: 5 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,13 @@ func main() {
}

if config.SeparateSources {
config.Runtime.QueryBuilder = query.NewSourceQueryBuilder(config.SortBy,
config.Runtime.QueryBuilder = query.NewSourceQueryBuilder(
config.Runtime.AURClient, config.Runtime.AURCache,
config.SortBy,
config.Runtime.Mode, config.SearchBy, config.BottomUp, config.SingleLineResults)
} else {
config.Runtime.QueryBuilder = query.NewMixedSourceQueryBuilder(config.SortBy,
config.Runtime.QueryBuilder = query.NewMixedSourceQueryBuilder(
config.Runtime.AURClient, config.SortBy,
config.Runtime.Mode, config.SearchBy, config.BottomUp, config.SingleLineResults)
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/dep/depGraph.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func (g *Grapher) GraphFromTargets(ctx context.Context,
)

switch target.DB {
case "":
case "": // unspecified db
if g.dbExecutor.SyncPackage(target.Name) != nil {
graph.AddNode(target.Name)
g.ValidateAndSetNodeInfo(graph, target.Name, &topo.NodeInfo[*InstallInfo]{
Expand Down
10 changes: 7 additions & 3 deletions pkg/query/mixed_sources.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const sourceAUR = "aur"

type Builder interface {
Len() int
Execute(ctx context.Context, dbExecutor db.Executor, aurClient aur.ClientInterface, pkgS []string)
Execute(ctx context.Context, dbExecutor db.Executor, pkgS []string)
Results(w io.Writer, dbExecutor db.Executor, verboseSearch SearchVerbosity) error
GetTargets(include, exclude intrange.IntRanges, otherExclude stringset.StringSet) ([]string, error)
}
Expand All @@ -38,16 +38,20 @@ type MixedSourceQueryBuilder struct {
queryMap map[string]map[string]interface{}
bottomUp bool
singleLineResults bool

aurClient aur.ClientInterface
}

func NewMixedSourceQueryBuilder(
aurClient aur.ClientInterface,
sortBy string,
targetMode parser.TargetMode,
searchBy string,
bottomUp,
singleLineResults bool,
) *MixedSourceQueryBuilder {
return &MixedSourceQueryBuilder{
aurClient: aurClient,
bottomUp: bottomUp,
sortBy: sortBy,
targetMode: targetMode,
Expand Down Expand Up @@ -122,7 +126,7 @@ func (a *abstractResults) Less(i, j int) bool {
return simA > simB
}

func (s *MixedSourceQueryBuilder) Execute(ctx context.Context, dbExecutor db.Executor, aurClient aur.ClientInterface, pkgS []string) {
func (s *MixedSourceQueryBuilder) Execute(ctx context.Context, dbExecutor db.Executor, pkgS []string) {
var aurErr error

pkgS = RemoveInvalidTargets(pkgS, s.targetMode)
Expand All @@ -141,7 +145,7 @@ func (s *MixedSourceQueryBuilder) Execute(ctx context.Context, dbExecutor db.Exe

if s.targetMode.AtLeastAUR() {
var aurResults aurQuery
aurResults, aurErr = queryAUR(ctx, aurClient, pkgS, s.searchBy)
aurResults, aurErr = queryAUR(ctx, s.aurClient, pkgS, s.searchBy)
dbName := sourceAUR

for i := range aurResults {
Expand Down
6 changes: 3 additions & 3 deletions pkg/query/mixed_sources_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ func TestMixedSourceQueryBuilder(t *testing.T) {

for _, tc := range testCases {
t.Run(tc.desc, func(t *testing.T) {
queryBuilder := NewMixedSourceQueryBuilder("votes", parser.ModeAny, "", tc.bottomUp, false)
client, err := aur.NewClient(aur.WithHTTPClient(&mockDoer{}))
queryBuilder := NewMixedSourceQueryBuilder(client, "votes", parser.ModeAny, "", tc.bottomUp, false)
search := []string{"linux"}
mockStore := &mockDB{}

client, err := aur.NewClient(aur.WithHTTPClient(&mockDoer{}))
require.NoError(t, err)
queryBuilder.Execute(context.Background(), mockStore, client, search)
queryBuilder.Execute(context.Background(), mockStore, search)
assert.Len(t, queryBuilder.results, 3)
assert.Equal(t, 3, queryBuilder.Len())

Expand Down
15 changes: 13 additions & 2 deletions pkg/query/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/Jguer/yay/v11/pkg/db"
"github.com/Jguer/yay/v11/pkg/intrange"
"github.com/Jguer/yay/v11/pkg/metadata"
"github.com/Jguer/yay/v11/pkg/settings/parser"
"github.com/Jguer/yay/v11/pkg/stringset"
"github.com/Jguer/yay/v11/pkg/text"
Expand All @@ -34,16 +35,23 @@ type SourceQueryBuilder struct {
targetMode parser.TargetMode
bottomUp bool
singleLineResults bool

aurClient aur.ClientInterface
aurCache *metadata.AURCache
}

func NewSourceQueryBuilder(
aurClient aur.ClientInterface,
aurCache *metadata.AURCache,
sortBy string,
targetMode parser.TargetMode,
searchBy string,
bottomUp,
singleLineResults bool,
) *SourceQueryBuilder {
return &SourceQueryBuilder{
aurClient: aurClient,
aurCache: aurCache,
repoQuery: []alpm.IPackage{},
aurQuery: []aur.Pkg{},
bottomUp: bottomUp,
Expand All @@ -54,13 +62,16 @@ func NewSourceQueryBuilder(
}
}

func (s *SourceQueryBuilder) Execute(ctx context.Context, dbExecutor db.Executor, aurClient aur.ClientInterface, pkgS []string) {
func (s *SourceQueryBuilder) Execute(ctx context.Context,
dbExecutor db.Executor,
pkgS []string,
) {
var aurErr error

pkgS = RemoveInvalidTargets(pkgS, s.targetMode)

if s.targetMode.AtLeastAUR() {
s.aurQuery, aurErr = queryAUR(ctx, aurClient, pkgS, s.searchBy)
s.aurQuery, aurErr = queryAUR(ctx, s.aurClient, pkgS, s.searchBy)
s.aurQuery = filterAURResults(pkgS, s.aurQuery)

sort.Sort(aurSortable{aurQuery: s.aurQuery, sortBy: s.sortBy, bottomUp: s.bottomUp})
Expand Down
9 changes: 5 additions & 4 deletions pkg/query/source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,14 @@ func TestSourceQueryBuilder(t *testing.T) {

for _, tc := range testCases {
t.Run(tc.desc, func(t *testing.T) {
queryBuilder := NewSourceQueryBuilder("votes", parser.ModeAny, "", tc.bottomUp, false)
client, err := aur.NewClient(aur.WithHTTPClient(&mockDoer{}))
require.NoError(t, err)

queryBuilder := NewSourceQueryBuilder(client, nil, "votes", parser.ModeAny, "", tc.bottomUp, false)
search := []string{"linux"}
mockStore := &mockDB{}

client, err := aur.NewClient(aur.WithHTTPClient(&mockDoer{}))
require.NoError(t, err)
queryBuilder.Execute(context.Background(), mockStore, client, search)
queryBuilder.Execute(context.Background(), mockStore, search)
assert.Len(t, queryBuilder.aurQuery, 1)
assert.Len(t, queryBuilder.repoQuery, 2)
assert.Equal(t, 3, queryBuilder.Len())
Expand Down
2 changes: 2 additions & 0 deletions pkg/settings/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/Jguer/aur"
"github.com/Jguer/votar/pkg/vote"

"github.com/Jguer/yay/v11/pkg/db"
"github.com/Jguer/yay/v11/pkg/metadata"
"github.com/Jguer/yay/v11/pkg/query"
"github.com/Jguer/yay/v11/pkg/settings/exe"
Expand All @@ -29,4 +30,5 @@ type Runtime struct {
AURClient *aur.Client
VoteClient *vote.Client
AURCache *metadata.AURCache
DBExecutor db.Executor
}
4 changes: 2 additions & 2 deletions query.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ import (
)

// SyncSearch presents a query to the local repos and to the AUR.
func syncSearch(ctx context.Context, pkgS []string, aurClient aur.ClientInterface,
func syncSearch(ctx context.Context, pkgS []string,
dbExecutor db.Executor, queryBuilder query.Builder, verbose bool,
) error {
queryBuilder.Execute(ctx, dbExecutor, aurClient, pkgS)
queryBuilder.Execute(ctx, dbExecutor, pkgS)

searchMode := query.Minimal
if verbose {
Expand Down

0 comments on commit 01721c8

Please sign in to comment.