Skip to content

Commit f8bb8e9

Browse files
authored
fix(mariadb): add nil pointer checks for cmd outputs (#628)
relates to STACKITCLI-103
1 parent bdf28fc commit f8bb8e9

File tree

13 files changed

+331
-32
lines changed

13 files changed

+331
-32
lines changed

internal/cmd/mariadb/credentials/create/create.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ func NewCmd(p *print.Printer) *cobra.Command {
7979
return fmt.Errorf("create MariaDB credentials: %w", err)
8080
}
8181

82-
return outputResult(p, model, instanceLabel, resp)
82+
return outputResult(p, model.OutputFormat, model.ShowPassword, instanceLabel, resp)
8383
},
8484
}
8585
configureFlags(cmd)
@@ -123,11 +123,15 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *mariadb.API
123123
return req
124124
}
125125

126-
func outputResult(p *print.Printer, model *inputModel, instanceLabel string, resp *mariadb.CredentialsResponse) error {
127-
if !model.ShowPassword {
126+
func outputResult(p *print.Printer, outputFormat string, showPassword bool, instanceLabel string, resp *mariadb.CredentialsResponse) error {
127+
if resp == nil {
128+
return fmt.Errorf("response is nil")
129+
}
130+
131+
if !showPassword && resp.HasRaw() && resp.Raw.Credentials != nil {
128132
resp.Raw.Credentials.Password = utils.Ptr("hidden")
129133
}
130-
switch model.OutputFormat {
134+
switch outputFormat {
131135
case print.JSONOutputFormat:
132136
details, err := json.MarshalIndent(resp, "", " ")
133137
if err != nil {
@@ -151,7 +155,7 @@ func outputResult(p *print.Printer, model *inputModel, instanceLabel string, res
151155
if username := resp.Raw.Credentials.Username; username != nil && *username != "" {
152156
p.Outputf("Username: %s\n", *username)
153157
}
154-
if !model.ShowPassword {
158+
if !showPassword {
155159
p.Outputf("Password: <hidden>\n")
156160
} else {
157161
p.Outputf("Password: %s\n", utils.PtrString(resp.Raw.Credentials.Password))

internal/cmd/mariadb/credentials/create/create_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,3 +200,40 @@ func TestBuildRequest(t *testing.T) {
200200
})
201201
}
202202
}
203+
204+
func TestOutputResult(t *testing.T) {
205+
type args struct {
206+
outputFormat string
207+
showPassword bool
208+
instanceLabel string
209+
credentials *mariadb.CredentialsResponse
210+
}
211+
tests := []struct {
212+
name string
213+
args args
214+
wantErr bool
215+
}{
216+
{
217+
name: "empty",
218+
args: args{},
219+
wantErr: true,
220+
},
221+
{
222+
name: "set empty credentials",
223+
args: args{
224+
credentials: &mariadb.CredentialsResponse{},
225+
},
226+
wantErr: false,
227+
},
228+
}
229+
230+
p := print.NewPrinter()
231+
p.Cmd = NewCmd(p)
232+
for _, tt := range tests {
233+
t.Run(tt.name, func(t *testing.T) {
234+
if err := outputResult(p, tt.args.outputFormat, tt.args.showPassword, tt.args.instanceLabel, tt.args.credentials); (err != nil) != tt.wantErr {
235+
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
236+
}
237+
})
238+
}
239+
}

internal/cmd/mariadb/credentials/describe/describe.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,10 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *mariadb.API
112112
}
113113

114114
func outputResult(p *print.Printer, outputFormat string, credentials *mariadb.CredentialsResponse) error {
115+
if credentials == nil {
116+
return fmt.Errorf("credentials is nil")
117+
}
118+
115119
switch outputFormat {
116120
case print.JSONOutputFormat:
117121
details, err := json.MarshalIndent(credentials, "", " ")
@@ -131,7 +135,7 @@ func outputResult(p *print.Printer, outputFormat string, credentials *mariadb.Cr
131135
return nil
132136
default:
133137
table := tables.NewTable()
134-
table.AddRow("ID", *credentials.Id)
138+
table.AddRow("ID", utils.PtrString(credentials.Id))
135139
table.AddSeparator()
136140
// The username field cannot be set by the user so we only display it if it's not returned empty
137141
if credentials.HasRaw() && credentials.Raw.Credentials != nil {

internal/cmd/mariadb/credentials/describe/describe_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,3 +243,38 @@ func TestBuildRequest(t *testing.T) {
243243
})
244244
}
245245
}
246+
247+
func TestOutputResult(t *testing.T) {
248+
type args struct {
249+
outputFormat string
250+
credentials *mariadb.CredentialsResponse
251+
}
252+
tests := []struct {
253+
name string
254+
args args
255+
wantErr bool
256+
}{
257+
{
258+
name: "empty",
259+
args: args{},
260+
wantErr: true,
261+
},
262+
{
263+
name: "set empty credentials",
264+
args: args{
265+
credentials: &mariadb.CredentialsResponse{},
266+
},
267+
wantErr: false,
268+
},
269+
}
270+
271+
p := print.NewPrinter()
272+
p.Cmd = NewCmd(p)
273+
for _, tt := range tests {
274+
t.Run(tt.name, func(t *testing.T) {
275+
if err := outputResult(p, tt.args.outputFormat, tt.args.credentials); (err != nil) != tt.wantErr {
276+
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
277+
}
278+
})
279+
}
280+
}

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

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,3 +207,45 @@ func TestBuildRequest(t *testing.T) {
207207
})
208208
}
209209
}
210+
211+
func TestOutputResult(t *testing.T) {
212+
type args struct {
213+
outputFormat string
214+
credentials []mariadb.CredentialsListItem
215+
}
216+
tests := []struct {
217+
name string
218+
args args
219+
wantErr bool
220+
}{
221+
{
222+
name: "empty",
223+
args: args{},
224+
wantErr: false,
225+
},
226+
{
227+
name: "set empty credentials slice",
228+
args: args{
229+
credentials: []mariadb.CredentialsListItem{},
230+
},
231+
wantErr: false,
232+
},
233+
{
234+
name: "set empty credential in credentials slice",
235+
args: args{
236+
credentials: []mariadb.CredentialsListItem{{}},
237+
},
238+
wantErr: false,
239+
},
240+
}
241+
242+
p := print.NewPrinter()
243+
p.Cmd = NewCmd(p)
244+
for _, tt := range tests {
245+
t.Run(tt.name, func(t *testing.T) {
246+
if err := outputResult(p, tt.args.outputFormat, tt.args.credentials); (err != nil) != tt.wantErr {
247+
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
248+
}
249+
})
250+
}
251+
}

internal/cmd/mariadb/instance/create/create.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ func NewCmd(p *print.Printer) *cobra.Command {
125125
s.Stop()
126126
}
127127

128-
return outputResult(p, model, projectLabel, resp)
128+
return outputResult(p, model.OutputFormat, model.Async, projectLabel, resp)
129129
},
130130
}
131131
configureFlags(cmd)
@@ -251,8 +251,12 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient mariaDBClien
251251
return req, nil
252252
}
253253

254-
func outputResult(p *print.Printer, model *inputModel, projectLabel string, resp *mariadb.CreateInstanceResponse) error {
255-
switch model.OutputFormat {
254+
func outputResult(p *print.Printer, outputFormat string, async bool, projectLabel string, resp *mariadb.CreateInstanceResponse) error {
255+
if resp == nil {
256+
return fmt.Errorf("response is nil")
257+
}
258+
259+
switch outputFormat {
256260
case print.JSONOutputFormat:
257261
details, err := json.MarshalIndent(resp, "", " ")
258262
if err != nil {
@@ -271,7 +275,7 @@ func outputResult(p *print.Printer, model *inputModel, projectLabel string, resp
271275
return nil
272276
default:
273277
operationState := "Created"
274-
if model.Async {
278+
if async {
275279
operationState = "Triggered creation of"
276280
}
277281
p.Outputf("%s instance for project %q. Instance ID: %s\n", operationState, projectLabel, utils.PtrString(resp.InstanceId))

internal/cmd/mariadb/instance/create/create_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,3 +463,40 @@ func TestBuildRequest(t *testing.T) {
463463
})
464464
}
465465
}
466+
467+
func TestOutputResult(t *testing.T) {
468+
type args struct {
469+
outputFormat string
470+
async bool
471+
projectLabel string
472+
resp *mariadb.CreateInstanceResponse
473+
}
474+
tests := []struct {
475+
name string
476+
args args
477+
wantErr bool
478+
}{
479+
{
480+
name: "empty",
481+
args: args{},
482+
wantErr: true,
483+
},
484+
{
485+
name: "set empty response",
486+
args: args{
487+
resp: &mariadb.CreateInstanceResponse{},
488+
},
489+
wantErr: false,
490+
},
491+
}
492+
493+
p := print.NewPrinter()
494+
p.Cmd = NewCmd(p)
495+
for _, tt := range tests {
496+
t.Run(tt.name, func(t *testing.T) {
497+
if err := outputResult(p, tt.args.outputFormat, tt.args.async, tt.args.projectLabel, tt.args.resp); (err != nil) != tt.wantErr {
498+
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
499+
}
500+
})
501+
}
502+
}

internal/cmd/mariadb/instance/describe/describe.go

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,10 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *mariadb.API
100100
}
101101

102102
func outputResult(p *print.Printer, outputFormat string, instance *mariadb.Instance) error {
103+
if instance == nil {
104+
return fmt.Errorf("instance is nil")
105+
}
106+
103107
switch outputFormat {
104108
case print.JSONOutputFormat:
105109
details, err := json.MarshalIndent(instance, "", " ")
@@ -123,18 +127,22 @@ func outputResult(p *print.Printer, outputFormat string, instance *mariadb.Insta
123127
table.AddSeparator()
124128
table.AddRow("NAME", utils.PtrString(instance.Name))
125129
table.AddSeparator()
126-
table.AddRow("LAST OPERATION TYPE", utils.PtrString(instance.LastOperation.Type))
127-
table.AddSeparator()
128-
table.AddRow("LAST OPERATION STATE", utils.PtrString(instance.LastOperation.State))
129-
table.AddSeparator()
130+
if instance.LastOperation != nil {
131+
table.AddRow("LAST OPERATION TYPE", utils.PtrString(instance.LastOperation.Type))
132+
table.AddSeparator()
133+
table.AddRow("LAST OPERATION STATE", utils.PtrString(instance.LastOperation.State))
134+
table.AddSeparator()
135+
}
130136
table.AddRow("PLAN ID", utils.PtrString(instance.PlanId))
131137
// Only show ACL if it's present and not empty
132-
acl := (*instance.Parameters)[aclParameterKey]
133-
aclStr, ok := acl.(string)
134-
if ok {
135-
if aclStr != "" {
136-
table.AddSeparator()
137-
table.AddRow("ACL", aclStr)
138+
if instance.Parameters != nil {
139+
acl := (*instance.Parameters)[aclParameterKey]
140+
aclStr, ok := acl.(string)
141+
if ok {
142+
if aclStr != "" {
143+
table.AddSeparator()
144+
table.AddRow("ACL", aclStr)
145+
}
138146
}
139147
}
140148
err := table.Display(p)

internal/cmd/mariadb/instance/describe/describe_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,3 +216,38 @@ func TestBuildRequest(t *testing.T) {
216216
})
217217
}
218218
}
219+
220+
func TestOutputResult(t *testing.T) {
221+
type args struct {
222+
outputFormat string
223+
instance *mariadb.Instance
224+
}
225+
tests := []struct {
226+
name string
227+
args args
228+
wantErr bool
229+
}{
230+
{
231+
name: "empty",
232+
args: args{},
233+
wantErr: true,
234+
},
235+
{
236+
name: "set empty instance",
237+
args: args{
238+
instance: &mariadb.Instance{},
239+
},
240+
wantErr: false,
241+
},
242+
}
243+
244+
p := print.NewPrinter()
245+
p.Cmd = NewCmd(p)
246+
for _, tt := range tests {
247+
t.Run(tt.name, func(t *testing.T) {
248+
if err := outputResult(p, tt.args.outputFormat, tt.args.instance); (err != nil) != tt.wantErr {
249+
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
250+
}
251+
})
252+
}
253+
}

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,11 +152,18 @@ func outputResult(p *print.Printer, outputFormat string, instances []mariadb.Ins
152152
table.SetHeader("ID", "NAME", "LAST OPERATION TYPE", "LAST OPERATION STATE")
153153
for i := range instances {
154154
instance := instances[i]
155+
156+
lastOperationType, lastOperationState := "", ""
157+
if instance.LastOperation != nil {
158+
lastOperationType = utils.PtrString(instance.LastOperation.Type)
159+
lastOperationState = utils.PtrString(instance.LastOperation.State)
160+
}
161+
155162
table.AddRow(
156163
utils.PtrString(instance.InstanceId),
157164
utils.PtrString(instance.Name),
158-
utils.PtrString(instance.LastOperation.Type),
159-
utils.PtrString(instance.LastOperation.State),
165+
lastOperationType,
166+
lastOperationState,
160167
)
161168
}
162169
err := table.Display(p)

0 commit comments

Comments
 (0)