From 462ec32a6a91973f9231603b08e56977d54f68e9 Mon Sep 17 00:00:00 2001 From: Wojciech Trocki Date: Tue, 10 Aug 2021 16:08:27 +0100 Subject: [PATCH] chore: add support for metadata fetching --- pkg/cmd/registry/artifacts/artifacts.go | 2 + .../registry/artifacts/metadata/metadata.go | 136 ++++++++++++++++++ .../registry/artifacts/versions/versions.go | 2 +- 3 files changed, 139 insertions(+), 1 deletion(-) create mode 100644 pkg/cmd/registry/artifacts/metadata/metadata.go diff --git a/pkg/cmd/registry/artifacts/artifacts.go b/pkg/cmd/registry/artifacts/artifacts.go index 9953269ec..5d6e629f0 100644 --- a/pkg/cmd/registry/artifacts/artifacts.go +++ b/pkg/cmd/registry/artifacts/artifacts.go @@ -7,6 +7,7 @@ import ( "github.com/redhat-developer/app-services-cli/pkg/cmd/registry/artifacts/crud/get" "github.com/redhat-developer/app-services-cli/pkg/cmd/registry/artifacts/crud/list" "github.com/redhat-developer/app-services-cli/pkg/cmd/registry/artifacts/crud/update" + "github.com/redhat-developer/app-services-cli/pkg/cmd/registry/artifacts/metadata" "github.com/redhat-developer/app-services-cli/pkg/cmd/registry/artifacts/versions" "github.com/spf13/cobra" @@ -43,6 +44,7 @@ func NewArtifactsCommand(f *factory.Factory) *cobra.Command { update.NewUpdateCommand(f), // Misc + metadata.NewMetadataCommand(f), versions.NewVersionsCommand(f), ) diff --git a/pkg/cmd/registry/artifacts/metadata/metadata.go b/pkg/cmd/registry/artifacts/metadata/metadata.go new file mode 100644 index 000000000..e830b8479 --- /dev/null +++ b/pkg/cmd/registry/artifacts/metadata/metadata.go @@ -0,0 +1,136 @@ +package metadata + +import ( + "context" + "encoding/json" + "fmt" + + flagutil "github.com/redhat-developer/app-services-cli/pkg/cmdutil/flags" + "github.com/redhat-developer/app-services-cli/pkg/connection" + "github.com/redhat-developer/app-services-cli/pkg/dump" + "github.com/redhat-developer/app-services-cli/pkg/iostreams" + "github.com/redhat-developer/app-services-cli/pkg/localize" + "github.com/redhat-developer/app-services-cli/pkg/serviceregistry/registryinstanceerror" + "github.com/spf13/cobra" + "gopkg.in/yaml.v2" + + "github.com/redhat-developer/app-services-cli/internal/config" + "github.com/redhat-developer/app-services-cli/pkg/cmd/factory" + "github.com/redhat-developer/app-services-cli/pkg/cmd/registry/artifacts/util" + + "github.com/redhat-developer/app-services-cli/pkg/logging" +) + +type Options struct { + artifact string + group string + outputFormat string + + registryID string + + IO *iostreams.IOStreams + Config config.IConfig + Logger func() (logging.Logger, error) + Connection factory.ConnectionFunc + localizer localize.Localizer +} + +func NewMetadataCommand(f *factory.Factory) *cobra.Command { + opts := &Options{ + Config: f.Config, + Connection: f.Connection, + IO: f.IOStreams, + localizer: f.Localizer, + Logger: f.Logger, + } + + cmd := &cobra.Command{ + Use: "metadata", + Short: "Get artifact metadata", + Long: ` +Gets the metadata for an artifact in the service registry. +The returned metadata includes both generated (read-only) and editable metadata (such as name and description). +`, + Example: ` +## Get latest artifact metadata for default group +rhoas service-registry artifacts metadata my-artifact + +## Get latest artifact metadata for my-group group +rhoas service-registry artifacts metadata my-artifact --group mygroup + `, + Args: cobra.RangeArgs(0, 2), + RunE: func(cmd *cobra.Command, args []string) error { + if len(args) > 0 { + opts.artifact = args[0] + } + + if opts.registryID != "" { + return runGet(opts) + } + + cfg, err := opts.Config.Load() + if err != nil { + return err + } + + if !cfg.HasServiceRegistry() { + return fmt.Errorf("No service Registry selected. Use 'rhoas service-registry use' to select your registry") + } + + opts.registryID = fmt.Sprint(cfg.Services.ServiceRegistry.InstanceID) + return runGet(opts) + }, + } + + cmd.Flags().StringVarP(&opts.artifact, "artifact", "a", "", "Id of the artifact") + cmd.Flags().StringVarP(&opts.group, "group", "g", "", "Group of the artifact") + cmd.Flags().StringVarP(&opts.registryID, "registryId", "", "", "Id of the registry to be used. By default uses currently selected registry") + cmd.Flags().StringVarP(&opts.outputFormat, "output", "o", "", "Output format (json, yaml, yml)") + + flagutil.EnableOutputFlagCompletion(cmd) + + return cmd +} + +func runGet(opts *Options) error { + logger, err := opts.Logger() + if err != nil { + return err + } + + conn, err := opts.Connection(connection.DefaultConfigRequireMasAuth) + if err != nil { + return err + } + + dataAPI, _, err := conn.API().ServiceRegistryInstance(opts.registryID) + if err != nil { + return err + } + + if opts.group == "" { + logger.Info("Group was not specified. Using 'default' artifacts group.") + opts.group = util.DefaultArtifactGroup + } + + logger.Info("Fetching artifact metadata") + + ctx := context.Background() + request := dataAPI.MetadataApi.GetArtifactMetaData(ctx, opts.group, opts.artifact) + response, _, err := request.Execute() + if err != nil { + return registryinstanceerror.TransformError(err) + } + + logger.Info("Successfully fetched artifact metadata") + + switch opts.outputFormat { + case "yaml", "yml": + data, _ := yaml.Marshal(response) + _ = dump.YAML(opts.IO.Out, data) + default: + data, _ := json.Marshal(response) + _ = dump.JSON(opts.IO.Out, data) + } + return nil +} diff --git a/pkg/cmd/registry/artifacts/versions/versions.go b/pkg/cmd/registry/artifacts/versions/versions.go index 44ec177f3..4a805f00b 100644 --- a/pkg/cmd/registry/artifacts/versions/versions.go +++ b/pkg/cmd/registry/artifacts/versions/versions.go @@ -113,7 +113,7 @@ func runGet(opts *Options) error { logger.Info("Fetching artifact versions") ctx := context.Background() - request := dataAPI.MetadataApi.GetArtifactVersionMetaDataByContent(ctx, opts.group, opts.artifact) + request := dataAPI.VersionsApi.ListArtifactVersions(ctx, opts.group, opts.artifact) response, _, err := request.Execute() if err != nil { return registryinstanceerror.TransformError(err)