-
Notifications
You must be signed in to change notification settings - Fork 66
feat(service-registry): added artifact owner-get owner-set commands #1745
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
Merged
rkpattnaik780
merged 3 commits into
redhat-developer:main
from
SafarMirek:sr-owner-commands
Sep 21, 2022
Merged
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,119 @@ | ||
| package owner | ||
|
|
||
| import ( | ||
| "github.com/AlecAivazis/survey/v2" | ||
| "github.com/redhat-developer/app-services-cli/pkg/cmd/registry/registrycmdutil" | ||
| "github.com/redhat-developer/app-services-cli/pkg/cmd/registry/rule/rulecmdutil" | ||
| "github.com/redhat-developer/app-services-cli/pkg/core/cmdutil/flagutil" | ||
| "github.com/redhat-developer/app-services-cli/pkg/core/ioutil/icon" | ||
| "github.com/redhat-developer/app-services-cli/pkg/shared/contextutil" | ||
| "github.com/redhat-developer/app-services-cli/pkg/shared/factory" | ||
| "github.com/spf13/cobra" | ||
| "k8s.io/utils/strings/slices" | ||
| ) | ||
|
|
||
| type OwnerGetOptions struct { | ||
| artifact string | ||
| group string | ||
|
|
||
| registryID string | ||
|
|
||
| f *factory.Factory | ||
| } | ||
|
|
||
| // NewGetCommand creates a new command to get a service registry setting | ||
| func NewGetCommand(f *factory.Factory) *cobra.Command { | ||
|
|
||
| opts := &OwnerGetOptions{ | ||
| f: f, | ||
| } | ||
|
|
||
| cmd := &cobra.Command{ | ||
| Use: "owner-get", | ||
| Short: f.Localizer.MustLocalize("artifact.cmd.owner.get.description.short"), | ||
| Long: f.Localizer.MustLocalize("artifact.cmd.owner.get.description.long"), | ||
| Example: f.Localizer.MustLocalize("artifact.cmd.owner.get.example"), | ||
| Args: cobra.NoArgs, | ||
| Hidden: true, | ||
| RunE: func(cmd *cobra.Command, _ []string) (err error) { | ||
|
|
||
| var missingFlags []string | ||
|
|
||
| if opts.artifact == "" { | ||
| missingFlags = append(missingFlags, "artifact-id") | ||
| } | ||
|
|
||
| if !opts.f.IOStreams.CanPrompt() && len(missingFlags) > 0 { | ||
| return flagutil.RequiredWhenNonInteractiveError(missingFlags...) | ||
| } | ||
|
|
||
| if len(missingFlags) > 0 { | ||
| err = runGetInteractivePrompt(opts, missingFlags) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| } | ||
|
|
||
| if opts.registryID != "" { | ||
| return runGet(opts) | ||
| } | ||
|
|
||
| registryInstance, err := contextutil.GetCurrentRegistryInstance(f) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| opts.registryID = registryInstance.GetId() | ||
|
|
||
| return runGet(opts) | ||
| }, | ||
| } | ||
|
|
||
| flags := rulecmdutil.NewFlagSet(cmd, f) | ||
|
|
||
| flags.AddRegistryInstance(&opts.registryID) | ||
| flags.AddGroup(&opts.group) | ||
| flags.AddArtifactID(&opts.artifact) | ||
|
|
||
| return cmd | ||
| } | ||
|
|
||
| func runGet(opts *OwnerGetOptions) error { | ||
| conn, err := opts.f.Connection() | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| api := conn.API() | ||
|
|
||
| a, _, err := api.ServiceRegistryInstance(opts.registryID) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| request := a.MetadataApi.GetArtifactOwner(opts.f.Context, opts.group, opts.artifact) | ||
|
|
||
| artifactOwner, _, err := request.Execute() | ||
| if err != nil { | ||
| return registrycmdutil.TransformInstanceError(err) | ||
| } | ||
|
|
||
| opts.f.Logger.Info(icon.SuccessPrefix(), *artifactOwner.Owner) | ||
SafarMirek marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return nil | ||
| } | ||
|
|
||
| func runGetInteractivePrompt(opts *OwnerGetOptions, missingFlags []string) (err error) { | ||
|
|
||
| if slices.Contains(missingFlags, "artifact-id") { | ||
| artifactIdPrompt := &survey.Input{ | ||
| Message: opts.f.Localizer.MustLocalize("artifact.cmd.owner.get.input.artifactId.message"), | ||
| } | ||
|
|
||
| err = survey.AskOne(artifactIdPrompt, &opts.artifact) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
This file contains hidden or 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,142 @@ | ||
| package owner | ||
|
|
||
| import ( | ||
| "github.com/AlecAivazis/survey/v2" | ||
| "github.com/redhat-developer/app-services-cli/pkg/cmd/registry/registrycmdutil" | ||
| "github.com/redhat-developer/app-services-cli/pkg/cmd/registry/rule/rulecmdutil" | ||
| "github.com/redhat-developer/app-services-cli/pkg/core/cmdutil/flagutil" | ||
| "github.com/redhat-developer/app-services-cli/pkg/core/ioutil/icon" | ||
| "github.com/redhat-developer/app-services-cli/pkg/core/localize" | ||
| "github.com/redhat-developer/app-services-cli/pkg/shared/contextutil" | ||
| "github.com/redhat-developer/app-services-cli/pkg/shared/factory" | ||
| registryinstanceclient "github.com/redhat-developer/app-services-sdk-go/registryinstance/apiv1internal/client" | ||
| "github.com/spf13/cobra" | ||
| "k8s.io/utils/strings/slices" | ||
| ) | ||
|
|
||
| type options struct { | ||
| artifact string | ||
| group string | ||
| owner string | ||
|
|
||
| registryID string | ||
|
|
||
| f *factory.Factory | ||
| } | ||
|
|
||
| // NewGetCommand creates a new command to get a service registry setting | ||
SafarMirek marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| func NewSetCommand(f *factory.Factory) *cobra.Command { | ||
|
|
||
| opts := &options{ | ||
| f: f, | ||
| } | ||
|
|
||
| cmd := &cobra.Command{ | ||
| Use: "owner-set", | ||
| Short: f.Localizer.MustLocalize("artifact.cmd.owner.set.description.short"), | ||
| Long: f.Localizer.MustLocalize("artifact.cmd.owner.set.description.long"), | ||
| Example: f.Localizer.MustLocalize("artifact.cmd.owner.set.example"), | ||
| Args: cobra.NoArgs, | ||
| Hidden: true, | ||
| RunE: func(cmd *cobra.Command, _ []string) (err error) { | ||
|
|
||
| var missingFlags []string | ||
|
|
||
| if opts.artifact == "" { | ||
| missingFlags = append(missingFlags, "artifact-id") | ||
| } | ||
|
|
||
| if opts.owner == "" { | ||
| missingFlags = append(missingFlags, "owner") | ||
| } | ||
|
|
||
| if !opts.f.IOStreams.CanPrompt() && len(missingFlags) > 0 { | ||
| return flagutil.RequiredWhenNonInteractiveError(missingFlags...) | ||
| } | ||
|
|
||
| if len(missingFlags) > 0 { | ||
| err = runSetInteractivePrompt(opts, missingFlags) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| } | ||
|
|
||
| if opts.registryID != "" { | ||
| return runSet(opts) | ||
| } | ||
|
|
||
| registryInstance, err := contextutil.GetCurrentRegistryInstance(f) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| opts.registryID = registryInstance.GetId() | ||
|
|
||
| return runSet(opts) | ||
| }, | ||
| } | ||
|
|
||
| flags := rulecmdutil.NewFlagSet(cmd, f) | ||
|
|
||
| flags.AddRegistryInstance(&opts.registryID) | ||
| flags.AddGroup(&opts.group) | ||
| flags.AddArtifactID(&opts.artifact) | ||
|
|
||
| flags.StringVar(&opts.owner, "owner", "", f.Localizer.MustLocalize("setting.set.cmd.flag.owner.description")) | ||
|
|
||
| return cmd | ||
| } | ||
|
|
||
| func runSet(opts *options) error { | ||
| conn, err := opts.f.Connection() | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| api := conn.API() | ||
|
|
||
| a, _, err := api.ServiceRegistryInstance(opts.registryID) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| request := a.MetadataApi.UpdateArtifactOwner(opts.f.Context, opts.group, opts.artifact) | ||
|
|
||
| request = request.ArtifactOwner(registryinstanceclient.ArtifactOwner{ | ||
| Owner: &opts.owner, | ||
| }) | ||
|
|
||
| _, err = request.Execute() | ||
| if err != nil { | ||
| return registrycmdutil.TransformInstanceError(err) | ||
| } | ||
|
|
||
| opts.f.Logger.Info(icon.SuccessPrefix(), opts.f.Localizer.MustLocalize("artifact.cmd.owner.set.success", localize.NewEntry("Name", opts.artifact))) | ||
| return nil | ||
| } | ||
|
|
||
| func runSetInteractivePrompt(opts *options, missingFlags []string) (err error) { | ||
|
|
||
| if slices.Contains(missingFlags, "artifact-id") { | ||
| artifactIdPrompt := &survey.Input{ | ||
| Message: opts.f.Localizer.MustLocalize("artifact.cmd.owner.set.input.artifactId.message"), | ||
| } | ||
|
|
||
| err = survey.AskOne(artifactIdPrompt, &opts.artifact) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| } | ||
| if slices.Contains(missingFlags, "owner") { | ||
| ownerIdPrompt := &survey.Input{ | ||
| Message: opts.f.Localizer.MustLocalize("artifact.cmd.owner.set.input.owner.message"), | ||
| } | ||
|
|
||
| err = survey.AskOne(ownerIdPrompt, &opts.owner) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.