-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #107 from kndpio/98-search-command-for-packages
implemented search cli command to return found packages from configur…
- Loading branch information
Showing
6 changed files
with
145 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package registry | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/charmbracelet/log" | ||
"github.com/kndpio/kndp/internal/search" | ||
"github.com/pterm/pterm" | ||
"k8s.io/client-go/kubernetes" | ||
"k8s.io/client-go/rest" | ||
) | ||
|
||
// SearchCmd is the struct representing the search command | ||
type SearchCmd struct { | ||
// Query is the search query | ||
Query string `arg:"" help:"search query"` | ||
Versions bool `optional:"" short:"v" help:"display all versions"` | ||
} | ||
|
||
func (c *SearchCmd) Run(ctx context.Context, client *kubernetes.Clientset, config *rest.Config, logger *log.Logger) error { | ||
tableRegs, err := search.SearchPackages(ctx, client, config, c.Query, c.Versions, logger) | ||
if err != nil { | ||
return err | ||
} | ||
if len(tableRegs) <= 1 { | ||
logger.Info("No packages found") | ||
} else { | ||
pterm.DefaultTable.WithHasHeader().WithData(tableRegs).Render() | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package github | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"strings" | ||
|
||
"github.com/charmbracelet/log" | ||
"github.com/kndpio/kndp/internal/registry" | ||
"github.com/pterm/pterm" | ||
|
||
"github.com/google/go-github/v61/github" | ||
) | ||
|
||
// GetPackages list packages and their versions from Container Registry | ||
func GetPackages(ctx context.Context, query string, version bool, r *registry.Registry, registryUrl string, org string, logger *log.Logger) (pterm.TableData, error) { | ||
auth := registry.RegistryConfig{} | ||
json.Unmarshal([]byte(r.Data[".dockerconfigjson"]), &auth) | ||
clientgh := github.NewClient(nil).WithAuthToken(auth.Auths[registryUrl].Password) | ||
tableRegs := pterm.TableData{ | ||
{"URL", "VERSION"}, | ||
} | ||
pkgType := "container" | ||
|
||
pkgs, _, err := clientgh.Organizations.ListPackages(ctx, org, &github.PackageListOptions{PackageType: &pkgType}) | ||
if err != nil { | ||
logger.Errorf("Cannot get packages from %s", registryUrl) | ||
return nil, err | ||
} | ||
|
||
pkgVersions := make(map[string][]*github.PackageVersion) | ||
for _, pkg := range pkgs { | ||
versions, _, err := clientgh.Organizations.PackageGetAllVersions(ctx, org, pkgType, pkg.GetName(), nil) | ||
if err != nil { | ||
logger.Errorf("Cannot get package versions for %s/%s", org, *pkg.Name) | ||
return nil, err | ||
} | ||
if !version { | ||
if len(versions) > 0 { | ||
pkgVersions[*pkg.Name] = []*github.PackageVersion{versions[0]} | ||
} | ||
} else { | ||
pkgVersions[*pkg.Name] = versions | ||
} | ||
} | ||
|
||
for _, pkg := range pkgs { | ||
if !strings.Contains(*pkg.Name, query) { | ||
continue | ||
} | ||
versions := pkgVersions[*pkg.Name] | ||
for _, v := range versions { | ||
tags := v.GetMetadata().Container.Tags | ||
if len(tags) > 0 { | ||
tableRegs = append(tableRegs, []string{ | ||
"ghcr.io/" + org + "/" + *pkg.Name, | ||
tags[0], | ||
}) | ||
} | ||
} | ||
} | ||
|
||
return tableRegs, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package search | ||
|
||
import ( | ||
"context" | ||
"net/url" | ||
"strings" | ||
|
||
"github.com/charmbracelet/log" | ||
"github.com/pterm/pterm" | ||
|
||
"github.com/kndpio/kndp/internal/github" | ||
"github.com/kndpio/kndp/internal/registry" | ||
"k8s.io/client-go/kubernetes" | ||
"k8s.io/client-go/rest" | ||
) | ||
|
||
func SearchPackages(ctx context.Context, client *kubernetes.Clientset, config *rest.Config, query string, versions bool, logger *log.Logger) (pterm.TableData, error) { | ||
registries, err := registry.Registries(ctx, client) | ||
if err != nil { | ||
logger.Error("Cannot get registries") | ||
return nil, err | ||
} | ||
|
||
for _, r := range registries { | ||
registryUrl := r.Annotations["kndp-registry-server-url"] | ||
u, _ := url.Parse(registryUrl) | ||
org := strings.TrimPrefix(u.Path, "/") | ||
// Switch statement to handle different registry types | ||
switch { | ||
case strings.Contains(registryUrl, "ghcr.io"): | ||
tableRegs, err := github.GetPackages(ctx, query, versions, r, registryUrl, org, logger) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return tableRegs, nil | ||
default: | ||
logger.Errorf("Registry %s is not supported", registryUrl) | ||
} | ||
|
||
} | ||
return nil, err | ||
} |