-
Notifications
You must be signed in to change notification settings - Fork 0
/
interactive.go
51 lines (39 loc) · 1.16 KB
/
interactive.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package kafka
import (
"context"
"fmt"
"github.com/AlecAivazis/survey/v2"
"github.com/aerogear/charmil-host-example/pkg/connection"
kafkamgmtclient "github.com/redhat-developer/app-services-sdk-go/kafkamgmt/apiv1/client"
"github.com/aerogear/charmil/core/utils/logging"
)
const (
queryLimit = "1000"
)
func InteractiveSelect(connection connection.Connection, logger logging.Logger) (*kafkamgmtclient.KafkaRequest, error) {
api := connection.API()
response, _, err := api.Kafka().GetKafkas(context.Background()).Size(queryLimit).Execute()
if err != nil {
return nil, fmt.Errorf("unable to list Kafka instances: %w", err)
}
if response.Size == 0 {
logger.Info("No Kafka instances were found.")
return nil, nil
}
kafkas := []string{}
for index := 0; index < len(response.Items); index++ {
kafkas = append(kafkas, *response.Items[index].Name)
}
prompt := &survey.Select{
Message: "Select Kafka instance to connect:",
Options: kafkas,
PageSize: 10,
}
var selectedKafkaIndex int
err = survey.AskOne(prompt, &selectedKafkaIndex)
if err != nil {
return nil, err
}
selectedKafka := response.Items[selectedKafkaIndex]
return &selectedKafka, nil
}