Skip to content

Commit

Permalink
chore(text): use logger in new engine services (#1939)
Browse files Browse the repository at this point in the history
* use logger in vcs

* use logger in query builder

* use logger in migrations
  • Loading branch information
Jguer authored Feb 25, 2023
1 parent 841395c commit fa2e726
Show file tree
Hide file tree
Showing 11 changed files with 69 additions and 24 deletions.
5 changes: 3 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,13 @@ func main() {
if config.SeparateSources {
config.Runtime.QueryBuilder = query.NewSourceQueryBuilder(
config.Runtime.AURClient, config.Runtime.AURCache,
config.SortBy,
config.Runtime.Logger.Child("querybuilder"), config.SortBy,
config.Runtime.Mode, config.SearchBy, config.BottomUp,
config.SingleLineResults, config.NewInstallEngine)
} else {
config.Runtime.QueryBuilder = query.NewMixedSourceQueryBuilder(
config.Runtime.AURClient, config.Runtime.AURCache, config.SortBy,
config.Runtime.AURClient, config.Runtime.AURCache,
config.Runtime.Logger.Child("mixed.querybuilder"), config.SortBy,
config.Runtime.Mode, config.SearchBy,
config.BottomUp, config.SingleLineResults, config.NewInstallEngine)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/dep/dep_graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ func (g *Grapher) GraphFromAUR(ctx context.Context,

aurPkgs, errCache := g.aurClient.Get(ctx, &aurc.Query{By: aurc.Name, Needles: targets})
if errCache != nil {
text.Errorln(errCache)
g.logger.Errorln(errCache)
}

for i := range aurPkgs {
Expand Down Expand Up @@ -424,7 +424,7 @@ func (g *Grapher) findDepsFromAUR(ctx context.Context,
By: aurc.Name, Needles: missingNeedles, Contains: false,
})
if errCache != nil {
text.Errorln(errCache)
g.logger.Errorln(errCache)
}

for i := range aurPkgs {
Expand Down
7 changes: 5 additions & 2 deletions pkg/query/mixed_sources.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,13 @@ type MixedSourceQueryBuilder struct {

aurClient aur.QueryClient
rpcClient rpc.ClientInterface
logger *text.Logger
}

func NewMixedSourceQueryBuilder(
rpcClient rpc.ClientInterface,
aurClient aur.QueryClient,
logger *text.Logger,
sortBy string,
targetMode parser.TargetMode,
searchBy string,
Expand All @@ -58,6 +60,7 @@ func NewMixedSourceQueryBuilder(
return &MixedSourceQueryBuilder{
rpcClient: rpcClient,
aurClient: aurClient,
logger: logger,
bottomUp: bottomUp,
sortBy: sortBy,
targetMode: targetMode,
Expand Down Expand Up @@ -205,10 +208,10 @@ func (s *MixedSourceQueryBuilder) Execute(ctx context.Context, dbExecutor db.Exe
s.results = sortableResults.results

if aurErr != nil {
text.Errorln(ErrAURSearch{inner: aurErr})
s.logger.Errorln(ErrAURSearch{inner: aurErr})

if len(repoResults) != 0 {
text.Warnln(gotext.Get("Showing repo packages only"))
s.logger.Warnln(gotext.Get("Showing repo packages only"))
}
}
}
Expand Down
10 changes: 7 additions & 3 deletions pkg/query/mixed_sources_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/Jguer/aur/rpc"

"github.com/Jguer/yay/v11/pkg/settings/parser"
"github.com/Jguer/yay/v11/pkg/text"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand All @@ -32,7 +33,11 @@ func TestMixedSourceQueryBuilder(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.desc, func(t *testing.T) {
client, err := rpc.NewClient(rpc.WithHTTPClient(&mockDoer{}))
queryBuilder := NewMixedSourceQueryBuilder(client, client, "votes", parser.ModeAny, "", tc.bottomUp, false, false)

w := &strings.Builder{}
queryBuilder := NewMixedSourceQueryBuilder(client, client,
text.NewLogger(w, strings.NewReader(""), false, "test"),
"votes", parser.ModeAny, "", tc.bottomUp, false, false)
search := []string{"linux"}
mockStore := &mockDB{}

Expand All @@ -51,11 +56,10 @@ func TestMixedSourceQueryBuilder(t *testing.T) {
assert.Equal(t, "linux", queryBuilder.results[0].name)
}

w := &strings.Builder{}
queryBuilder.Results(w, mockStore, Detailed)

wString := w.String()
require.GreaterOrEqual(t, len(wString), 1)
require.GreaterOrEqual(t, len(wString), 1, wString)
assert.Equal(t, tc.want, wString)
})
}
Expand Down
7 changes: 5 additions & 2 deletions pkg/query/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,13 @@ type SourceQueryBuilder struct {

aurClient rpc.ClientInterface
aurCache aur.QueryClient
logger *text.Logger
}

func NewSourceQueryBuilder(
aurClient rpc.ClientInterface,
aurCache aur.QueryClient,
logger *text.Logger,
sortBy string,
targetMode parser.TargetMode,
searchBy string,
Expand All @@ -58,6 +60,7 @@ func NewSourceQueryBuilder(
return &SourceQueryBuilder{
aurClient: aurClient,
aurCache: aurCache,
logger: logger,
repoQuery: []alpm.IPackage{},
aurQuery: []aur.Pkg{},
bottomUp: bottomUp,
Expand Down Expand Up @@ -93,8 +96,8 @@ func (s *SourceQueryBuilder) Execute(ctx context.Context,
}

if aurErr != nil && len(s.repoQuery) != 0 {
text.Errorln(ErrAURSearch{inner: aurErr})
text.Warnln(gotext.Get("Showing repo packages only"))
s.logger.Errorln(ErrAURSearch{inner: aurErr})
s.logger.Warnln(gotext.Get("Showing repo packages only"))
}
}

Expand Down
5 changes: 4 additions & 1 deletion pkg/query/source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/Jguer/yay/v11/pkg/db"
"github.com/Jguer/yay/v11/pkg/db/mock"
"github.com/Jguer/yay/v11/pkg/settings/parser"
"github.com/Jguer/yay/v11/pkg/text"

"github.com/Jguer/go-alpm/v2"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -109,7 +110,9 @@ func TestSourceQueryBuilder(t *testing.T) {
client, err := rpc.NewClient(rpc.WithHTTPClient(&mockDoer{}))
require.NoError(t, err)

queryBuilder := NewSourceQueryBuilder(client, nil, "votes", parser.ModeAny, "", tc.bottomUp, false, false)
queryBuilder := NewSourceQueryBuilder(client, client,
text.NewLogger(io.Discard, bytes.NewBufferString(""), false, "test"),
"votes", parser.ModeAny, "", tc.bottomUp, false, false)
search := []string{"linux"}
mockStore := &mockDB{}

Expand Down
6 changes: 5 additions & 1 deletion pkg/settings/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,9 @@ func DefaultConfig(version string) *Configuration {
Version: version,
Debug: false,
UseRPC: true,
Runtime: &Runtime{
Logger: text.GlobalLogger,
},
}
}

Expand Down Expand Up @@ -331,7 +334,8 @@ func NewConfig(version string) (*Configuration, error) {
}

newConfig.Runtime.VCSStore = vcs.NewInfoStore(
filepath.Join(cacheHome, vcsFileName), newConfig.Runtime.CmdBuilder)
filepath.Join(cacheHome, vcsFileName), newConfig.Runtime.CmdBuilder,
newConfig.Runtime.Logger.Child("vcs"))

err := newConfig.Runtime.VCSStore.Load()

Expand Down
3 changes: 1 addition & 2 deletions pkg/settings/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"

"github.com/Jguer/yay/v11/pkg/db"
"github.com/Jguer/yay/v11/pkg/text"

"github.com/leonelquinteros/gotext"
)
Expand Down Expand Up @@ -51,7 +50,7 @@ func (c *Configuration) RunMigrations(migrations []configMigration, configPath s
for _, migration := range migrations {
if db.VerCmp(migration.TargetVersion(), c.Version) > 0 {
if migration.Do(c) {
text.Infoln("Config migration executed (",
c.Runtime.Logger.Infoln("Config migration executed (",
migration.TargetVersion(), "):", migration)

saveConfig = true
Expand Down
21 changes: 18 additions & 3 deletions pkg/settings/migrations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@ package settings

import (
"encoding/json"
"io"
"os"
"strings"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/Jguer/yay/v11/pkg/text"
)

func TestMigrationNothingToDo(t *testing.T) {
Expand All @@ -21,7 +25,10 @@ func TestMigrationNothingToDo(t *testing.T) {
config := Configuration{
Version: "99.0.0",
// Create runtime with runtimeVersion
Runtime: &Runtime{Version: "20.0.0"},
Runtime: &Runtime{
Version: "20.0.0",
Logger: text.NewLogger(io.Discard, strings.NewReader(""), false, "test"),
},
}

// Run Migration
Expand All @@ -42,7 +49,12 @@ func TestMigrationNothingToDo(t *testing.T) {

func TestProvidesMigrationDo(t *testing.T) {
migration := &configProviderMigration{}
config := Configuration{Provides: true}
config := Configuration{
Provides: true,
Runtime: &Runtime{
Logger: text.NewLogger(io.Discard, strings.NewReader(""), false, "test"),
},
}

assert.True(t, migration.Do(&config))

Expand Down Expand Up @@ -120,7 +132,10 @@ func TestProvidesMigration(t *testing.T) {
Version: tc.testConfig.Version,
Provides: tc.testConfig.Provides,
// Create runtime with runtimeVersion
Runtime: &Runtime{Version: tc.testConfig.Runtime.Version},
Runtime: &Runtime{
Logger: text.NewLogger(io.Discard, strings.NewReader(""), false, "test"),
Version: tc.testConfig.Runtime.Version,
},
}

// Run Migration
Expand Down
14 changes: 9 additions & 5 deletions pkg/vcs/vcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ type InfoStore struct {
FilePath string
CmdBuilder exe.GitCmdBuilder
mux sync.Mutex
logger *text.Logger
}

// OriginInfoByURL stores the OriginInfo of each origin URL provided.
Expand All @@ -62,12 +63,15 @@ type OriginInfo struct {
SHA string `json:"sha"`
}

func NewInfoStore(filePath string, cmdBuilder exe.GitCmdBuilder) *InfoStore {
func NewInfoStore(filePath string, cmdBuilder exe.GitCmdBuilder,
logger *text.Logger,
) *InfoStore {
infoStore := &InfoStore{
CmdBuilder: cmdBuilder,
FilePath: filePath,
OriginsByPackage: map[string]OriginInfoByURL{},
mux: sync.Mutex{},
logger: logger,
}

return infoStore
Expand All @@ -87,11 +91,11 @@ func (v *InfoStore) getCommit(ctx context.Context, url, branch string, protocols
if err != nil {
exitError := &exec.ExitError{}
if ok := errors.As(err, &exitError); ok && exitError.ExitCode() == 128 {
text.Warnln(gotext.Get("devel check for package failed: '%s' encountered an error", cmd.String()))
v.logger.Warnln(gotext.Get("devel check for package failed: '%s' encountered an error", cmd.String()))
return ""
}

text.Warnln(err)
v.logger.Warnln(err)

return ""
}
Expand Down Expand Up @@ -135,7 +139,7 @@ func (v *InfoStore) Update(ctx context.Context, pkgName string, sources []gosrc.

v.OriginsByPackage[pkgName] = info

text.Warnln(gotext.Get("Found git repo: %s", text.Cyan(url)))
v.logger.Warnln(gotext.Get("Found git repo: %s", text.Cyan(url)))

if err := v.Save(); err != nil {
fmt.Fprintln(os.Stderr, err)
Expand Down Expand Up @@ -320,7 +324,7 @@ func (v *InfoStore) CleanOrphans(pkgs map[string]alpm.IPackage) {

for pkgName := range v.OriginsByPackage {
if _, ok := pkgs[pkgName]; !ok {
text.Debugln("removing orphaned vcs package:", pkgName)
v.logger.Debugln("removing orphaned vcs package:", pkgName)
missing = append(missing, pkgName)
}
}
Expand Down
11 changes: 10 additions & 1 deletion pkg/vcs/vcs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import (
"encoding/json"
"errors"
"fmt"
"io"
"os"
"os/exec"
"strings"
"testing"

gosrc "github.com/Morganamilo/go-srcinfo"
Expand All @@ -16,6 +18,7 @@ import (

"github.com/Jguer/yay/v11/pkg/db"
"github.com/Jguer/yay/v11/pkg/settings/exe"
"github.com/Jguer/yay/v11/pkg/text"
)

func TestParsing(t *testing.T) {
Expand Down Expand Up @@ -76,7 +79,8 @@ func TestNewInfoStore(t *testing.T) {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
got := NewInfoStore(tt.args.filePath, tt.args.cmdBuilder)
got := NewInfoStore(tt.args.filePath, tt.args.cmdBuilder,
text.NewLogger(io.Discard, strings.NewReader(""), true, "test"))
assert.NotNil(t, got)
assert.Equal(t, []string{"--a", "--b"}, got.CmdBuilder.(*exe.CmdBuilder).GitFlags)
assert.Equal(t, tt.args.cmdBuilder, got.CmdBuilder)
Expand Down Expand Up @@ -223,6 +227,7 @@ func TestInfoStoreToUpgrade(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
v := &InfoStore{
logger: text.GlobalLogger,
CmdBuilder: tt.fields.CmdBuilder,
OriginsByPackage: map[string]OriginInfoByURL{
"yay": tt.args.infos,
Expand Down Expand Up @@ -355,6 +360,7 @@ func TestInfoStore_NeedsUpdate(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
v := &InfoStore{
logger: text.GlobalLogger,
CmdBuilder: tt.fields.CmdBuilder,
}
got := v.needsUpdate(context.Background(), tt.args.infos)
Expand Down Expand Up @@ -404,6 +410,7 @@ func TestInfoStore_Update(t *testing.T) {
t.Parallel()
v := &InfoStore{
OriginsByPackage: tt.fields.OriginsByPackage,
logger: text.GlobalLogger,
FilePath: filePath,
CmdBuilder: tt.fields.CmdBuilder,
}
Expand Down Expand Up @@ -467,6 +474,7 @@ func TestInfoStore_Remove(t *testing.T) {
t.Parallel()
v := &InfoStore{
OriginsByPackage: tt.fields.OriginsByPackage,
logger: text.GlobalLogger,
FilePath: filePath,
}
v.RemovePackages(tt.args.pkgs)
Expand Down Expand Up @@ -515,6 +523,7 @@ func TestInfoStore_CleanOrphans(t *testing.T) {
v := &InfoStore{
OriginsByPackage: tt.fields.OriginsByPackage,
FilePath: filePath,
logger: text.NewLogger(io.Discard, strings.NewReader(""), false, "test"),
}
v.CleanOrphans(tt.args.pkgs)
assert.Len(t, tt.fields.OriginsByPackage, 3)
Expand Down

0 comments on commit fa2e726

Please sign in to comment.