This repository has been archived on 2026-03-18. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
lotus-package-manager/Commands/SearchCommand.go
2021-11-20 15:57:22 +01:00

84 lines
No EOL
2.1 KiB
Go

package Commands
import (
"github.com/olekukonko/tablewriter"
"github.com/urfave/cli/v2"
"lpm-cli/Lotus"
"lpm-cli/ProtoHandler"
proto "lpm-cli/proto/lpm/go"
"os"
)
func NewSearchCommand() *cli.Command {
logger := Lotus.Logger("searchCommand")
return &cli.Command {
Name: "search",
Usage: "Seaches a packages",
Description: "Sends a search request to the default registry server, or to a user-defined registry server",
HelpName: "search",
Action: func(c *cli.Context) error {
var query string
if len(os.Args) > 3{
query = os.Args[2] + " " + os.Args[3]
} else {
query = os.Args[2]
}
packages := SearchPackages(Lotus.Platform(), Lotus.Architecture(), query)
//sample code
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Publisher", "Package", "Description", "Latest Version", "Registry"})
for _, pkg := range packages {
table.Append([]string{pkg.PackageMetadata.Publisher, pkg.PackageMetadata.Name, pkg.PackageMetadata.Description, pkg.PackageMetadata.Version, pkg.Register.Register})
}
table.Render()
//Sample code end
return nil
},
OnUsageError: func(c *cli.Context, err error, isSubcommand bool) error {
if err != nil{
logger.Fatal(err)
}
return err
},
}
}
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
}