Skip to content

Commit

Permalink
chore: add support for metadata fetching
Browse files Browse the repository at this point in the history
  • Loading branch information
wtrocki committed Aug 10, 2021
1 parent 53eecdd commit 462ec32
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 1 deletion.
2 changes: 2 additions & 0 deletions pkg/cmd/registry/artifacts/artifacts.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -43,6 +44,7 @@ func NewArtifactsCommand(f *factory.Factory) *cobra.Command {
update.NewUpdateCommand(f),

// Misc
metadata.NewMetadataCommand(f),
versions.NewVersionsCommand(f),
)

Expand Down
136 changes: 136 additions & 0 deletions pkg/cmd/registry/artifacts/metadata/metadata.go
Original file line number Diff line number Diff line change
@@ -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
}
2 changes: 1 addition & 1 deletion pkg/cmd/registry/artifacts/versions/versions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 462ec32

Please sign in to comment.