Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: i18n support for service registry art Artifact commands #1026

Merged
merged 1 commit into from
Sep 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 5 additions & 41 deletions pkg/cmd/registry/artifact/artifacts.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,47 +15,11 @@ import (

func NewArtifactsCommand(f *factory.Factory) *cobra.Command {
cmd := &cobra.Command{
Use: "artifact",
Short: "Manage Service Registry Artifacts",
Long: `
Manage Service Registry Artifacts using the currently selected Service Registry.

Commands are executed on the currently selected Service Registry instance that can be overridden by --instance-id flag.

Apicurio Registry Artifacts enables developers to manage and share the structure of their data.
For example, client applications can dynamically push or pull the latest updates to or from the registry without needing to redeploy.
Apicurio Registry also enables developers to create rules that govern how registry content can evolve over time.
For example, this includes rules for content validation and version compatibility.

Artifact commands enable client applications to manage the artifacts in the registry.
This set of commands provide create, read, update, and delete operations for schema and API artifacts, rules, versions, and metadata.
`,
Example: `
## Create artifact in my-group from schema.json file
rhoas service-registry artifact create --artifact-id=my-artifact --group=my-group artifact.json

## Get artifact content
rhoas service-registry artifact get --artifact-id=my-artifact --group=my-group --output-file=artifact.json

## Get artifact content by hash
rhoas service-registry artifact download --hash=cab4...al9 --output-file=artifact.json

## Delete artifact
rhoas service-registry artifact delete --artifact-id=my-artifact

## Get artifact metadata
rhoas service-registry artifact metadata --artifact-id=my-artifact --group=my-group

## Update artifact
rhoas service-registry artifact update --artifact-id=my-artifact artifact-new.json

## List Artifacts
rhoas service-registry artifact list --group=my-group --limit=10 page=1

## View artifact versions
rhoas service-registry artifact versions --artifact-id=my-artifact --group=my-group
`,
Args: cobra.MinimumNArgs(1),
Use: f.Localizer.MustLocalize("artifact.cmd.use"),
Short: f.Localizer.MustLocalize("artifact.cmd.description.short"),
Long: f.Localizer.MustLocalize("artifact.cmd.description.long"),
Example: f.Localizer.MustLocalize("artifact.cmd.example"),
Args: cobra.MinimumNArgs(1),
}

// add sub-commands
Expand Down
71 changes: 19 additions & 52 deletions pkg/cmd/registry/artifact/crud/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,33 +46,6 @@ type Options struct {
localizer localize.Localizer
}

var longDescription = `
Creates a new artifact by posting the artifact content to the registry.

Artifacts can be typically in JSON format for most of the supported types, but may be in another format for a few (for example, PROTOBUF).
The registry attempts to figure out what kind of artifact is being added from the following supported list:

- Avro (AVRO)
- Protobuf (PROTOBUF)
- JSON Schema (JSON)
- Kafka Connect (KCONNECT)
- OpenAPI (OPENAPI)
- AsyncAPI (ASYNCAPI)
- GraphQL (GRAPHQL)
- Web Services Description Language (WSDL)
- XML Schema (XSD)

An artifact is created using the content provided in the body of the request.
This content is created under a unique artifact ID that can be provided by user.
If not provided in the request, the server generates a unique ID for the artifact.
It is typically recommended that callers provide the ID, because this is a meaningful identifier, and for most use cases should be supplied by the caller.
If an artifact with the provided artifact ID already exists command will fail with error.


When --group parameter is missing the command will use "default" group.
when --instance-id is missing the command will create a new artifact for currently active service registry (visible in rhoas service-registry describe)
`

func NewCreateCommand(f *factory.Factory) *cobra.Command {
opts := &Options{
IO: f.IOStreams,
Expand All @@ -83,17 +56,11 @@ func NewCreateCommand(f *factory.Factory) *cobra.Command {
}

cmd := &cobra.Command{
Use: "create",
Short: "Creates new artifact from file or standard input",
Long: longDescription,
Example: `
# Create an artifact in default group
rhoas service-registry artifact create my-artifact.json

# Create an artifact with specified type
rhoas service-registry artifact create --type=JSON my-artifact.json
`,
Args: cobra.RangeArgs(0, 1),
Use: f.Localizer.MustLocalize("artifact.cmd.create.use"),
Copy link
Collaborator

@wtrocki wtrocki Sep 2, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did not internationalise use but I think we aren't consistent in this across cli.
Command names should not change

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me know if you want to revert strings for "use".

Copy link
Collaborator

@wtrocki wtrocki Sep 2, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need. All good. Changes like this should be cli wide.

Short: f.Localizer.MustLocalize("artifact.cmd.create.description.short"),
Long: f.Localizer.MustLocalize("artifact.cmd.create.description.long"),
Example: f.Localizer.MustLocalize("artifact.cmd.create.example"),
Args: cobra.RangeArgs(0, 1),
RunE: func(cmd *cobra.Command, args []string) error {
validOutputFormats := flagutil.ValidOutputFormats
if opts.outputFormat != "" && !flagutil.IsValidInput(opts.outputFormat, validOutputFormats...) {
Expand All @@ -115,12 +82,12 @@ rhoas service-registry artifact create --type=JSON my-artifact.json

if opts.artifactType != "" {
if _, err = registryinstanceclient.NewArtifactTypeFromValue(opts.artifactType); err != nil {
return errors.New("invalid artifact type. Please use one of following values: " + util.GetAllowedArtifactTypeEnumValuesAsString())
return errors.New(opts.localizer.MustLocalize("artifact.cmd.create.error.invalidArtifactType", localize.NewEntry("AllowedTypes", util.GetAllowedArtifactTypeEnumValuesAsString())))
}
}

if !cfg.HasServiceRegistry() {
return errors.New("no service Registry selected. Use 'rhoas service-registry use' use to select your registry")
return errors.New(opts.localizer.MustLocalize("artifact.cmd.common.error.noServiceRegistrySelected"))
}

opts.registryID = cfg.Services.ServiceRegistry.InstanceID
Expand All @@ -129,17 +96,17 @@ rhoas service-registry artifact create --type=JSON my-artifact.json
}

cmd.Flags().StringVarP(&opts.outputFormat, "output", "o", "json", opts.localizer.MustLocalize("registry.cmd.flag.output.description"))
cmd.Flags().StringVar(&opts.file, "file", "", "File location of the artifact")
cmd.Flags().StringVar(&opts.file, "file", "", opts.localizer.MustLocalize("artifact.common.file.location"))

cmd.Flags().StringVarP(&opts.artifact, "artifact-id", "a", "", "Id of the artifact")
cmd.Flags().StringVarP(&opts.group, "group", "g", util.DefaultArtifactGroup, "Artifact group")
cmd.Flags().StringVarP(&opts.artifact, "artifact-id", "a", "", opts.localizer.MustLocalize("artifact.common.id"))
cmd.Flags().StringVarP(&opts.group, "group", "g", util.DefaultArtifactGroup, opts.localizer.MustLocalize("artifact.common.group"))

cmd.Flags().StringVar(&opts.version, "version", "", "Custom version of the artifact (for example 1.0.0)")
cmd.Flags().StringVar(&opts.name, "name", "", "Custom name of the artifact")
cmd.Flags().StringVar(&opts.description, "description", "", "Custom description of the artifact")
cmd.Flags().StringVar(&opts.version, "version", "", opts.localizer.MustLocalize("artifact.common.custom.version"))
cmd.Flags().StringVar(&opts.name, "name", "", opts.localizer.MustLocalize("artifact.common.custom.name"))
cmd.Flags().StringVar(&opts.description, "description", "", opts.localizer.MustLocalize("artifact.common.custom.description"))

cmd.Flags().StringVarP(&opts.artifactType, "type", "t", "", "Type of artifact. Choose from: "+util.GetAllowedArtifactTypeEnumValuesAsString())
cmd.Flags().StringVar(&opts.registryID, "instance-id", "", "Id of the registry to be used. By default uses currently selected registry")
cmd.Flags().StringVarP(&opts.artifactType, "type", "t", "", opts.localizer.MustLocalize("artifact.common.type", localize.NewEntry("AllowedTypes", util.GetAllowedArtifactTypeEnumValuesAsString())))
cmd.Flags().StringVar(&opts.registryID, "instance-id", "", opts.localizer.MustLocalize("artifact.common.instance.id"))

flagutil.EnableOutputFlagCompletion(cmd)

Expand All @@ -162,19 +129,19 @@ func runCreate(opts *Options) error {
}

if opts.group == util.DefaultArtifactGroup {
opts.Logger.Info("Group was not specified. Using", util.DefaultArtifactGroup, "artifacts group.")
opts.Logger.Info(opts.localizer.MustLocalize("artifact.common.message.no.group", localize.NewEntry("DefaultArtifactGroup", util.DefaultArtifactGroup)))
opts.group = util.DefaultArtifactGroup
}

var specifiedFile *os.File
if opts.file != "" {
opts.Logger.Info("Opening file: " + opts.file)
opts.Logger.Info(opts.localizer.MustLocalize("artifact.common.message.opening.file", localize.NewEntry("FileName", opts.file)))
specifiedFile, err = os.Open(opts.file)
if err != nil {
return err
}
} else {
opts.Logger.Info("Reading file content from standard input")
opts.Logger.Info(opts.localizer.MustLocalize("artifact.common.message.reading.file"))
specifiedFile, err = util.CreateFileFromStdin()
if err != nil {
return err
Expand Down Expand Up @@ -206,7 +173,7 @@ func runCreate(opts *Options) error {
if err != nil {
return registryinstanceerror.TransformError(err)
}
opts.Logger.Info("Artifact created")
opts.Logger.Info(opts.localizer.MustLocalize("artifact.common.message.created"))

dump.PrintDataInFormat(opts.outputFormat, metadata, opts.IO.Out)

Expand Down
48 changes: 18 additions & 30 deletions pkg/cmd/registry/artifact/crud/delete/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,23 +45,11 @@ func NewDeleteCommand(f *factory.Factory) *cobra.Command {
}

cmd := &cobra.Command{
Use: "delete",
Short: "Deletes single or all artifacts in a given group",
Long: `
Deletes single or all artifacts in a given group.

When called without arguments delete will delete all artifacts in the group
When --artifact-id is specified delete deletes only single artifact and its version
When --group parameter is missing the command will use "default" group.
`,
Example: `
## Delete all artifacts in the group "default"
rhoas service-registry artifact delete

## Delete artifact in the group "default" with name "my-artifact"
rhoas service-registry artifact delete --artifact-id=my-artifact
`,
Args: cobra.NoArgs,
Use: f.Localizer.MustLocalize("artifact.cmd.delete.use"),
Short: f.Localizer.MustLocalize("artifact.cmd.delete.description.short"),
Long: f.Localizer.MustLocalize("artifact.cmd.delete.description.long"),
Example: f.Localizer.MustLocalize("artifact.cmd.delete.example"),
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
if !opts.IO.CanPrompt() && !opts.force {
return flag.RequiredWhenNonInteractiveError("yes")
Expand All @@ -77,18 +65,18 @@ rhoas service-registry artifact delete --artifact-id=my-artifact
}

if !cfg.HasServiceRegistry() {
return errors.New("no service Registry selected. Use 'rhoas service-registry use' use to select your registry")
return errors.New(opts.localizer.MustLocalize("artifact.cmd.common.error.noServiceRegistrySelected"))
}

opts.registryID = cfg.Services.ServiceRegistry.InstanceID
return runDelete(opts)
},
}

cmd.Flags().BoolVarP(&opts.force, "yes", "y", false, "Delete without prompt")
cmd.Flags().StringVarP(&opts.artifact, "artifact-id", "a", "", "Id of the artifact")
cmd.Flags().StringVarP(&opts.group, "group", "g", util.DefaultArtifactGroup, "Artifact group")
cmd.Flags().StringVar(&opts.registryID, "instance-id", "", "Id of the registry to be used. By default uses currently selected registry")
cmd.Flags().BoolVarP(&opts.force, "yes", "y", false, opts.localizer.MustLocalize("artifact.common.delete.without.prompt"))
cmd.Flags().StringVarP(&opts.artifact, "artifact-id", "a", "", opts.localizer.MustLocalize("artifact.common.id"))
cmd.Flags().StringVarP(&opts.group, "group", "g", util.DefaultArtifactGroup, opts.localizer.MustLocalize("artifact.common.group"))
cmd.Flags().StringVar(&opts.registryID, "instance-id", "", opts.localizer.MustLocalize("artifact.common.registryIdToUse"))
flagutil.EnableOutputFlagCompletion(cmd)

return cmd
Expand All @@ -106,14 +94,14 @@ func runDelete(opts *Options) error {
}

if opts.group == util.DefaultArtifactGroup {
opts.Logger.Info("Group was not specified. Using", util.DefaultArtifactGroup, "artifacts group.")
opts.Logger.Info(opts.localizer.MustLocalize("artifact.common.message.no.group", localize.NewEntry("DefaultArtifactGroup", util.DefaultArtifactGroup)))
opts.group = util.DefaultArtifactGroup
}

ctx := context.Background()
if opts.artifact == "" {
opts.Logger.Info("Artifact was not specified. Command will delete all artifacts in the group")
err = confirmDelete(opts, "Do you want to delete ALL ARTIFACTS from group "+opts.group)
opts.Logger.Info(opts.localizer.MustLocalize("artifact.common.message.deleteAllArtifactsInGroup"))
err = confirmDelete(opts, opts.localizer.MustLocalize("artifact.common.message.deleteAllArtifactsFromGroup", localize.NewEntry("GroupName", opts.group)))
if err != nil {
return err
}
Expand All @@ -122,14 +110,14 @@ func runDelete(opts *Options) error {
if err != nil {
return registryinstanceerror.TransformError(err)
}
opts.Logger.Info("Artifacts in group " + opts.group + " deleted")
opts.Logger.Info(opts.localizer.MustLocalize("artifact.common.message.AllArtifactsInGroupDeleted", localize.NewEntry("GroupName", opts.group)))
} else {
_, _, err := dataAPI.MetadataApi.GetArtifactMetaData(ctx, opts.group, opts.artifact).Execute()
if err != nil {
return errors.New("artifact " + opts.artifact + " not found")
return errors.New(opts.localizer.MustLocalize("artifact.common.error.artifact.notFound", localize.NewEntry("Name", opts.artifact)))
}
opts.Logger.Info("Deleting artifact " + opts.artifact)
err = confirmDelete(opts, "Do you want to delete artifact "+opts.artifact+" from group "+opts.group)
opts.Logger.Info(opts.localizer.MustLocalize("artifact.common.message.deleting.artifact", localize.NewEntry("Name", opts.artifact)))
err = confirmDelete(opts, opts.localizer.MustLocalize("artifact.common.message.deleting.artifactFromGroup", localize.NewEntry("Name", opts.artifact), localize.NewEntry("Group", opts.group)))
if err != nil {
return err
}
Expand All @@ -139,7 +127,7 @@ func runDelete(opts *Options) error {
if err != nil {
return registryinstanceerror.TransformError(err)
}
opts.Logger.Info("Artifact deleted: " + opts.artifact)
opts.Logger.Info(opts.localizer.MustLocalize("artifact.common.message.deleted", localize.NewEntry("Name", opts.artifact)))
}

return nil
Expand Down
50 changes: 16 additions & 34 deletions pkg/cmd/registry/artifact/crud/get/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,32 +46,14 @@ func NewGetCommand(f *factory.Factory) *cobra.Command {
}

cmd := &cobra.Command{
Use: "get",
Short: "Get artifact by id and group",
Long: `Get artifact by specifying id and group.
Command will fetch the latest artifact from the registry based on the artifact-id and group.

When --version is specified command will fetch the specific version of the artifact.
Get command will fetch artifacts based on --group and --artifact-id and --version.
For fetching artifacts using global identifiers please use "service-registry download" command
`,
Example: `
## Get latest artifact with name "my-artifact" and print it out to standard out
rhoas service-registry artifact get --artifact-id=my-artifact

## Get latest artifact with name "my-artifact" from group "my-group" and save it to artifact.json file
rhoas service-registry artifact get --artifact-id=my-artifact --group=my-group --output-file=artifact.json

## Get latest artifact and pipe it to other command
rhoas service-registry artifact get --artifact-id=my-artifact | grep -i 'user'

## Get artifact with custom version and print it out to standard out
rhoas service-registry artifact get --artifact-id=myartifact --version=4
`,
Args: cobra.NoArgs,
Use: f.Localizer.MustLocalize("artifact.cmd.get.use"),
Short: f.Localizer.MustLocalize("artifact.cmd.get.description.short"),
Long: f.Localizer.MustLocalize("artifact.cmd.get.description.long"),
Example: f.Localizer.MustLocalize("artifact.cmd.get.example"),
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
if opts.artifact == "" {
return errors.New("artifact id is required. Please specify artifact by using --artifact-id flag")
return errors.New(f.Localizer.MustLocalize("artifact.common.message.artifactIdRequired"))
}

if opts.registryID != "" {
Expand All @@ -84,19 +66,19 @@ rhoas service-registry artifact get --artifact-id=myartifact --version=4
}

if !cfg.HasServiceRegistry() {
return errors.New("no service registry selected. Please specify registry by using --instance-id flag")
return errors.New(opts.localizer.MustLocalize("registry.no.service.selected.use.instance.id.flag"))
}

opts.registryID = cfg.Services.ServiceRegistry.InstanceID
return runGet(opts)
},
}

cmd.Flags().StringVarP(&opts.artifact, "artifact-id", "a", "", "Id of the artifact")
cmd.Flags().StringVarP(&opts.group, "group", "g", util.DefaultArtifactGroup, "Artifact group")
cmd.Flags().StringVar(&opts.registryID, "instance-id", "", "Id of the registry to be used. By default uses currently selected registry")
cmd.Flags().StringVar(&opts.outputFile, "output-file", "", "Location of the output file")
cmd.Flags().StringVar(&opts.version, "version", "", "Version of the artifact")
cmd.Flags().StringVarP(&opts.artifact, "artifact-id", "a", "", opts.localizer.MustLocalize("artifact.common.id"))
cmd.Flags().StringVarP(&opts.group, "group", "g", util.DefaultArtifactGroup, opts.localizer.MustLocalize("artifact.common.group"))
cmd.Flags().StringVar(&opts.registryID, "instance-id", "", opts.localizer.MustLocalize("artifact.common.instance.id"))
cmd.Flags().StringVar(&opts.outputFile, "output-file", "", opts.localizer.MustLocalize("artifact.common.message.file.location"))
cmd.Flags().StringVar(&opts.version, "version", "", opts.localizer.MustLocalize("artifact.common.version"))

flagutil.EnableOutputFlagCompletion(cmd)

Expand All @@ -115,18 +97,18 @@ func runGet(opts *Options) error {
}

if opts.group == util.DefaultArtifactGroup {
opts.Logger.Info("Group was not specified. Using", util.DefaultArtifactGroup, "artifacts group.")
opts.Logger.Info(opts.localizer.MustLocalize("artifact.common.message.no.group", localize.NewEntry("DefaultArtifactGroup", util.DefaultArtifactGroup)))
opts.group = util.DefaultArtifactGroup
}

ctx := context.Background()
var dataFile *os.File
if opts.version != "" {
opts.Logger.Info("Fetching artifact with version: " + opts.version)
opts.Logger.Info(opts.localizer.MustLocalize("artifact.common.message.fetching.with.version", localize.NewEntry("Version", opts.version)))
request := dataAPI.VersionsApi.GetArtifactVersion(ctx, opts.group, opts.artifact, opts.version)
dataFile, _, err = request.Execute()
} else {
opts.Logger.Info("Fetching latest artifact")
opts.Logger.Info(opts.localizer.MustLocalize("artifact.common.message.fetching.latest"))
request := dataAPI.ArtifactsApi.GetLatestArtifact(ctx, opts.group, opts.artifact)
dataFile, _, err = request.Execute()
}
Expand All @@ -147,6 +129,6 @@ func runGet(opts *Options) error {
fmt.Fprintf(os.Stdout, "%v\n", string(fileContent))
}

opts.Logger.Info("Successfully fetched artifact")
opts.Logger.Info(opts.localizer.MustLocalize("artifact.common.message.fetched.successfully"))
return nil
}
Loading