From fa2e726ca6022e57327e68c810c9b222af4c65bb Mon Sep 17 00:00:00 2001 From: Jo Date: Sat, 25 Feb 2023 19:03:27 +0000 Subject: [PATCH] chore(text): use logger in new engine services (#1939) * use logger in vcs * use logger in query builder * use logger in migrations --- main.go | 5 +++-- pkg/dep/dep_graph.go | 4 ++-- pkg/query/mixed_sources.go | 7 +++++-- pkg/query/mixed_sources_test.go | 10 +++++++--- pkg/query/source.go | 7 +++++-- pkg/query/source_test.go | 5 ++++- pkg/settings/config.go | 6 +++++- pkg/settings/migrations.go | 3 +-- pkg/settings/migrations_test.go | 21 ++++++++++++++++++--- pkg/vcs/vcs.go | 14 +++++++++----- pkg/vcs/vcs_test.go | 11 ++++++++++- 11 files changed, 69 insertions(+), 24 deletions(-) diff --git a/main.go b/main.go index d72964a56..acd48d7fd 100644 --- a/main.go +++ b/main.go @@ -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) } diff --git a/pkg/dep/dep_graph.go b/pkg/dep/dep_graph.go index d54a68f2c..22895fd74 100644 --- a/pkg/dep/dep_graph.go +++ b/pkg/dep/dep_graph.go @@ -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 { @@ -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 { diff --git a/pkg/query/mixed_sources.go b/pkg/query/mixed_sources.go index 61cd94ff1..b17fc5af1 100644 --- a/pkg/query/mixed_sources.go +++ b/pkg/query/mixed_sources.go @@ -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, @@ -58,6 +60,7 @@ func NewMixedSourceQueryBuilder( return &MixedSourceQueryBuilder{ rpcClient: rpcClient, aurClient: aurClient, + logger: logger, bottomUp: bottomUp, sortBy: sortBy, targetMode: targetMode, @@ -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")) } } } diff --git a/pkg/query/mixed_sources_test.go b/pkg/query/mixed_sources_test.go index 7f83c1b15..9a52fb982 100644 --- a/pkg/query/mixed_sources_test.go +++ b/pkg/query/mixed_sources_test.go @@ -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" @@ -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{} @@ -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) }) } diff --git a/pkg/query/source.go b/pkg/query/source.go index 0dff1f703..0d31f963c 100644 --- a/pkg/query/source.go +++ b/pkg/query/source.go @@ -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, @@ -58,6 +60,7 @@ func NewSourceQueryBuilder( return &SourceQueryBuilder{ aurClient: aurClient, aurCache: aurCache, + logger: logger, repoQuery: []alpm.IPackage{}, aurQuery: []aur.Pkg{}, bottomUp: bottomUp, @@ -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")) } } diff --git a/pkg/query/source_test.go b/pkg/query/source_test.go index 28d34a77c..e24e25d6e 100644 --- a/pkg/query/source_test.go +++ b/pkg/query/source_test.go @@ -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" @@ -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{} diff --git a/pkg/settings/config.go b/pkg/settings/config.go index e54898cef..4e18495b2 100644 --- a/pkg/settings/config.go +++ b/pkg/settings/config.go @@ -239,6 +239,9 @@ func DefaultConfig(version string) *Configuration { Version: version, Debug: false, UseRPC: true, + Runtime: &Runtime{ + Logger: text.GlobalLogger, + }, } } @@ -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() diff --git a/pkg/settings/migrations.go b/pkg/settings/migrations.go index e40aaf9ec..0f26adb93 100644 --- a/pkg/settings/migrations.go +++ b/pkg/settings/migrations.go @@ -4,7 +4,6 @@ import ( "fmt" "github.com/Jguer/yay/v11/pkg/db" - "github.com/Jguer/yay/v11/pkg/text" "github.com/leonelquinteros/gotext" ) @@ -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 diff --git a/pkg/settings/migrations_test.go b/pkg/settings/migrations_test.go index 4b868834a..706472e87 100644 --- a/pkg/settings/migrations_test.go +++ b/pkg/settings/migrations_test.go @@ -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) { @@ -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 @@ -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)) @@ -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 diff --git a/pkg/vcs/vcs.go b/pkg/vcs/vcs.go index 47c0575c6..abf52d28c 100644 --- a/pkg/vcs/vcs.go +++ b/pkg/vcs/vcs.go @@ -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. @@ -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 @@ -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 "" } @@ -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) @@ -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) } } diff --git a/pkg/vcs/vcs_test.go b/pkg/vcs/vcs_test.go index e8f7ed260..8f5f843a0 100644 --- a/pkg/vcs/vcs_test.go +++ b/pkg/vcs/vcs_test.go @@ -5,8 +5,10 @@ import ( "encoding/json" "errors" "fmt" + "io" "os" "os/exec" + "strings" "testing" gosrc "github.com/Morganamilo/go-srcinfo" @@ -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) { @@ -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) @@ -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, @@ -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) @@ -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, } @@ -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) @@ -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)