Skip to content

Commit 8707c89

Browse files
author
Enda Phelan
committed
feat(cmd): Display message if there are no clusters
1 parent d1cb530 commit 8707c89

File tree

1 file changed

+24
-20
lines changed

1 file changed

+24
-20
lines changed

cmd/rhmas/kafka/list/list.go

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
package list
22

33
import (
4-
"os"
54
"context"
65
"encoding/json"
76
"fmt"
7+
"os"
88

99
"github.com/antihax/optional"
1010
"github.com/bf2fc6cc711aee1a0c2a/cli/cmd/rhmas/flags"
11-
"github.com/bf2fc6cc711aee1a0c2a/cli/pkg/kafka"
1211
"github.com/bf2fc6cc711aee1a0c2a/cli/pkg/api/rhmas"
12+
"github.com/bf2fc6cc711aee1a0c2a/cli/pkg/kafka"
1313
"github.com/spf13/cobra"
1414

1515
mas "github.com/bf2fc6cc711aee1a0c2a/cli/client/mas"
@@ -44,28 +44,32 @@ func runList(cmd *cobra.Command, _ []string) {
4444

4545
client := rhmas.BuildClient()
4646
options := mas.ApiManagedServicesApiV1KafkasGetOpts{Page: optional.NewString(page), Size: optional.NewString(size)}
47-
response, status, err := client.DefaultApi.ApiManagedServicesApiV1KafkasGet(context.Background(), &options)
47+
response, _, err := client.DefaultApi.ApiManagedServicesApiV1KafkasGet(context.Background(), &options)
4848

4949
if err != nil {
50-
fmt.Fprintf(os.Stderr, "Error retrieving Kafka clusters: %v", err)
50+
fmt.Fprintf(os.Stderr, "Error retrieving Kafka clusters: %v\n", err)
51+
os.Exit(1)
52+
}
53+
54+
if response.Size == 0 {
55+
fmt.Fprintln(os.Stderr, "No Kafka clusters found.")
5156
return
5257
}
5358

54-
if status.StatusCode == 200 {
55-
jsonResponse, _ := json.Marshal(response)
56-
57-
var kafkaList kafka.ClusterList
58-
if err = json.Unmarshal(jsonResponse, &kafkaList); err != nil {
59-
fmt.Fprintf(os.Stderr, "Could not format Kakfa cluster to table: %v", err)
60-
outputFormat = "json"
61-
}
62-
63-
switch outputFormat {
64-
case "json":
65-
data, _ := json.MarshalIndent(kafkaList.Items, "", " ")
66-
fmt.Print(string(data))
67-
default:
68-
kafka.PrintToTable(kafkaList.Items)
69-
}
59+
jsonResponse, _ := json.Marshal(response)
60+
61+
var kafkaList kafka.ClusterList
62+
63+
if err = json.Unmarshal(jsonResponse, &kafkaList); err != nil {
64+
fmt.Fprintf(os.Stderr, "Could not unmarshal Kakfa items into table: %v", err)
65+
outputFormat = "json"
66+
}
67+
68+
switch outputFormat {
69+
case "json":
70+
data, _ := json.MarshalIndent(kafkaList.Items, "", " ")
71+
fmt.Print(string(data))
72+
default:
73+
kafka.PrintToTable(kafkaList.Items)
7074
}
7175
}

0 commit comments

Comments
 (0)