lure/internal/repos/find.go

61 lines
1.3 KiB
Go
Raw Normal View History

2022-12-01 02:26:25 +00:00
package repos
import (
2022-12-24 20:56:02 +00:00
"github.com/jmoiron/sqlx"
2022-12-01 02:26:25 +00:00
"go.arsenm.dev/lure/internal/db"
)
2022-12-01 06:15:34 +00:00
// FindPkgs looks for packages matching the inputs inside the database.
// It returns a map that maps the package name input to the packages found for it.
// It also returns a slice that contains the names of all packages that were not found.
2022-12-24 20:56:02 +00:00
func FindPkgs(gdb *sqlx.DB, pkgs []string) (map[string][]db.Package, []string, error) {
2022-12-01 02:26:25 +00:00
found := map[string][]db.Package{}
notFound := []string(nil)
2022-12-01 02:26:25 +00:00
for _, pkgName := range pkgs {
result, err := db.GetPkgs(gdb, "name LIKE ?", pkgName)
if err != nil {
return nil, nil, err
2022-12-01 02:26:25 +00:00
}
added := 0
2022-12-24 20:56:02 +00:00
for result.Next() {
2022-12-01 02:26:25 +00:00
var pkg db.Package
2022-12-24 20:56:02 +00:00
err = result.StructScan(&pkg)
2022-12-01 02:26:25 +00:00
if err != nil {
2022-12-24 20:56:02 +00:00
return nil, nil, err
2022-12-01 02:26:25 +00:00
}
added++
found[pkgName] = append(found[pkgName], pkg)
}
2022-12-24 20:56:02 +00:00
result.Close()
2022-12-01 02:26:25 +00:00
if added == 0 {
result, err := db.GetPkgs(gdb, "json_array_contains(provides, ?)", pkgName)
2022-12-01 02:26:25 +00:00
if err != nil {
return nil, nil, err
2022-12-01 02:26:25 +00:00
}
2022-12-24 20:56:02 +00:00
for result.Next() {
2022-12-01 02:26:25 +00:00
var pkg db.Package
2022-12-24 20:56:02 +00:00
err = result.StructScan(&pkg)
2022-12-01 02:26:25 +00:00
if err != nil {
2022-12-24 20:56:02 +00:00
return nil, nil, err
2022-12-01 02:26:25 +00:00
}
added++
found[pkgName] = append(found[pkgName], pkg)
}
2022-12-24 20:56:02 +00:00
result.Close()
2022-12-01 02:26:25 +00:00
}
if added == 0 {
notFound = append(notFound, pkgName)
}
2022-12-01 02:26:25 +00:00
}
return found, notFound, nil
2022-12-01 02:26:25 +00:00
}