Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 1 addition & 1 deletion cmd/rhoas/pkged.go

Large diffs are not rendered by default.

14 changes: 11 additions & 3 deletions locales/cmd/kafka/common/active.en.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,19 @@ description = "Description for --output flag"
one = 'Format in which to display the Kafka instance. Choose from: "json", "yml", "yaml"'
other = 'Format in which to display the Kafka instances. Choose from: "json", "yml", "yaml"'

[kafka.common.error.idFlagRequired]
one = '--id flag is required when name argument is not provided'

[kafka.common.error.idAndNameCannotBeUsed]
one = 'name argument and --id flag cannot be used at the same time'

[kafka.common.error.couldNotFetchKafkas]
description = 'Error message when list of Kafka instances could not be fetched'
one = 'unable to list Kafka instances'

[kafka.common.input.instanceName.message]
one = 'Select Kafka instance to connect:'

[kafka.common.log.info.noKafkaInstances]
description = 'Info message when no Kafka instances were found'
one = 'No Kafka instances were found.'

[kafka.topic.common.error.topicNotFoundError]
one = 'topic "{{.TopicName}}" does not exist in Kafka instance "{{.InstanceName}}"'
6 changes: 1 addition & 5 deletions locales/cmd/kafka/list/active.en.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,4 @@ one = 'Text search to filter the Kafka instances by name, owner, cloud_provider,

[kafka.list.log.debug.filteringKafkaList]
description = 'Debug message when filtering the list of Kafka instances'
one = 'Filtering Kafka instances with the query "{{.Search}}"'

[kafka.list.log.info.noKafkaInstances]
description = 'Info message when no Kafkas were found'
one = 'No Kafka instances were found.'
one = 'Filtering Kafka instances with the query "{{.Search}}"'
3 changes: 3 additions & 0 deletions locales/cmd/kafka/use/active.en.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ one = 'Unique ID of the Kafka instance you want to set as the current instance.'
description = 'Error message when current Kafka could not be saved in config'
one = 'could not set "{{.Name}}" as the current Kafka instance'

[kafka.use.error.idOrNameRequired]
one= '--id or name required when not running interactively'

[kafka.use.log.info.useSuccess]
description = 'Info message when current Kafka was set'
one = 'Kafka instance "{{.Name}}" has been set as the current instance.'
2 changes: 1 addition & 1 deletion pkg/cmd/kafka/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func runList(opts *options) error {
}

if response.Size == 0 {
logger.Info(localizer.MustLocalizeFromID("kafka.list.log.info.noKafkaInstances"))
logger.Info(localizer.MustLocalizeFromID("kafka.common.log.info.noKafkaInstances"))
return nil
}

Expand Down
51 changes: 45 additions & 6 deletions pkg/cmd/kafka/use/use.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

kasclient "github.com/bf2fc6cc711aee1a0c2a/cli/pkg/api/kas/client"
"github.com/bf2fc6cc711aee1a0c2a/cli/pkg/connection"
"github.com/bf2fc6cc711aee1a0c2a/cli/pkg/iostreams"

"github.com/bf2fc6cc711aee1a0c2a/cli/pkg/cmdutil"

Expand All @@ -20,20 +21,23 @@ import (
"github.com/bf2fc6cc711aee1a0c2a/cli/pkg/logging"
)

type options struct {
id string
name string
type Options struct {
id string
name string
interactive bool

IO *iostreams.IOStreams
Config config.IConfig
Connection factory.ConnectionFunc
Logger func() (logging.Logger, error)
}

func NewUseCommand(f *factory.Factory) *cobra.Command {
opts := &options{
opts := &Options{
Config: f.Config,
Connection: f.Connection,
Logger: f.Logger,
IO: f.IOStreams,
}

cmd := &cobra.Command{
Expand All @@ -53,7 +57,10 @@ func NewUseCommand(f *factory.Factory) *cobra.Command {
if len(args) > 0 {
opts.name = args[0]
} else if opts.id == "" {
return errors.New(localizer.MustLocalizeFromID("kafka.common.error.idFlagRequired"))
if !opts.IO.CanPrompt() {
return errors.New(localizer.MustLocalizeFromID("kafka.use.error.idOrNameRequired"))
}
opts.interactive = true
}

if opts.name != "" && opts.id != "" {
Expand All @@ -69,7 +76,16 @@ func NewUseCommand(f *factory.Factory) *cobra.Command {
return cmd
}

func runUse(opts *options) error {
func runUse(opts *Options) error {

if opts.interactive {
// run the use command interactively
err := runInteractivePrompt(opts)
if err != nil {
return err
}
}

logger, err := opts.Logger()
if err != nil {
return err
Expand Down Expand Up @@ -126,3 +142,26 @@ func runUse(opts *options) error {

return nil
}

func runInteractivePrompt(opts *Options) error {
logger, err := opts.Logger()
if err != nil {
return err
}

connection, err := opts.Connection(connection.DefaultConfigSkipMasAuth)
if err != nil {
return err
}

logger.Debug(localizer.MustLocalizeFromID("common.log.debug.startingInteractivePrompt"))

selectedKafka, err := kafka.InteractiveSelect(connection, logger)
if err != nil {
return err
}

opts.name = selectedKafka.GetName()

return nil
}
16 changes: 11 additions & 5 deletions pkg/kafka/kafka.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,26 @@ import (
kasclient "github.com/bf2fc6cc711aee1a0c2a/cli/pkg/api/kas/client"

"github.com/AlecAivazis/survey/v2"
"github.com/bf2fc6cc711aee1a0c2a/cli/internal/localizer"
"github.com/bf2fc6cc711aee1a0c2a/cli/pkg/connection"
"github.com/bf2fc6cc711aee1a0c2a/cli/pkg/logging"
)

const (
queryLimit = "1000"
)

func InteractiveSelect(connection connection.Connection, logger logging.Logger) (*kasclient.KafkaRequest, error) {
api := connection.API()

response, _, apiErr := api.Kafka().ListKafkas(context.Background()).Execute()
response, _, apiErr := api.Kafka().ListKafkas(context.Background()).Size(queryLimit).Execute()

if apiErr.Error() != "" {
return nil, fmt.Errorf("Unable to list Kafka instances: %w", apiErr)
return nil, fmt.Errorf("%v: %w", localizer.MustLocalizeFromID("kafka.common.error.couldNotFetchKafkas"), apiErr)
}

if response.Size == 0 {
logger.Info("No Kafka instances")
Copy link
Contributor

Choose a reason for hiding this comment

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

Thank you for localizing things as you go along :)

logger.Info(localizer.MustLocalizeFromID("kafka.common.log.info.noKafkaInstances"))
return nil, nil
}

Expand All @@ -31,8 +36,9 @@ func InteractiveSelect(connection connection.Connection, logger logging.Logger)
}

prompt := &survey.Select{
Message: "Select Kafka cluster to connect",
Options: kafkas,
Message: localizer.MustLocalizeFromID("kafka.common.input.instanceName.message"),
Options: kafkas,
PageSize: 10,
}

var selectedKafkaIndex int
Expand Down