|
| 1 | +package metadata |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + |
| 8 | + flagutil "github.com/redhat-developer/app-services-cli/pkg/cmdutil/flags" |
| 9 | + "github.com/redhat-developer/app-services-cli/pkg/connection" |
| 10 | + "github.com/redhat-developer/app-services-cli/pkg/dump" |
| 11 | + "github.com/redhat-developer/app-services-cli/pkg/iostreams" |
| 12 | + "github.com/redhat-developer/app-services-cli/pkg/localize" |
| 13 | + "github.com/redhat-developer/app-services-cli/pkg/serviceregistry/registryinstanceerror" |
| 14 | + "github.com/spf13/cobra" |
| 15 | + "gopkg.in/yaml.v2" |
| 16 | + |
| 17 | + "github.com/redhat-developer/app-services-cli/internal/config" |
| 18 | + "github.com/redhat-developer/app-services-cli/pkg/cmd/factory" |
| 19 | + "github.com/redhat-developer/app-services-cli/pkg/cmd/registry/artifacts/util" |
| 20 | + |
| 21 | + "github.com/redhat-developer/app-services-cli/pkg/logging" |
| 22 | +) |
| 23 | + |
| 24 | +type Options struct { |
| 25 | + artifact string |
| 26 | + group string |
| 27 | + outputFormat string |
| 28 | + |
| 29 | + registryID string |
| 30 | + |
| 31 | + IO *iostreams.IOStreams |
| 32 | + Config config.IConfig |
| 33 | + Logger func() (logging.Logger, error) |
| 34 | + Connection factory.ConnectionFunc |
| 35 | + localizer localize.Localizer |
| 36 | +} |
| 37 | + |
| 38 | +func NewMetadataCommand(f *factory.Factory) *cobra.Command { |
| 39 | + opts := &Options{ |
| 40 | + Config: f.Config, |
| 41 | + Connection: f.Connection, |
| 42 | + IO: f.IOStreams, |
| 43 | + localizer: f.Localizer, |
| 44 | + Logger: f.Logger, |
| 45 | + } |
| 46 | + |
| 47 | + cmd := &cobra.Command{ |
| 48 | + Use: "metadata", |
| 49 | + Short: "Get artifact metadata", |
| 50 | + Long: ` |
| 51 | +Gets the metadata for an artifact in the service registry. |
| 52 | +The returned metadata includes both generated (read-only) and editable metadata (such as name and description). |
| 53 | +`, |
| 54 | + Example: ` |
| 55 | +## Get latest artifact metadata for default group |
| 56 | +rhoas service-registry artifacts metadata my-artifact |
| 57 | +
|
| 58 | +## Get latest artifact metadata for my-group group |
| 59 | +rhoas service-registry artifacts metadata my-artifact --group mygroup |
| 60 | + `, |
| 61 | + Args: cobra.RangeArgs(0, 2), |
| 62 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 63 | + if len(args) > 0 { |
| 64 | + opts.artifact = args[0] |
| 65 | + } |
| 66 | + |
| 67 | + if opts.registryID != "" { |
| 68 | + return runGet(opts) |
| 69 | + } |
| 70 | + |
| 71 | + cfg, err := opts.Config.Load() |
| 72 | + if err != nil { |
| 73 | + return err |
| 74 | + } |
| 75 | + |
| 76 | + if !cfg.HasServiceRegistry() { |
| 77 | + return fmt.Errorf("No service Registry selected. Use 'rhoas service-registry use' to select your registry") |
| 78 | + } |
| 79 | + |
| 80 | + opts.registryID = fmt.Sprint(cfg.Services.ServiceRegistry.InstanceID) |
| 81 | + return runGet(opts) |
| 82 | + }, |
| 83 | + } |
| 84 | + |
| 85 | + cmd.Flags().StringVarP(&opts.artifact, "artifact", "a", "", "Id of the artifact") |
| 86 | + cmd.Flags().StringVarP(&opts.group, "group", "g", "", "Group of the artifact") |
| 87 | + cmd.Flags().StringVarP(&opts.registryID, "registryId", "", "", "Id of the registry to be used. By default uses currently selected registry") |
| 88 | + cmd.Flags().StringVarP(&opts.outputFormat, "output", "o", "", "Output format (json, yaml, yml)") |
| 89 | + |
| 90 | + flagutil.EnableOutputFlagCompletion(cmd) |
| 91 | + |
| 92 | + return cmd |
| 93 | +} |
| 94 | + |
| 95 | +func runGet(opts *Options) error { |
| 96 | + logger, err := opts.Logger() |
| 97 | + if err != nil { |
| 98 | + return err |
| 99 | + } |
| 100 | + |
| 101 | + conn, err := opts.Connection(connection.DefaultConfigRequireMasAuth) |
| 102 | + if err != nil { |
| 103 | + return err |
| 104 | + } |
| 105 | + |
| 106 | + dataAPI, _, err := conn.API().ServiceRegistryInstance(opts.registryID) |
| 107 | + if err != nil { |
| 108 | + return err |
| 109 | + } |
| 110 | + |
| 111 | + if opts.group == "" { |
| 112 | + logger.Info("Group was not specified. Using 'default' artifacts group.") |
| 113 | + opts.group = util.DefaultArtifactGroup |
| 114 | + } |
| 115 | + |
| 116 | + logger.Info("Fetching artifact metadata") |
| 117 | + |
| 118 | + ctx := context.Background() |
| 119 | + request := dataAPI.MetadataApi.GetArtifactMetaData(ctx, opts.group, opts.artifact) |
| 120 | + response, _, err := request.Execute() |
| 121 | + if err != nil { |
| 122 | + return registryinstanceerror.TransformError(err) |
| 123 | + } |
| 124 | + |
| 125 | + logger.Info("Successfully fetched artifact metadata") |
| 126 | + |
| 127 | + switch opts.outputFormat { |
| 128 | + case "yaml", "yml": |
| 129 | + data, _ := yaml.Marshal(response) |
| 130 | + _ = dump.YAML(opts.IO.Out, data) |
| 131 | + default: |
| 132 | + data, _ := json.Marshal(response) |
| 133 | + _ = dump.JSON(opts.IO.Out, data) |
| 134 | + } |
| 135 | + return nil |
| 136 | +} |
0 commit comments