-
Notifications
You must be signed in to change notification settings - Fork 72
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
feat: group create, get, list command #1849
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
package create | ||
|
||
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/factory" | ||
registryinstanceclient "github.com/redhat-developer/app-services-sdk-core/app-services-sdk-go/registryinstance/apiv1internal/client" | ||
"github.com/spf13/cobra" | ||
"k8s.io/utils/strings/slices" | ||
|
||
"github.com/redhat-developer/app-services-cli/pkg/shared/contextutil" | ||
) | ||
|
||
type options struct { | ||
registryID string | ||
|
||
groupId string | ||
description string | ||
properties map[string]string | ||
|
||
f *factory.Factory | ||
} | ||
|
||
// NewCreateCommand creates a new command to create a new artifact group | ||
func NewCreateCommand(f *factory.Factory) *cobra.Command { | ||
|
||
opts := &options{ | ||
f: f, | ||
} | ||
|
||
cmd := &cobra.Command{ | ||
Use: "create", | ||
Short: f.Localizer.MustLocalize("group.create.cmd.description.short"), | ||
Long: f.Localizer.MustLocalize("group.create.cmd.description.long"), | ||
Example: f.Localizer.MustLocalize("group.create.cmd.example"), | ||
Args: cobra.NoArgs, | ||
RunE: func(cmd *cobra.Command, _ []string) (err error) { | ||
var missingFlags []string | ||
|
||
if opts.groupId == "" { | ||
missingFlags = append(missingFlags, "group-id") | ||
} | ||
|
||
if !opts.f.IOStreams.CanPrompt() && len(missingFlags) > 0 { | ||
return flagutil.RequiredWhenNonInteractiveError(missingFlags...) | ||
} | ||
|
||
if len(missingFlags) > 0 { | ||
err = runInteractivePrompt(opts, missingFlags) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
if opts.registryID != "" { | ||
return runCreate(opts) | ||
} | ||
|
||
registryInstance, err := contextutil.GetCurrentRegistryInstance(f) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
opts.registryID = registryInstance.GetId() | ||
|
||
return runCreate(opts) | ||
}, | ||
} | ||
|
||
flags := rulecmdutil.NewFlagSet(cmd, f) | ||
|
||
flags.StringVarP(&opts.groupId, "group-id", "g", "", opts.f.Localizer.MustLocalize("group.cmd.create.flag.group-id")) | ||
flags.StringVarP(&opts.description, "description", "d", "", opts.f.Localizer.MustLocalize("group.cmd.create.flag.description")) | ||
flags.StringToStringVarP(&opts.properties, "properties", "p", map[string]string{}, opts.f.Localizer.MustLocalize("group.cmd.create.flag.properties")) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. WIll suggest to check the implementation in |
||
|
||
flags.AddRegistryInstance(&opts.registryID) | ||
|
||
return cmd | ||
|
||
} | ||
|
||
func runCreate(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.GroupsApi.CreateGroup(opts.f.Context) | ||
|
||
createGroupMetaData := registryinstanceclient.CreateGroupMetaData{ | ||
Id: opts.groupId, | ||
Description: &opts.description, | ||
Properties: &opts.properties, | ||
} | ||
|
||
request = request.CreateGroupMetaData(createGroupMetaData) | ||
|
||
_, _, err = request.Execute() | ||
if err != nil { | ||
return registrycmdutil.TransformInstanceError(err) | ||
} | ||
|
||
opts.f.Logger.Info(icon.SuccessPrefix(), opts.f.Localizer.MustLocalize("group.cmd.create.log.info.created", localize.NewEntry("GroupId", opts.groupId))) | ||
|
||
return nil | ||
} | ||
|
||
func runInteractivePrompt(opts *options, missingFlags []string) (err error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The interactive mode allows |
||
|
||
if slices.Contains(missingFlags, "group-id") { | ||
settingNamePrompt := &survey.Input{ | ||
Message: opts.f.Localizer.MustLocalize("group.cmd.create.input.group-id.message"), | ||
} | ||
|
||
err = survey.AskOne(settingNamePrompt, &opts.groupId) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
package get | ||
|
||
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/dump" | ||
"github.com/redhat-developer/app-services-cli/pkg/shared/factory" | ||
"github.com/spf13/cobra" | ||
"k8s.io/utils/strings/slices" | ||
|
||
"github.com/redhat-developer/app-services-cli/pkg/shared/contextutil" | ||
) | ||
|
||
type options struct { | ||
registryID string | ||
|
||
groupId string | ||
outputFormat string | ||
|
||
f *factory.Factory | ||
} | ||
|
||
// NewGetCommand creates a new command to get an artifacts group metadata | ||
func NewGetCommand(f *factory.Factory) *cobra.Command { | ||
|
||
opts := &options{ | ||
f: f, | ||
} | ||
|
||
cmd := &cobra.Command{ | ||
Use: "get", | ||
Short: f.Localizer.MustLocalize("group.get.cmd.description.short"), | ||
Long: f.Localizer.MustLocalize("group.get.cmd.description.long"), | ||
Example: f.Localizer.MustLocalize("group.get.cmd.example"), | ||
Args: cobra.NoArgs, | ||
RunE: func(cmd *cobra.Command, _ []string) (err error) { | ||
var missingFlags []string | ||
|
||
if opts.outputFormat != "" && !flagutil.IsValidInput(opts.outputFormat, flagutil.ValidOutputFormats...) { | ||
return flagutil.InvalidValueError("output", opts.outputFormat, flagutil.ValidOutputFormats...) | ||
} | ||
|
||
if opts.groupId == "" { | ||
missingFlags = append(missingFlags, "group-id") | ||
} | ||
|
||
if !opts.f.IOStreams.CanPrompt() && len(missingFlags) > 0 { | ||
return flagutil.RequiredWhenNonInteractiveError(missingFlags...) | ||
} | ||
|
||
if len(missingFlags) > 0 { | ||
err = runInteractivePrompt(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.StringVarP(&opts.groupId, "group-id", "g", "", opts.f.Localizer.MustLocalize("group.cmd.get.flag.group-id")) | ||
flags.AddOutput(&opts.outputFormat) | ||
|
||
flags.AddRegistryInstance(&opts.registryID) | ||
|
||
return cmd | ||
|
||
} | ||
|
||
func runGet(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.GroupsApi.GetGroupById(opts.f.Context, opts.groupId) | ||
|
||
groupMetaData, _, err := request.Execute() | ||
if err != nil { | ||
return registrycmdutil.TransformInstanceError(err) | ||
} | ||
|
||
return dump.Formatted(opts.f.IOStreams.Out, opts.outputFormat, groupMetaData) | ||
} | ||
|
||
func runInteractivePrompt(opts *options, missingFlags []string) (err error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't have interactive mode for get operations, it will be better to remove it. |
||
|
||
if slices.Contains(missingFlags, "group-id") { | ||
settingNamePrompt := &survey.Input{ | ||
Message: opts.f.Localizer.MustLocalize("group.cmd.get.input.group-id.message"), | ||
} | ||
|
||
err = survey.AskOne(settingNamePrompt, &opts.groupId) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package group | ||
|
||
import ( | ||
"github.com/redhat-developer/app-services-cli/pkg/cmd/registry/group/create" | ||
"github.com/redhat-developer/app-services-cli/pkg/cmd/registry/group/get" | ||
"github.com/redhat-developer/app-services-cli/pkg/cmd/registry/group/list" | ||
"github.com/redhat-developer/app-services-cli/pkg/shared/factory" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func NewGroupCommand(f *factory.Factory) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "group", | ||
Short: f.Localizer.MustLocalize("group.cmd.description.short"), | ||
Long: f.Localizer.MustLocalize("group.cmd.description.long"), | ||
Example: f.Localizer.MustLocalize("group.cmd.example"), | ||
Args: cobra.MinimumNArgs(1), | ||
Hidden: true, | ||
} | ||
|
||
// add sub-commands | ||
cmd.AddCommand( | ||
list.NewListCommand(f), | ||
create.NewCreateCommand(f), | ||
get.NewGetCommand(f), | ||
) | ||
|
||
return cmd | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't currently have shorthand for description and properties currently.