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
|
* You should have received a copy of the GNU General Public License
|
||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package repos
|
package repos
|
||||||
|
|
||||||
import (
|
import (
|
||||||
@ -24,10 +23,7 @@ import (
|
|||||||
"plemya-x.ru/alr/internal/db"
|
"plemya-x.ru/alr/internal/db"
|
||||||
)
|
)
|
||||||
|
|
||||||
// FindPkgs looks for packages matching the inputs inside the database.
|
func (rs *Repos) FindPkgs(ctx context.Context, pkgs []string) (map[string][]db.Package, []string, error) {
|
||||||
// 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) {
|
|
||||||
found := map[string][]db.Package{}
|
found := map[string][]db.Package{}
|
||||||
notFound := []string(nil)
|
notFound := []string(nil)
|
||||||
|
|
||||||
@ -36,7 +32,7 @@ func FindPkgs(ctx context.Context, pkgs []string) (map[string][]db.Package, []st
|
|||||||
continue
|
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 {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
@ -55,7 +51,7 @@ func FindPkgs(ctx context.Context, pkgs []string) (map[string][]db.Package, []st
|
|||||||
result.Close()
|
result.Close()
|
||||||
|
|
||||||
if added == 0 {
|
if added == 0 {
|
||||||
result, err := db.GetPkgs(ctx, "name LIKE ?", pkgName)
|
result, err := rs.db.GetPkgs(ctx, "name LIKE ?", pkgName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
|
@ -18,9 +18,7 @@
|
|||||||
|
|
||||||
package repos_test
|
package repos_test
|
||||||
|
|
||||||
/*
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
"reflect"
|
"reflect"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
@ -31,18 +29,15 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func TestFindPkgs(t *testing.T) {
|
func TestFindPkgs(t *testing.T) {
|
||||||
_, err := db.Open(":memory:")
|
e := prepare(t)
|
||||||
if err != nil {
|
defer cleanup(t, e)
|
||||||
t.Fatalf("Expected no error, got %s", err)
|
|
||||||
}
|
|
||||||
defer db.Close()
|
|
||||||
|
|
||||||
setCfgDirs(t)
|
rs := repos.New(
|
||||||
defer removeCacheDir(t)
|
e.Cfg,
|
||||||
|
e.Db,
|
||||||
|
)
|
||||||
|
|
||||||
ctx := context.Background()
|
err := rs.Pull(e.Ctx, []types.Repo{
|
||||||
|
|
||||||
err = repos.Pull(ctx, []types.Repo{
|
|
||||||
{
|
{
|
||||||
Name: "default",
|
Name: "default",
|
||||||
URL: "https://gitea.plemya-x.ru/xpamych/xpamych-alr-repo.git",
|
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)
|
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 {
|
if err != nil {
|
||||||
t.Fatalf("Expected no error, got %s", err)
|
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))
|
t.Errorf("Expected 1 package found, got %d", len(found))
|
||||||
}
|
}
|
||||||
|
|
||||||
itdPkgs, ok := found["itd"]
|
alrPkgs, ok := found["alr"]
|
||||||
if !ok {
|
if !ok {
|
||||||
t.Fatalf("Expected 'itd' packages to be found")
|
t.Fatalf("Expected 'alr' packages to be found")
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(itdPkgs) < 2 {
|
if len(alrPkgs) < 2 {
|
||||||
t.Errorf("Expected two 'itd' packages to be found")
|
t.Errorf("Expected two 'alr' packages to be found")
|
||||||
}
|
}
|
||||||
|
|
||||||
for i, pkg := range itdPkgs {
|
for i, pkg := range alrPkgs {
|
||||||
if !strings.HasPrefix(pkg.Name, "itd") {
|
if !strings.HasPrefix(pkg.Name, "alr") {
|
||||||
t.Errorf("Expected package name of all found packages to start with 'itd', got %s on element %d", pkg.Name, i)
|
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) {
|
func TestFindPkgsEmpty(t *testing.T) {
|
||||||
_, err := db.Open(":memory:")
|
e := prepare(t)
|
||||||
if err != nil {
|
defer cleanup(t, e)
|
||||||
t.Fatalf("Expected no error, got %s", err)
|
|
||||||
}
|
|
||||||
defer db.Close()
|
|
||||||
|
|
||||||
setCfgDirs(t)
|
rs := repos.New(
|
||||||
defer removeCacheDir(t)
|
e.Cfg,
|
||||||
|
e.Db,
|
||||||
|
)
|
||||||
|
|
||||||
err = db.InsertPackage(db.Package{
|
err := e.Db.InsertPackage(e.Ctx, db.Package{
|
||||||
Name: "test1",
|
Name: "test1",
|
||||||
Repository: "default",
|
Repository: "default",
|
||||||
Version: "0.0.1",
|
Version: "0.0.1",
|
||||||
@ -106,7 +103,7 @@ func TestFindPkgsEmpty(t *testing.T) {
|
|||||||
t.Fatalf("Expected no error, got %s", err)
|
t.Fatalf("Expected no error, got %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
err = db.InsertPackage(db.Package{
|
err = e.Db.InsertPackage(e.Ctx, db.Package{
|
||||||
Name: "test2",
|
Name: "test2",
|
||||||
Repository: "default",
|
Repository: "default",
|
||||||
Version: "0.0.1",
|
Version: "0.0.1",
|
||||||
@ -121,7 +118,7 @@ func TestFindPkgsEmpty(t *testing.T) {
|
|||||||
t.Fatalf("Expected no error, got %s", err)
|
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 {
|
if err != nil {
|
||||||
t.Fatalf("Expected no error, got %s", err)
|
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)
|
t.Errorf("Expected 'test2' package, got '%s'", testPkgs[0].Name)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
|
@ -5,6 +5,7 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"plemya-x.ru/alr/internal/config"
|
"plemya-x.ru/alr/internal/config"
|
||||||
|
"plemya-x.ru/alr/internal/db"
|
||||||
database "plemya-x.ru/alr/internal/db"
|
database "plemya-x.ru/alr/internal/db"
|
||||||
"plemya-x.ru/alr/internal/types"
|
"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)
|
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
|
// FOR LEGACY ONLY
|
||||||
// =======================
|
// =======================
|
Loading…
Reference in New Issue
Block a user