Skip to content

Commit

Permalink
feat(kafka list): add search flag
Browse files Browse the repository at this point in the history
Search flag has been added to kafka list to filter by text.

fixes #181
  • Loading branch information
rkpattnaik780 committed Feb 22, 2021
1 parent 85b00d7 commit 58af1e1
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/commands/rhoas_kafka_list.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ rhoas kafka list [flags]
--limit int The maximum number of Kafka instances to be returned (default 100)
-o, --output string Format in which to display the Kafka instances. Choose from: "json", "yml", "yaml"
--page int Display the Kafka instances from the specified page number.
--search string Text search to filter the Kafka instances
....

=== Options inherited from parent commands
Expand Down
4 changes: 4 additions & 0 deletions locales/cmd/kafka/list/active.en.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ one = 'Display the Kafka instances from the specified page number.'
description = 'Description for the --limit flag'
one = 'The maximum number of Kafka instances to be returned'

[kafka.list.flag.search]
description = 'Description for the --search flag'
one = 'Text search to filter the Kafka instances'

[kafka.list.log.info.noKafkaInstances]
description = 'Info message when no Kafkas were found'
one = 'No Kafka instances were found.'
20 changes: 20 additions & 0 deletions pkg/cmd/kafka/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package list
import (
"context"
"encoding/json"
"fmt"
"strconv"

kasclient "github.com/bf2fc6cc711aee1a0c2a/cli/pkg/api/kas/client"
Expand Down Expand Up @@ -37,6 +38,7 @@ type options struct {
outputFormat string
page int
limit int
search string

IO *iostreams.IOStreams
Config config.IConfig
Expand All @@ -49,6 +51,7 @@ func NewListCommand(f *factory.Factory) *cobra.Command {
opts := &options{
page: 0,
limit: 100,
search: "",
Config: f.Config,
Connection: f.Connection,
Logger: f.Logger,
Expand All @@ -75,6 +78,7 @@ func NewListCommand(f *factory.Factory) *cobra.Command {
}))
cmd.Flags().IntVarP(&opts.page, "page", "", 0, localizer.MustLocalizeFromID("kafka.list.flag.page"))
cmd.Flags().IntVarP(&opts.limit, "limit", "", 100, localizer.MustLocalizeFromID("kafka.list.flag.limit"))
cmd.Flags().StringVarP(&opts.search, "search", "", "", localizer.MustLocalizeFromID("kafka.list.flag.search"))

return cmd
}
Expand All @@ -95,6 +99,7 @@ func runList(opts *options) error {
a := api.Kafka().ListKafkas(context.Background())
a = a.Page(strconv.Itoa(opts.page))
a = a.Size(strconv.Itoa(opts.limit))
a = a.Search(buildQuery(opts.search))
response, _, apiErr := a.Execute()

if apiErr.Error() != "" {
Expand Down Expand Up @@ -140,3 +145,18 @@ func mapResponseItemsToRows(kafkas []kasclient.KafkaRequest) []kafkaRow {

return rows
}

func buildQuery(search string) string {

var queryString string

if search != "" {
queryString = fmt.Sprintf(
"name like %%%[1]v%% or owner like %%%[1]v%% or cloud_provider like %%%[1]v%% or region like %%%[1]v%% or status like %%%[1]v%%",
search,
)
}

return queryString

}

0 comments on commit 58af1e1

Please sign in to comment.