Skip to content

Commit f44493d

Browse files
committed
fix(alb): print valid JSON/YAML output for list cmds
relates to STACKITCLI-273 / #893
1 parent 99f4d2d commit f44493d

File tree

5 files changed

+43
-38
lines changed

5 files changed

+43
-38
lines changed

internal/cmd/beta/alb/list/list.go

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ type inputModel struct {
2525
}
2626

2727
const (
28-
labelSelectorFlag = "label-selector"
29-
limitFlag = "limit"
28+
limitFlag = "limit"
3029
)
3130

3231
func NewCmd(params *params.CmdParams) *cobra.Command {
@@ -73,19 +72,14 @@ func NewCmd(params *params.CmdParams) *cobra.Command {
7372
if err != nil {
7473
return fmt.Errorf("list load balancerse: %w", err)
7574
}
75+
items := response.GetLoadBalancers()
7676

77-
if items := response.LoadBalancers; items == nil || len(*items) == 0 {
78-
params.Printer.Info("No load balancers found for project %q", projectLabel)
79-
} else {
80-
if model.Limit != nil && len(*items) > int(*model.Limit) {
81-
*items = (*items)[:*model.Limit]
82-
}
83-
if err := outputResult(params.Printer, model.OutputFormat, *items); err != nil {
84-
return fmt.Errorf("output loadbalancers: %w", err)
85-
}
77+
// Truncate output
78+
if model.Limit != nil && len(items) > int(*model.Limit) {
79+
items = items[:*model.Limit]
8680
}
8781

88-
return nil
82+
return outputResult(params.Printer, model.OutputFormat, projectLabel, items)
8983
},
9084
}
9185

@@ -125,8 +119,13 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *alb.APIClie
125119

126120
return request
127121
}
128-
func outputResult(p *print.Printer, outputFormat string, items []alb.LoadBalancer) error {
122+
func outputResult(p *print.Printer, outputFormat, projectLabel string, items []alb.LoadBalancer) error {
129123
return p.OutputResult(outputFormat, items, func() error {
124+
if len(items) == 0 {
125+
p.Outputf("No load balancers found for project %q", projectLabel)
126+
return nil
127+
}
128+
130129
table := tables.NewTable()
131130
table.SetHeader("NAME", "EXTERNAL ADDRESS", "REGION", "STATUS", "VERSION", "ERRORS")
132131
for i := range items {

internal/cmd/beta/alb/list/list_test.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package list
22

33
import (
44
"context"
5+
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
56
"strconv"
67
"testing"
78

@@ -19,11 +20,14 @@ import (
1920
type testCtxKey struct{}
2021

2122
var (
22-
testCtx = context.WithValue(context.Background(), testCtxKey{}, "foo")
23-
testClient = &alb.APIClient{}
24-
testProjectId = uuid.NewString()
25-
testRegion = "eu01"
26-
testLimit int64 = 10
23+
testCtx = context.WithValue(context.Background(), testCtxKey{}, "foo")
24+
testClient = &alb.APIClient{}
25+
testProjectId = uuid.NewString()
26+
)
27+
28+
const (
29+
testRegion = "eu01"
30+
testLimit int64 = 10
2731
)
2832

2933
func fixtureFlagValues(mods ...func(flagValues map[string]string)) map[string]string {
@@ -41,7 +45,7 @@ func fixtureFlagValues(mods ...func(flagValues map[string]string)) map[string]st
4145
func fixtureInputModel(mods ...func(model *inputModel)) *inputModel {
4246
model := &inputModel{
4347
GlobalFlagModel: &globalflags.GlobalFlagModel{ProjectId: testProjectId, Region: testRegion, Verbosity: globalflags.VerbosityDefault},
44-
Limit: &testLimit,
48+
Limit: utils.Ptr(testLimit),
4549
}
4650
for _, mod := range mods {
4751
mod(model)
@@ -136,6 +140,7 @@ func TestBuildRequest(t *testing.T) {
136140
func Test_outputResult(t *testing.T) {
137141
type args struct {
138142
outputFormat string
143+
projectLabel string
139144
items []alb.LoadBalancer
140145
}
141146
tests := []struct {
@@ -164,7 +169,7 @@ func Test_outputResult(t *testing.T) {
164169
p.Cmd = NewCmd(&params.CmdParams{Printer: p})
165170
for _, tt := range tests {
166171
t.Run(tt.name, func(t *testing.T) {
167-
if err := outputResult(p, tt.args.outputFormat, tt.args.items); (err != nil) != tt.wantErr {
172+
if err := outputResult(p, tt.args.outputFormat, tt.args.projectLabel, tt.args.items); (err != nil) != tt.wantErr {
168173
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
169174
}
170175
})

internal/cmd/beta/alb/observability-credentials/list/list.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,9 @@ func NewCmd(params *params.CmdParams) *cobra.Command {
6868
if err != nil {
6969
return fmt.Errorf("list credentials: %w", err)
7070
}
71+
items := resp.GetCredentials()
7172

72-
if resp.Credentials == nil || len(*resp.Credentials) == 0 {
73-
params.Printer.Info("No credentials found\n")
74-
return nil
75-
}
76-
77-
items := *resp.Credentials
73+
// Truncate output
7874
if model.Limit != nil && len(items) > int(*model.Limit) {
7975
items = items[:*model.Limit]
8076
}
@@ -122,6 +118,11 @@ func outputResult(p *print.Printer, outputFormat string, items []alb.Credentials
122118
}
123119

124120
return p.OutputResult(outputFormat, items, func() error {
121+
if len(items) == 0 {
122+
p.Outputf("No credentials found\n")
123+
return nil
124+
}
125+
125126
table := tables.NewTable()
126127
table.SetHeader("CREDENTIAL REF", "DISPLAYNAME", "USERNAME", "REGION")
127128

internal/cmd/beta/alb/plans/plans.go

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -62,16 +62,9 @@ func NewCmd(params *params.CmdParams) *cobra.Command {
6262
if err != nil {
6363
return fmt.Errorf("list plans: %w", err)
6464
}
65+
items := response.GetValidPlans()
6566

66-
if items := response.ValidPlans; items == nil || len(*items) == 0 {
67-
params.Printer.Info("No plans found for project %q", projectLabel)
68-
} else {
69-
if err := outputResult(params.Printer, model.OutputFormat, *items); err != nil {
70-
return fmt.Errorf("output plans: %w", err)
71-
}
72-
}
73-
74-
return nil
67+
return outputResult(params.Printer, model.OutputFormat, projectLabel, items)
7568
},
7669
}
7770

@@ -98,8 +91,13 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *alb.APIClie
9891
return request
9992
}
10093

101-
func outputResult(p *print.Printer, outputFormat string, items []alb.PlanDetails) error {
94+
func outputResult(p *print.Printer, outputFormat, projectLabel string, items []alb.PlanDetails) error {
10295
return p.OutputResult(outputFormat, items, func() error {
96+
if len(items) == 0 {
97+
p.Outputf("No plans found for project %q", projectLabel)
98+
return nil
99+
}
100+
103101
table := tables.NewTable()
104102
table.SetHeader("PLAN ID", "NAME", "FLAVOR", "MAX CONNS", "DESCRIPTION")
105103
for _, item := range items {

internal/cmd/beta/alb/plans/plans_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,10 @@ var (
2121
testCtx = context.WithValue(context.Background(), testCtxKey{}, "foo")
2222
testClient = &alb.APIClient{}
2323
testProjectId = uuid.NewString()
24-
testRegion = "eu01"
2524
)
2625

26+
const testRegion = "eu01"
27+
2728
func fixtureFlagValues(mods ...func(flagValues map[string]string)) map[string]string {
2829
flagValues := map[string]string{
2930
globalflags.ProjectIdFlag: testProjectId,
@@ -132,6 +133,7 @@ func TestBuildRequest(t *testing.T) {
132133
func Test_outputResult(t *testing.T) {
133134
type args struct {
134135
outputFormat string
136+
projectLabel string
135137
items []alb.PlanDetails
136138
}
137139
tests := []struct {
@@ -160,7 +162,7 @@ func Test_outputResult(t *testing.T) {
160162
p.Cmd = NewCmd(&params.CmdParams{Printer: p})
161163
for _, tt := range tests {
162164
t.Run(tt.name, func(t *testing.T) {
163-
if err := outputResult(p, tt.args.outputFormat, tt.args.items); (err != nil) != tt.wantErr {
165+
if err := outputResult(p, tt.args.outputFormat, tt.args.projectLabel, tt.args.items); (err != nil) != tt.wantErr {
164166
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
165167
}
166168
})

0 commit comments

Comments
 (0)