refactor: migrate repos find to struct
This commit is contained in:
parent
12d83f2015
commit
e1829c4824
@ -15,7 +15,6 @@
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package repos
|
||||
|
||||
import (
|
||||
@ -24,10 +23,7 @@ import (
|
||||
"plemya-x.ru/alr/internal/db"
|
||||
)
|
||||
|
||||
// FindPkgs looks for packages matching the inputs inside the database.
|
||||
// It returns a map that maps the package name input to any packages found for it.
|
||||
// It also returns a slice that contains the names of all packages that were not found.
|
||||
func FindPkgs(ctx context.Context, pkgs []string) (map[string][]db.Package, []string, error) {
|
||||
func (rs *Repos) FindPkgs(ctx context.Context, pkgs []string) (map[string][]db.Package, []string, error) {
|
||||
found := map[string][]db.Package{}
|
||||
notFound := []string(nil)
|
||||
|
||||
@ -36,7 +32,7 @@ func FindPkgs(ctx context.Context, pkgs []string) (map[string][]db.Package, []st
|
||||
continue
|
||||
}
|
||||
|
||||
result, err := db.GetPkgs(ctx, "json_array_contains(provides, ?)", pkgName)
|
||||
result, err := rs.db.GetPkgs(ctx, "json_array_contains(provides, ?)", pkgName)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
@ -55,7 +51,7 @@ func FindPkgs(ctx context.Context, pkgs []string) (map[string][]db.Package, []st
|
||||
result.Close()
|
||||
|
||||
if added == 0 {
|
||||
result, err := db.GetPkgs(ctx, "name LIKE ?", pkgName)
|
||||
result, err := rs.db.GetPkgs(ctx, "name LIKE ?", pkgName)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
@ -18,9 +18,7 @@
|
||||
|
||||
package repos_test
|
||||
|
||||
/*
|
||||
import (
|
||||
"context"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
@ -31,18 +29,15 @@ import (
|
||||
)
|
||||
|
||||
func TestFindPkgs(t *testing.T) {
|
||||
_, err := db.Open(":memory:")
|
||||
if err != nil {
|
||||
t.Fatalf("Expected no error, got %s", err)
|
||||
}
|
||||
defer db.Close()
|
||||
e := prepare(t)
|
||||
defer cleanup(t, e)
|
||||
|
||||
setCfgDirs(t)
|
||||
defer removeCacheDir(t)
|
||||
rs := repos.New(
|
||||
e.Cfg,
|
||||
e.Db,
|
||||
)
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
err = repos.Pull(ctx, []types.Repo{
|
||||
err := rs.Pull(e.Ctx, []types.Repo{
|
||||
{
|
||||
Name: "default",
|
||||
URL: "https://gitea.plemya-x.ru/xpamych/xpamych-alr-repo.git",
|
||||
@ -52,7 +47,10 @@ func TestFindPkgs(t *testing.T) {
|
||||
t.Fatalf("Expected no error, got %s", err)
|
||||
}
|
||||
|
||||
found, notFound, err := repos.FindPkgs([]string{"itd", "nonexistentpackage1", "nonexistentpackage2"})
|
||||
found, notFound, err := rs.FindPkgs(
|
||||
e.Ctx,
|
||||
[]string{"alr", "nonexistentpackage1", "nonexistentpackage2"},
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatalf("Expected no error, got %s", err)
|
||||
}
|
||||
@ -65,33 +63,32 @@ func TestFindPkgs(t *testing.T) {
|
||||
t.Errorf("Expected 1 package found, got %d", len(found))
|
||||
}
|
||||
|
||||
itdPkgs, ok := found["itd"]
|
||||
alrPkgs, ok := found["alr"]
|
||||
if !ok {
|
||||
t.Fatalf("Expected 'itd' packages to be found")
|
||||
t.Fatalf("Expected 'alr' packages to be found")
|
||||
}
|
||||
|
||||
if len(itdPkgs) < 2 {
|
||||
t.Errorf("Expected two 'itd' packages to be found")
|
||||
if len(alrPkgs) < 2 {
|
||||
t.Errorf("Expected two 'alr' packages to be found")
|
||||
}
|
||||
|
||||
for i, pkg := range itdPkgs {
|
||||
if !strings.HasPrefix(pkg.Name, "itd") {
|
||||
t.Errorf("Expected package name of all found packages to start with 'itd', got %s on element %d", pkg.Name, i)
|
||||
for i, pkg := range alrPkgs {
|
||||
if !strings.HasPrefix(pkg.Name, "alr") {
|
||||
t.Errorf("Expected package name of all found packages to start with 'alr', got %s on element %d", pkg.Name, i)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestFindPkgsEmpty(t *testing.T) {
|
||||
_, err := db.Open(":memory:")
|
||||
if err != nil {
|
||||
t.Fatalf("Expected no error, got %s", err)
|
||||
}
|
||||
defer db.Close()
|
||||
e := prepare(t)
|
||||
defer cleanup(t, e)
|
||||
|
||||
setCfgDirs(t)
|
||||
defer removeCacheDir(t)
|
||||
rs := repos.New(
|
||||
e.Cfg,
|
||||
e.Db,
|
||||
)
|
||||
|
||||
err = db.InsertPackage(db.Package{
|
||||
err := e.Db.InsertPackage(e.Ctx, db.Package{
|
||||
Name: "test1",
|
||||
Repository: "default",
|
||||
Version: "0.0.1",
|
||||
@ -106,7 +103,7 @@ func TestFindPkgsEmpty(t *testing.T) {
|
||||
t.Fatalf("Expected no error, got %s", err)
|
||||
}
|
||||
|
||||
err = db.InsertPackage(db.Package{
|
||||
err = e.Db.InsertPackage(e.Ctx, db.Package{
|
||||
Name: "test2",
|
||||
Repository: "default",
|
||||
Version: "0.0.1",
|
||||
@ -121,7 +118,7 @@ func TestFindPkgsEmpty(t *testing.T) {
|
||||
t.Fatalf("Expected no error, got %s", err)
|
||||
}
|
||||
|
||||
found, notFound, err := repos.FindPkgs([]string{"test", ""})
|
||||
found, notFound, err := rs.FindPkgs(e.Ctx, []string{"test", ""})
|
||||
if err != nil {
|
||||
t.Fatalf("Expected no error, got %s", err)
|
||||
}
|
||||
@ -147,4 +144,3 @@ func TestFindPkgsEmpty(t *testing.T) {
|
||||
t.Errorf("Expected 'test2' package, got '%s'", testPkgs[0].Name)
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
@ -5,6 +5,7 @@ import (
|
||||
"sync"
|
||||
|
||||
"plemya-x.ru/alr/internal/config"
|
||||
"plemya-x.ru/alr/internal/db"
|
||||
database "plemya-x.ru/alr/internal/db"
|
||||
"plemya-x.ru/alr/internal/types"
|
||||
)
|
||||
@ -19,6 +20,15 @@ func Pull(ctx context.Context, repos []types.Repo) error {
|
||||
return GetInstance(ctx).Pull(ctx, repos)
|
||||
}
|
||||
|
||||
// FindPkgs looks for packages matching the inputs inside the database.
|
||||
// It returns a map that maps the package name input to any packages found for it.
|
||||
// It also returns a slice that contains the names of all packages that were not found.
|
||||
//
|
||||
// Deprecated: use struct method
|
||||
func FindPkgs(ctx context.Context, pkgs []string) (map[string][]db.Package, []string, error) {
|
||||
return GetInstance(ctx).FindPkgs(ctx, pkgs)
|
||||
}
|
||||
|
||||
// =======================
|
||||
// FOR LEGACY ONLY
|
||||
// =======================
|
Loading…
Reference in New Issue
Block a user