-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add support for metadata fetching
- Loading branch information
Showing
3 changed files
with
139 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters