Moved connection handler to Commands
This commit is contained in:
parent
150037aa57
commit
da2a4cdfd8
2 changed files with 39 additions and 82 deletions
39
Commands/Handler.go
Normal file
39
Commands/Handler.go
Normal file
|
|
@ -0,0 +1,39 @@
|
||||||
|
package Commands
|
||||||
|
|
||||||
|
import (
|
||||||
|
"lpm-cli/Lotus"
|
||||||
|
"lpm-cli/ProtoHandler"
|
||||||
|
proto "lpm-cli/Proto/lpm/go"
|
||||||
|
)
|
||||||
|
|
||||||
|
func fetchPackages(platform string, architecture []string, query string, registry Lotus.Registry) *proto.SearchResponse {
|
||||||
|
var protoWrap ProtoHandler.ProtoWrapper
|
||||||
|
|
||||||
|
conn := protoWrap.CreateConnection(registry.Address)
|
||||||
|
defer conn.Close()
|
||||||
|
|
||||||
|
client, ctx, cancel := protoWrap.CreateRegistryClient(conn)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
res, _ := client.Search(ctx, &proto.SearchRequest{Platform: platform, Architecture: architecture, Query: query})
|
||||||
|
|
||||||
|
return res
|
||||||
|
}
|
||||||
|
|
||||||
|
func SearchPackages(platform string, architecture []string, query string) []ProtoHandler.Package {
|
||||||
|
var packages []ProtoHandler.Package
|
||||||
|
var protoWrap ProtoHandler.ProtoWrapper
|
||||||
|
|
||||||
|
registries := Lotus.GetRegistries()
|
||||||
|
|
||||||
|
for _, reg := range registries {
|
||||||
|
res := fetchPackages(platform, architecture, query, reg)
|
||||||
|
if res != nil{
|
||||||
|
for _, pkg := range protoWrap.ParsePackages(res, reg){
|
||||||
|
packages = append(packages, pkg)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return packages
|
||||||
|
}
|
||||||
|
|
@ -1,82 +0,0 @@
|
||||||
package ProtoHandler
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"google.golang.org/grpc"
|
|
||||||
"lpm-cli/Lotus"
|
|
||||||
proto "lpm-cli/Proto/lpm/go"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
func createConnection(address string) *grpc.ClientConn{
|
|
||||||
|
|
||||||
logger := Lotus.DetailedLogger("ProtoHandler", "createConnection")
|
|
||||||
|
|
||||||
//TODO Remove Hardcoded addr
|
|
||||||
conn, err := grpc.Dial(address, grpc.WithInsecure())
|
|
||||||
if err != nil {
|
|
||||||
logger.Fatal(err)
|
|
||||||
}
|
|
||||||
return conn
|
|
||||||
}
|
|
||||||
|
|
||||||
func createRegistryClient(conn *grpc.ClientConn) (proto.LpmRegistryClient, context.Context, context.CancelFunc) {
|
|
||||||
|
|
||||||
lrc := proto.NewLpmRegistryClient(conn)
|
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second * 10)
|
|
||||||
|
|
||||||
return lrc, ctx, cancel
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
func parsePackages(res *proto.SearchResponse, reg Lotus.Registry) []Package{
|
|
||||||
var packages []Package
|
|
||||||
for _, pkg := range res.GetPackages(){
|
|
||||||
var tmpMeta PackageMetadata
|
|
||||||
tmpMeta.FillMetadata(pkg)
|
|
||||||
packages = append(packages, Package{
|
|
||||||
PackageMetadata: tmpMeta,
|
|
||||||
Register: Lotus.Registry{
|
|
||||||
Register: reg.Register,
|
|
||||||
Address: reg.Address,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
return packages
|
|
||||||
}
|
|
||||||
|
|
||||||
func fetchPackages(platform string, architecture []string, query string, registry Lotus.Registry) *proto.SearchResponse {
|
|
||||||
logger := Lotus.DetailedLogger("ProtoHandler", "fetchPackages")
|
|
||||||
|
|
||||||
conn := createConnection(registry.Address)
|
|
||||||
defer conn.Close()
|
|
||||||
|
|
||||||
client, ctx, cancel := createRegistryClient(conn)
|
|
||||||
defer cancel()
|
|
||||||
|
|
||||||
res, connErr := client.Search(ctx, &proto.SearchRequest{Platform: platform, Architecture: architecture, Query: query})
|
|
||||||
if connErr != nil{
|
|
||||||
logger.Error(connErr)
|
|
||||||
}
|
|
||||||
|
|
||||||
return res
|
|
||||||
}
|
|
||||||
|
|
||||||
func SearchPackages(platform string, architecture []string, query string) []Package {
|
|
||||||
|
|
||||||
registries := Lotus.GetRegistries()
|
|
||||||
var packages []Package
|
|
||||||
|
|
||||||
for _, reg := range registries{
|
|
||||||
|
|
||||||
res := fetchPackages(platform, architecture, query, reg)
|
|
||||||
if res != nil{
|
|
||||||
for _, pkg := range parsePackages(res, reg){
|
|
||||||
packages = append(packages, pkg)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return packages
|
|
||||||
}
|
|
||||||
Reference in a new issue