Skip to content
This repository has been archived by the owner on Jan 24, 2025. It is now read-only.

Commit

Permalink
Merge pull request #19 from grafana/add-new-catalog-helper
Browse files Browse the repository at this point in the history
add new catalog helper
  • Loading branch information
pablochacin authored Sep 9, 2024
2 parents b3b9f65 + d9147c4 commit 339bfc4
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
11 changes: 11 additions & 0 deletions catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"net/http"
"os"
"sort"
"strings"

"github.com/Masterminds/semver/v3"
)
Expand Down Expand Up @@ -90,6 +91,16 @@ func NewCatalogFromJSON(stream io.Reader) (Catalog, error) {
}, nil
}

// NewCatalog returns a catalog loaded from a location.
// The location can be a local path or an URL
func NewCatalog(ctx context.Context, location string) (Catalog, error) {
if strings.HasPrefix(location, "http") {
return NewCatalogFromURL(ctx, location)
}

return NewCatalogFromFile(location)
}

// NewCatalogFromFile creates a Catalog from a json file
func NewCatalogFromFile(catalogFile string) (Catalog, error) {
json, err := os.ReadFile(catalogFile) //nolint:forbidigo,gosec
Expand Down
10 changes: 8 additions & 2 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
package cmd

import (
"context"
"errors"
"fmt"

Expand Down Expand Up @@ -39,7 +40,7 @@ func New() *cobra.Command {
return fmt.Errorf("path to registry must be specified")
}

catalog, err := k6catalog.NewCatalogFromFile(path)
catalog, err := k6catalog.NewCatalog(context.TODO(), path)
if err != nil {
return err
}
Expand All @@ -55,7 +56,12 @@ func New() *cobra.Command {
},
}

cmd.Flags().StringVarP(&path, "catalog-file", "f", "", "path to catalog file")
cmd.Flags().StringVar(
&path,
"catalog",
k6catalog.DefaultCatalogURL,
"path to catalog. Can be a path to a local file or a URL",
)
cmd.Flags().StringVarP(&dependency, "name", "d", "", "name of dependency")
cmd.Flags().StringVarP(&constrains, "constrains", "c", "*", "version constrains")

Expand Down

0 comments on commit 339bfc4

Please # to comment.