diff --git a/catalog.go b/catalog.go index 405321d..a797a66 100644 --- a/catalog.go +++ b/catalog.go @@ -11,6 +11,7 @@ import ( "net/http" "os" "sort" + "strings" "github.com/Masterminds/semver/v3" ) @@ -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 diff --git a/cmd/cmd.go b/cmd/cmd.go index 190e290..a0259c9 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -2,6 +2,7 @@ package cmd import ( + "context" "errors" "fmt" @@ -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 } @@ -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")