Skip to content

Commit a0eee4e

Browse files
authored
fix(ske): print valid json output when listing clusters (#940)
also print valid json output when project has no ske clusters relates to STACKITCLI-240
1 parent 798c0c3 commit a0eee4e

File tree

3 files changed

+17
-14
lines changed

3 files changed

+17
-14
lines changed

internal/cmd/root.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ import (
66
"strings"
77
"time"
88

9-
"github.com/stackitcloud/stackit-cli/internal/cmd/params"
10-
119
affinityGroups "github.com/stackitcloud/stackit-cli/internal/cmd/affinity-groups"
1210
"github.com/stackitcloud/stackit-cli/internal/cmd/auth"
1311
"github.com/stackitcloud/stackit-cli/internal/cmd/beta"
@@ -28,6 +26,7 @@ import (
2826
"github.com/stackitcloud/stackit-cli/internal/cmd/observability"
2927
"github.com/stackitcloud/stackit-cli/internal/cmd/opensearch"
3028
"github.com/stackitcloud/stackit-cli/internal/cmd/organization"
29+
"github.com/stackitcloud/stackit-cli/internal/cmd/params"
3130
"github.com/stackitcloud/stackit-cli/internal/cmd/postgresflex"
3231
"github.com/stackitcloud/stackit-cli/internal/cmd/project"
3332
publicip "github.com/stackitcloud/stackit-cli/internal/cmd/public-ip"

internal/cmd/ske/cluster/list/list.go

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -85,22 +85,21 @@ func NewCmd(params *params.CmdParams) *cobra.Command {
8585
return fmt.Errorf("get SKE clusters: %w", err)
8686
}
8787
clusters := *resp.Items
88-
if len(clusters) == 0 {
89-
projectLabel, err := projectname.GetProjectName(ctx, params.Printer, params.CliVersion, cmd)
90-
if err != nil {
91-
params.Printer.Debug(print.ErrorLevel, "get project name: %v", err)
92-
projectLabel = model.ProjectId
93-
}
94-
params.Printer.Info("No clusters found for project %q\n", projectLabel)
95-
return nil
96-
}
9788

9889
// Truncate output
9990
if model.Limit != nil && len(clusters) > int(*model.Limit) {
10091
clusters = clusters[:*model.Limit]
10192
}
10293

103-
return outputResult(params.Printer, model.OutputFormat, clusters)
94+
projectLabel := model.ProjectId
95+
if len(clusters) == 0 {
96+
projectLabel, err = projectname.GetProjectName(ctx, params.Printer, params.CliVersion, cmd)
97+
if err != nil {
98+
params.Printer.Debug(print.ErrorLevel, "get project name: %v", err)
99+
}
100+
}
101+
102+
return outputResult(params.Printer, model.OutputFormat, projectLabel, clusters)
104103
},
105104
}
106105

@@ -148,7 +147,7 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *ske.APIClie
148147
return req
149148
}
150149

151-
func outputResult(p *print.Printer, outputFormat string, clusters []ske.Cluster) error {
150+
func outputResult(p *print.Printer, outputFormat, projectLabel string, clusters []ske.Cluster) error {
152151
switch outputFormat {
153152
case print.JSONOutputFormat:
154153
details, err := json.MarshalIndent(clusters, "", " ")
@@ -167,6 +166,11 @@ func outputResult(p *print.Printer, outputFormat string, clusters []ske.Cluster)
167166

168167
return nil
169168
default:
169+
if len(clusters) == 0 {
170+
p.Outputf("No clusters found for project %q\n", projectLabel)
171+
return nil
172+
}
173+
170174
table := tables.NewTable()
171175
table.SetHeader("NAME", "STATE", "VERSION", "POOLS", "MONITORING")
172176
for i := range clusters {

internal/cmd/ske/cluster/list/list_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ func TestOutputResult(t *testing.T) {
226226
p.Cmd = NewCmd(&params.CmdParams{Printer: p})
227227
for _, tt := range tests {
228228
t.Run(tt.name, func(t *testing.T) {
229-
if err := outputResult(p, tt.args.outputFormat, tt.args.clusters); (err != nil) != tt.wantErr {
229+
if err := outputResult(p, tt.args.outputFormat, "dummy-projectlabel", tt.args.clusters); (err != nil) != tt.wantErr {
230230
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
231231
}
232232
})

0 commit comments

Comments
 (0)