Skip to content

Commit fcb220a

Browse files
committed
fix(mariadb): print valid JSON/YAML for list commands
relates to STACKITCLI-263 / #893
1 parent 99f4d2d commit fcb220a

File tree

6 files changed

+63
-60
lines changed

6 files changed

+63
-60
lines changed

internal/cmd/mariadb/credentials/list/list.go

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -66,22 +66,19 @@ func NewCmd(params *params.CmdParams) *cobra.Command {
6666
if err != nil {
6767
return fmt.Errorf("list MariaDB credentials: %w", err)
6868
}
69-
credentials := *resp.CredentialsList
70-
if len(credentials) == 0 {
71-
instanceLabel, err := mariadbUtils.GetInstanceName(ctx, apiClient, model.ProjectId, model.InstanceId)
72-
if err != nil {
73-
params.Printer.Debug(print.ErrorLevel, "get instance name: %v", err)
74-
instanceLabel = model.InstanceId
75-
}
76-
params.Printer.Info("No credentials found for instance %q\n", instanceLabel)
77-
return nil
69+
credentials := resp.GetCredentialsList()
70+
71+
instanceLabel, err := mariadbUtils.GetInstanceName(ctx, apiClient, model.ProjectId, model.InstanceId)
72+
if err != nil {
73+
params.Printer.Debug(print.ErrorLevel, "get instance name: %v", err)
74+
instanceLabel = model.InstanceId
7875
}
7976

8077
// Truncate output
8178
if model.Limit != nil && len(credentials) > int(*model.Limit) {
8279
credentials = credentials[:*model.Limit]
8380
}
84-
return outputResult(params.Printer, model.OutputFormat, credentials)
81+
return outputResult(params.Printer, model.OutputFormat, instanceLabel, credentials)
8582
},
8683
}
8784
configureFlags(cmd)
@@ -125,8 +122,13 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *mariadb.API
125122
return req
126123
}
127124

128-
func outputResult(p *print.Printer, outputFormat string, credentials []mariadb.CredentialsListItem) error {
125+
func outputResult(p *print.Printer, outputFormat, instanceLabel string, credentials []mariadb.CredentialsListItem) error {
129126
return p.OutputResult(outputFormat, credentials, func() error {
127+
if len(credentials) == 0 {
128+
p.Outputf("No credentials found for instance %q\n", instanceLabel)
129+
return nil
130+
}
131+
130132
table := tables.NewTable()
131133
table.SetHeader("ID")
132134
for i := range credentials {

internal/cmd/mariadb/credentials/list/list_test.go

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ import (
1616
"github.com/stackitcloud/stackit-sdk-go/services/mariadb"
1717
)
1818

19-
var projectIdFlag = globalflags.ProjectIdFlag
20-
2119
type testCtxKey struct{}
2220

2321
var testCtx = context.WithValue(context.Background(), testCtxKey{}, "foo")
@@ -27,9 +25,9 @@ var testInstanceId = uuid.NewString()
2725

2826
func fixtureFlagValues(mods ...func(flagValues map[string]string)) map[string]string {
2927
flagValues := map[string]string{
30-
projectIdFlag: testProjectId,
31-
instanceIdFlag: testInstanceId,
32-
limitFlag: "10",
28+
globalflags.ProjectIdFlag: testProjectId,
29+
instanceIdFlag: testInstanceId,
30+
limitFlag: "10",
3331
}
3432
for _, mod := range mods {
3533
mod(flagValues)
@@ -82,21 +80,21 @@ func TestParseInput(t *testing.T) {
8280
{
8381
description: "project id missing",
8482
flagValues: fixtureFlagValues(func(flagValues map[string]string) {
85-
delete(flagValues, projectIdFlag)
83+
delete(flagValues, globalflags.ProjectIdFlag)
8684
}),
8785
isValid: false,
8886
},
8987
{
9088
description: "project id invalid 1",
9189
flagValues: fixtureFlagValues(func(flagValues map[string]string) {
92-
flagValues[projectIdFlag] = ""
90+
flagValues[globalflags.ProjectIdFlag] = ""
9391
}),
9492
isValid: false,
9593
},
9694
{
9795
description: "project id invalid 2",
9896
flagValues: fixtureFlagValues(func(flagValues map[string]string) {
99-
flagValues[projectIdFlag] = "invalid-uuid"
97+
flagValues[globalflags.ProjectIdFlag] = "invalid-uuid"
10098
}),
10199
isValid: false,
102100
},
@@ -174,8 +172,9 @@ func TestBuildRequest(t *testing.T) {
174172

175173
func TestOutputResult(t *testing.T) {
176174
type args struct {
177-
outputFormat string
178-
credentials []mariadb.CredentialsListItem
175+
outputFormat string
176+
instanceLabel string
177+
credentials []mariadb.CredentialsListItem
179178
}
180179
tests := []struct {
181180
name string
@@ -207,7 +206,7 @@ func TestOutputResult(t *testing.T) {
207206
p.Cmd = NewCmd(&params.CmdParams{Printer: p})
208207
for _, tt := range tests {
209208
t.Run(tt.name, func(t *testing.T) {
210-
if err := outputResult(p, tt.args.outputFormat, tt.args.credentials); (err != nil) != tt.wantErr {
209+
if err := outputResult(p, tt.args.outputFormat, tt.args.instanceLabel, tt.args.credentials); (err != nil) != tt.wantErr {
211210
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
212211
}
213212
})

internal/cmd/mariadb/instance/list/list.go

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -64,23 +64,20 @@ func NewCmd(params *params.CmdParams) *cobra.Command {
6464
if err != nil {
6565
return fmt.Errorf("get MariaDB instances: %w", err)
6666
}
67-
instances := *resp.Instances
68-
if len(instances) == 0 {
69-
projectLabel, err := projectname.GetProjectName(ctx, params.Printer, params.CliVersion, cmd)
70-
if err != nil {
71-
params.Printer.Debug(print.ErrorLevel, "get project name: %v", err)
72-
projectLabel = model.ProjectId
73-
}
74-
params.Printer.Info("No instances found for project %q\n", projectLabel)
75-
return nil
67+
instances := resp.GetInstances()
68+
69+
projectLabel, err := projectname.GetProjectName(ctx, params.Printer, params.CliVersion, cmd)
70+
if err != nil {
71+
params.Printer.Debug(print.ErrorLevel, "get project name: %v", err)
72+
projectLabel = model.ProjectId
7673
}
7774

7875
// Truncate output
7976
if model.Limit != nil && len(instances) > int(*model.Limit) {
8077
instances = instances[:*model.Limit]
8178
}
8279

83-
return outputResult(params.Printer, model.OutputFormat, instances)
80+
return outputResult(params.Printer, model.OutputFormat, projectLabel, instances)
8481
},
8582
}
8683

@@ -120,8 +117,13 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *mariadb.API
120117
return req
121118
}
122119

123-
func outputResult(p *print.Printer, outputFormat string, instances []mariadb.Instance) error {
120+
func outputResult(p *print.Printer, outputFormat, projectLabel string, instances []mariadb.Instance) error {
124121
return p.OutputResult(outputFormat, instances, func() error {
122+
if len(instances) == 0 {
123+
p.Outputf("No instances found for project %q\n", projectLabel)
124+
return nil
125+
}
126+
125127
table := tables.NewTable()
126128
table.SetHeader("ID", "NAME", "LAST OPERATION TYPE", "LAST OPERATION STATE")
127129
for i := range instances {

internal/cmd/mariadb/instance/list/list_test.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ import (
1616
"github.com/stackitcloud/stackit-sdk-go/services/mariadb"
1717
)
1818

19-
var projectIdFlag = globalflags.ProjectIdFlag
20-
2119
type testCtxKey struct{}
2220

2321
var testCtx = context.WithValue(context.Background(), testCtxKey{}, "foo")
@@ -26,8 +24,8 @@ var testProjectId = uuid.NewString()
2624

2725
func fixtureFlagValues(mods ...func(flagValues map[string]string)) map[string]string {
2826
flagValues := map[string]string{
29-
projectIdFlag: testProjectId,
30-
limitFlag: "10",
27+
globalflags.ProjectIdFlag: testProjectId,
28+
limitFlag: "10",
3129
}
3230
for _, mod := range mods {
3331
mod(flagValues)
@@ -79,21 +77,21 @@ func TestParseInput(t *testing.T) {
7977
{
8078
description: "project id missing",
8179
flagValues: fixtureFlagValues(func(flagValues map[string]string) {
82-
delete(flagValues, projectIdFlag)
80+
delete(flagValues, globalflags.ProjectIdFlag)
8381
}),
8482
isValid: false,
8583
},
8684
{
8785
description: "project id invalid 1",
8886
flagValues: fixtureFlagValues(func(flagValues map[string]string) {
89-
flagValues[projectIdFlag] = ""
87+
flagValues[globalflags.ProjectIdFlag] = ""
9088
}),
9189
isValid: false,
9290
},
9391
{
9492
description: "project id invalid 2",
9593
flagValues: fixtureFlagValues(func(flagValues map[string]string) {
96-
flagValues[projectIdFlag] = "invalid-uuid"
94+
flagValues[globalflags.ProjectIdFlag] = "invalid-uuid"
9795
}),
9896
isValid: false,
9997
},
@@ -151,6 +149,7 @@ func TestBuildRequest(t *testing.T) {
151149
func TestOutputResult(t *testing.T) {
152150
type args struct {
153151
outputFormat string
152+
projectLabel string
154153
instances []mariadb.Instance
155154
}
156155
tests := []struct {
@@ -183,7 +182,7 @@ func TestOutputResult(t *testing.T) {
183182
p.Cmd = NewCmd(&params.CmdParams{Printer: p})
184183
for _, tt := range tests {
185184
t.Run(tt.name, func(t *testing.T) {
186-
if err := outputResult(p, tt.args.outputFormat, tt.args.instances); (err != nil) != tt.wantErr {
185+
if err := outputResult(p, tt.args.outputFormat, tt.args.projectLabel, tt.args.instances); (err != nil) != tt.wantErr {
187186
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
188187
}
189188
})

internal/cmd/mariadb/plans/plans.go

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -64,23 +64,20 @@ func NewCmd(params *params.CmdParams) *cobra.Command {
6464
if err != nil {
6565
return fmt.Errorf("get MariaDB service plans: %w", err)
6666
}
67-
plans := *resp.Offerings
68-
if len(plans) == 0 {
69-
projectLabel, err := projectname.GetProjectName(ctx, params.Printer, params.CliVersion, cmd)
70-
if err != nil {
71-
params.Printer.Debug(print.ErrorLevel, "get project name: %v", err)
72-
projectLabel = model.ProjectId
73-
}
74-
params.Printer.Info("No plans found for project %q\n", projectLabel)
75-
return nil
67+
plans := resp.GetOfferings()
68+
69+
projectLabel, err := projectname.GetProjectName(ctx, params.Printer, params.CliVersion, cmd)
70+
if err != nil {
71+
params.Printer.Debug(print.ErrorLevel, "get project name: %v", err)
72+
projectLabel = model.ProjectId
7673
}
7774

7875
// Truncate output
7976
if model.Limit != nil && len(plans) > int(*model.Limit) {
8077
plans = plans[:*model.Limit]
8178
}
8279

83-
return outputResult(params.Printer, model.OutputFormat, plans)
80+
return outputResult(params.Printer, model.OutputFormat, projectLabel, plans)
8481
},
8582
}
8683

@@ -120,8 +117,13 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *mariadb.API
120117
return req
121118
}
122119

123-
func outputResult(p *print.Printer, outputFormat string, plans []mariadb.Offering) error {
120+
func outputResult(p *print.Printer, outputFormat, projectLabel string, plans []mariadb.Offering) error {
124121
return p.OutputResult(outputFormat, plans, func() error {
122+
if len(plans) == 0 {
123+
p.Outputf("No plans found for project %q\n", projectLabel)
124+
return nil
125+
}
126+
125127
table := tables.NewTable()
126128
table.SetHeader("OFFERING NAME", "VERSION", "ID", "NAME", "DESCRIPTION")
127129
for i := range plans {

internal/cmd/mariadb/plans/plans_test.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ import (
1616
"github.com/stackitcloud/stackit-sdk-go/services/mariadb"
1717
)
1818

19-
var projectIdFlag = globalflags.ProjectIdFlag
20-
2119
type testCtxKey struct{}
2220

2321
var testCtx = context.WithValue(context.Background(), testCtxKey{}, "foo")
@@ -26,8 +24,8 @@ var testProjectId = uuid.NewString()
2624

2725
func fixtureFlagValues(mods ...func(flagValues map[string]string)) map[string]string {
2826
flagValues := map[string]string{
29-
projectIdFlag: testProjectId,
30-
limitFlag: "10",
27+
globalflags.ProjectIdFlag: testProjectId,
28+
limitFlag: "10",
3129
}
3230
for _, mod := range mods {
3331
mod(flagValues)
@@ -79,21 +77,21 @@ func TestParseInput(t *testing.T) {
7977
{
8078
description: "project id missing",
8179
flagValues: fixtureFlagValues(func(flagValues map[string]string) {
82-
delete(flagValues, projectIdFlag)
80+
delete(flagValues, globalflags.ProjectIdFlag)
8381
}),
8482
isValid: false,
8583
},
8684
{
8785
description: "project id invalid 1",
8886
flagValues: fixtureFlagValues(func(flagValues map[string]string) {
89-
flagValues[projectIdFlag] = ""
87+
flagValues[globalflags.ProjectIdFlag] = ""
9088
}),
9189
isValid: false,
9290
},
9391
{
9492
description: "project id invalid 2",
9593
flagValues: fixtureFlagValues(func(flagValues map[string]string) {
96-
flagValues[projectIdFlag] = "invalid-uuid"
94+
flagValues[globalflags.ProjectIdFlag] = "invalid-uuid"
9795
}),
9896
isValid: false,
9997
},
@@ -151,6 +149,7 @@ func TestBuildRequest(t *testing.T) {
151149
func TestOutputResult(t *testing.T) {
152150
type args struct {
153151
outputFormat string
152+
projectLabel string
154153
plans []mariadb.Offering
155154
}
156155
tests := []struct {
@@ -183,7 +182,7 @@ func TestOutputResult(t *testing.T) {
183182
p.Cmd = NewCmd(&params.CmdParams{Printer: p})
184183
for _, tt := range tests {
185184
t.Run(tt.name, func(t *testing.T) {
186-
if err := outputResult(p, tt.args.outputFormat, tt.args.plans); (err != nil) != tt.wantErr {
185+
if err := outputResult(p, tt.args.outputFormat, tt.args.projectLabel, tt.args.plans); (err != nil) != tt.wantErr {
187186
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
188187
}
189188
})

0 commit comments

Comments
 (0)