diff --git a/pkg/cmd/kafka/delete/delete.go b/pkg/cmd/kafka/delete/delete.go index 56b6151380..559898d5e4 100644 --- a/pkg/cmd/kafka/delete/delete.go +++ b/pkg/cmd/kafka/delete/delete.go @@ -6,6 +6,7 @@ import ( "fmt" "github.com/redhat-developer/app-services-cli/pkg/connection" + kafkacmdutil "github.com/redhat-developer/app-services-cli/pkg/kafka/cmdutil" "github.com/redhat-developer/app-services-cli/pkg/localize" "github.com/redhat-developer/app-services-cli/pkg/iostreams" @@ -48,16 +49,12 @@ func NewDeleteCommand(f *factory.Factory) *cobra.Command { Short: opts.localizer.MustLocalize("kafka.delete.cmd.shortDescription"), Long: opts.localizer.MustLocalize("kafka.delete.cmd.longDescription"), Example: opts.localizer.MustLocalize("kafka.delete.cmd.example"), - Args: cobra.RangeArgs(0, 1), + Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, args []string) error { if !opts.IO.CanPrompt() && !opts.force { return flag.RequiredWhenNonInteractiveError("yes") } - if len(args) > 0 { - opts.name = args[0] - } - if opts.name != "" && opts.id != "" { return errors.New(opts.localizer.MustLocalize("service.error.idAndNameCannotBeUsed")) } @@ -84,6 +81,9 @@ func NewDeleteCommand(f *factory.Factory) *cobra.Command { cmd.Flags().StringVar(&opts.id, "id", "", opts.localizer.MustLocalize("kafka.delete.flag.id")) cmd.Flags().BoolVarP(&opts.force, "yes", "y", false, opts.localizer.MustLocalize("kafka.delete.flag.yes")) + cmd.Flags().StringVar(&opts.name, "name", "", opts.localizer.MustLocalize("kafka.describe.flag.name")) + + kafkacmdutil.RegisterNameFlagCompletionFunc(cmd, f) return cmd } diff --git a/pkg/cmd/kafka/describe/describe.go b/pkg/cmd/kafka/describe/describe.go index 01accaee40..fbd26f7739 100644 --- a/pkg/cmd/kafka/describe/describe.go +++ b/pkg/cmd/kafka/describe/describe.go @@ -8,13 +8,13 @@ import ( 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/iostreams" + kafkacmdutil "github.com/redhat-developer/app-services-cli/pkg/kafka/cmdutil" "github.com/redhat-developer/app-services-cli/pkg/localize" "github.com/redhat-developer/app-services-cli/pkg/cmd/flag" "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/cmdutil" "github.com/redhat-developer/app-services-cli/pkg/dump" "github.com/redhat-developer/app-services-cli/pkg/kafka" kafkamgmtclient "github.com/redhat-developer/app-services-sdk-go/kafkamgmt/apiv1/client" @@ -48,21 +48,13 @@ func NewDescribeCommand(f *factory.Factory) *cobra.Command { Short: opts.localizer.MustLocalize("kafka.describe.cmd.shortDescription"), Long: opts.localizer.MustLocalize("kafka.describe.cmd.longDescription"), Example: opts.localizer.MustLocalize("kafka.describe.cmd.example"), - Args: cobra.RangeArgs(0, 1), - // Dynamic completion of the Kafka name - ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { - return cmdutil.FilterValidKafkas(f, toComplete) - }, + Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, args []string) error { validOutputFormats := flagutil.ValidOutputFormats if opts.outputFormat != "" && !flagutil.IsValidInput(opts.outputFormat, validOutputFormats...) { return flag.InvalidValueError("output", opts.outputFormat, validOutputFormats...) } - if len(args) > 0 { - opts.name = args[0] - } - if opts.name != "" && opts.id != "" { return errors.New(opts.localizer.MustLocalize("service.error.idAndNameCannotBeUsed")) } @@ -89,7 +81,9 @@ func NewDescribeCommand(f *factory.Factory) *cobra.Command { cmd.Flags().StringVarP(&opts.outputFormat, "output", "o", "json", opts.localizer.MustLocalize("kafka.common.flag.output.description")) cmd.Flags().StringVar(&opts.id, "id", "", opts.localizer.MustLocalize("kafka.describe.flag.id")) + cmd.Flags().StringVar(&opts.name, "name", "", opts.localizer.MustLocalize("kafka.describe.flag.id")) + kafkacmdutil.RegisterNameFlagCompletionFunc(cmd, f) flagutil.EnableOutputFlagCompletion(cmd) return cmd diff --git a/pkg/cmd/kafka/topic/describe/describe.go b/pkg/cmd/kafka/topic/describe/describe.go index 9bc096b2a9..088b1b4513 100644 --- a/pkg/cmd/kafka/topic/describe/describe.go +++ b/pkg/cmd/kafka/topic/describe/describe.go @@ -25,7 +25,7 @@ import ( ) type Options struct { - topicName string + name string kafkaID string outputFormat string @@ -51,16 +51,8 @@ func NewDescribeTopicCommand(f *factory.Factory) *cobra.Command { Short: opts.localizer.MustLocalize("kafka.topic.describe.cmd.shortDescription"), Long: opts.localizer.MustLocalize("kafka.topic.describe.cmd.longDescription"), Example: opts.localizer.MustLocalize("kafka.topic.describe.cmd.example"), - Args: cobra.ExactValidArgs(1), - // dynamic completion of topic names - ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { - return cmdutil.FilterValidTopicNameArgs(f, toComplete) - }, + Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, args []string) (err error) { - if len(args) > 0 { - opts.topicName = args[0] - } - if opts.outputFormat != "" { if err = flag.ValidateOutput(opts.outputFormat); err != nil { return err @@ -88,6 +80,12 @@ func NewDescribeTopicCommand(f *factory.Factory) *cobra.Command { cmd.Flags().StringVarP(&opts.outputFormat, "output", "o", "json", opts.localizer.MustLocalize("kafka.topic.common.flag.output.description")) + cmd.Flags().StringVar(&opts.name, "name", "", opts.localizer.MustLocalize("kafka.topic.common.flag.output.description")) + _ = cmd.RegisterFlagCompletionFunc("name", func(cmd *cobra.Command, _ []string, toComplete string) ([]string, cobra.ShellCompDirective) { + return cmdutil.FilterValidTopicNameArgs(f, toComplete) + }) + _ = cmd.MarkFlagRequired("name") + flagutil.EnableOutputFlagCompletion(cmd) return cmd @@ -106,14 +104,14 @@ func runCmd(opts *Options) error { // fetch the topic topicResponse, httpRes, err := api.TopicsApi. - GetTopic(context.Background(), opts.topicName). + GetTopic(context.Background(), opts.name). Execute() if err != nil { if httpRes == nil { return err } - topicNameTmplPair := localize.NewEntry("TopicName", opts.topicName) + topicNameTmplPair := localize.NewEntry("TopicName", opts.name) kafkaNameTmplPair := localize.NewEntry("InstanceName", kafkaInstance.GetName()) operationTmplPair := localize.NewEntry("Operation", "delete") diff --git a/pkg/cmd/kafka/use/use.go b/pkg/cmd/kafka/use/use.go index ff43c0f546..20a4820160 100644 --- a/pkg/cmd/kafka/use/use.go +++ b/pkg/cmd/kafka/use/use.go @@ -7,11 +7,10 @@ import ( "github.com/redhat-developer/app-services-cli/pkg/connection" "github.com/redhat-developer/app-services-cli/pkg/iostreams" + kafkacmdutil "github.com/redhat-developer/app-services-cli/pkg/kafka/cmdutil" "github.com/redhat-developer/app-services-cli/pkg/localize" kafkamgmtclient "github.com/redhat-developer/app-services-sdk-go/kafkamgmt/apiv1/client" - "github.com/redhat-developer/app-services-cli/pkg/cmdutil" - "github.com/redhat-developer/app-services-cli/pkg/kafka" "github.com/spf13/cobra" @@ -48,9 +47,6 @@ func NewUseCommand(f *factory.Factory) *cobra.Command { Long: opts.localizer.MustLocalize("kafka.use.cmd.longDescription"), Example: opts.localizer.MustLocalize("kafka.use.cmd.example"), Args: cobra.NoArgs, - ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { - return cmdutil.FilterValidKafkas(f, toComplete) - }, RunE: func(cmd *cobra.Command, args []string) error { if opts.id == "" && opts.name == "" { if !opts.IO.CanPrompt() { @@ -69,6 +65,9 @@ func NewUseCommand(f *factory.Factory) *cobra.Command { cmd.Flags().StringVar(&opts.id, "id", "", opts.localizer.MustLocalize("kafka.use.flag.id")) cmd.Flags().StringVar(&opts.name, "name", "", opts.localizer.MustLocalize("kafka.use.flag.name")) + + kafkacmdutil.RegisterNameFlagCompletionFunc(cmd, f) + return cmd } diff --git a/pkg/kafka/cmdutil/util.go b/pkg/kafka/cmdutil/util.go new file mode 100644 index 0000000000..df8430903d --- /dev/null +++ b/pkg/kafka/cmdutil/util.go @@ -0,0 +1,13 @@ +package kafkacmdutil + +import ( + "github.com/redhat-developer/app-services-cli/pkg/cmd/factory" + "github.com/redhat-developer/app-services-cli/pkg/cmdutil" + "github.com/spf13/cobra" +) +// RegisterNameFlagCompletionFunc adds dynamic completion for the --name flag +func RegisterNameFlagCompletionFunc(cmd *cobra.Command, f *factory.Factory) error { + return cmd.RegisterFlagCompletionFunc("name", func(cmd *cobra.Command, _ []string, toComplete string) ([]string, cobra.ShellCompDirective) { + return cmdutil.FilterValidKafkas(f, toComplete) + }) +} diff --git a/pkg/localize/locales/en/cmd/kafka_delete.en.toml b/pkg/localize/locales/en/cmd/kafka_delete.en.toml index 50cc00bed8..92700fa31a 100644 --- a/pkg/localize/locales/en/cmd/kafka_delete.en.toml +++ b/pkg/localize/locales/en/cmd/kafka_delete.en.toml @@ -27,7 +27,11 @@ $ rhoas kafka delete --id=1iSY6RQ3JKI8Q0OTmjQFd3ocFRg [kafka.delete.flag.id] description = 'Description for the --id flag' -one = 'Unique ID of the Kafka instance you want to delete (if not provided, the current Kafka instance will be deleted)' +one = 'Unique ID of the Kafka instance you want to delete' + +[kafka.describe.flag.name] +description = 'Description for the --name flag' +one = 'Name of the Kafka instance you want to delete' [kafka.delete.flag.yes] description = 'Description for the --yes flag' diff --git a/pkg/localize/locales/en/cmd/kafka_describe.en.toml b/pkg/localize/locales/en/cmd/kafka_describe.en.toml index 94609e01db..8dbbd2f096 100644 --- a/pkg/localize/locales/en/cmd/kafka_describe.en.toml +++ b/pkg/localize/locales/en/cmd/kafka_describe.en.toml @@ -33,4 +33,8 @@ $ rhoas kafka describe -o yaml [kafka.describe.flag.id] description = 'Description for the --id flag' -one = 'Unique ID of the Kafka instance you want to view (if not provided, the current Kafka instance will be displayed)' +one = 'Unique ID of the Kafka instance you want to view' + +[kafka.describe.flag.name] +description = 'Description for the --name flag' +one = 'Name of the Kafka instance you want to view' diff --git a/pkg/localize/locales/en/cmd/kafka_topic_describe.en.toml b/pkg/localize/locales/en/cmd/kafka_topic_describe.en.toml index c65eab2c53..1ee7ad0ffb 100644 --- a/pkg/localize/locales/en/cmd/kafka_topic_describe.en.toml +++ b/pkg/localize/locales/en/cmd/kafka_topic_describe.en.toml @@ -9,6 +9,9 @@ one = ''' Print detailed configuration information for a Kafka topic. ''' +[kafka.topic.describe.flag.name] +one = 'Name of the Kafka topic you want to view' + [kafka.topic.describe.cmd.example] one = ''' # describe a topic diff --git a/pkg/localize/locales/en/service.crud.en.toml b/pkg/localize/locales/en/service.crud.en.toml index a4c0355279..b94e18972b 100644 --- a/pkg/localize/locales/en/service.crud.en.toml +++ b/pkg/localize/locales/en/service.crud.en.toml @@ -6,4 +6,4 @@ In order to be able to create a new instance, you must first review and accept t ''' [service.error.idAndNameCannotBeUsed] -one = 'name argument and --id flag cannot be used at the same time' \ No newline at end of file +one = '--name and --id flag cannot be used at the same time' \ No newline at end of file