Skip to content

Commit

Permalink
generate-from: make sure we take the type into account
Browse files Browse the repository at this point in the history
Filter things we pull by the type. Prior to this, the type specified
in the command-line would be ignore.

Signed-off-by: Vincent Demeester <[email protected]>
  • Loading branch information
vdemeester committed Dec 19, 2023
1 parent f5d6ff8 commit 90f10ea
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 8 deletions.
18 changes: 13 additions & 5 deletions internal/catalog/catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"net/http"
"os"
"path/filepath"
"strings"

"github.com/cli/go-gh/v2/pkg/api"
"github.com/openshift-pipelines/tektoncd-catalog/internal/fetcher"
Expand Down Expand Up @@ -44,12 +45,12 @@ func FetchFromExternals(e config.External, client *api.RESTClient) (Catalog, err
return c, nil
}

func GenerateFilesystem(path string, c Catalog) error {
func GenerateFilesystem(path string, c Catalog, resourceType string) error {
for name, resource := range c.Resources {
fmt.Fprintf(os.Stderr, "# Fetching resources from %s\n", name)
for version, uri := range resource {
fmt.Fprintf(os.Stderr, "## Fetching version %s\n", version)
if err := fetchAndExtract(path, uri, version); err != nil {
if err := fetchAndExtract(path, uri, version, resourceType); err != nil {
fmt.Fprintf(os.Stderr, "Failed to fetch resource %s: %v, skipping\n", uri, err)
continue
}
Expand All @@ -58,18 +59,18 @@ func GenerateFilesystem(path string, c Catalog) error {
return nil
}

func fetchAndExtract(path, url, version string) error {
func fetchAndExtract(path, url, version, resourceType string) error {
resp, err := http.Get(url)
if err != nil {
return err
}
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("Status error: %v", resp.StatusCode)
}
return untar(path, version, resp.Body)
return untar(path, version, resourceType, resp.Body)
}

func untar(dst, version string, r io.Reader) error {
func untar(dst, version, resourceType string, r io.Reader) error {
gzr, err := gzip.NewReader(r)
if err != nil {
return err
Expand Down Expand Up @@ -97,6 +98,13 @@ func untar(dst, version string, r io.Reader) error {
targetFolder := filepath.Join(dst, filepath.Dir(header.Name), version)
target := filepath.Join(targetFolder, filename)

if resourceType != "" {
if !strings.HasPrefix(header.Name, resourceType) {
fmt.Fprintf(os.Stderr, "### Ignoring %s (type not %s)\n", header.Name, resourceType)
continue
}
}

if err := os.MkdirAll(targetFolder, os.ModePerm); err != nil {
return err
}
Expand Down
3 changes: 1 addition & 2 deletions internal/cmd/generate-from.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ func (v *GenerateFromExternalCmd) Run(cfg *config.Config) error {
Repositories: []fc.Repository{{
Name: name,
URL: v.url,
Types: []string{v.resourceType},
IgnoreVersions: ignoreVersions,
}},
}
Expand All @@ -91,7 +90,7 @@ func (v *GenerateFromExternalCmd) Run(cfg *config.Config) error {
return err
}

return catalog.GenerateFilesystem(v.target, c)
return catalog.GenerateFilesystem(v.target, c, v.resourceType)
}

// NewCatalogGenerateFromExternalCmd instantiates the "generate" subcommand.
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func (v *GenerateCmd) Run(cfg *config.Config) error {
return err
}

return catalog.GenerateFilesystem(v.target, c)
return catalog.GenerateFilesystem(v.target, c, "")
}

// NewCatalogGenerateCmd instantiates the "generate" subcommand.
Expand Down

0 comments on commit 90f10ea

Please sign in to comment.