Skip to content

Commit 908703a

Browse files
author
Ruben Hönle
authored
fix(object-storage): add nil pointer checks for cmd outputs (#612)
relates to STACKITCLI-105
1 parent e12c8c2 commit 908703a

File tree

12 files changed

+295
-11
lines changed

12 files changed

+295
-11
lines changed

internal/cmd/object-storage/bucket/create/create.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ func NewCmd(p *print.Printer) *cobra.Command {
9090
s.Stop()
9191
}
9292

93-
return outputResult(p, model, resp)
93+
return outputResult(p, model.OutputFormat, model.Async, model.BucketName, resp)
9494
},
9595
}
9696
return cmd
@@ -126,8 +126,12 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *objectstora
126126
return req
127127
}
128128

129-
func outputResult(p *print.Printer, model *inputModel, resp *objectstorage.CreateBucketResponse) error {
130-
switch model.OutputFormat {
129+
func outputResult(p *print.Printer, outputFormat string, async bool, bucketName string, resp *objectstorage.CreateBucketResponse) error {
130+
if resp == nil {
131+
return fmt.Errorf("create bucket response is empty")
132+
}
133+
134+
switch outputFormat {
131135
case print.JSONOutputFormat:
132136
details, err := json.MarshalIndent(resp, "", " ")
133137
if err != nil {
@@ -146,10 +150,10 @@ func outputResult(p *print.Printer, model *inputModel, resp *objectstorage.Creat
146150
return nil
147151
default:
148152
operationState := "Created"
149-
if model.Async {
153+
if async {
150154
operationState = "Triggered creation of"
151155
}
152-
p.Outputf("%s bucket %q\n", operationState, model.BucketName)
156+
p.Outputf("%s bucket %q\n", operationState, bucketName)
153157
return nil
154158
}
155159
}

internal/cmd/object-storage/bucket/create/create_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,3 +214,39 @@ func TestBuildRequest(t *testing.T) {
214214
})
215215
}
216216
}
217+
218+
func TestOutputResult(t *testing.T) {
219+
type args struct {
220+
outputFormat string
221+
async bool
222+
bucketName string
223+
createBucketResponse *objectstorage.CreateBucketResponse
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 create bucket response",
237+
args: args{
238+
createBucketResponse: &objectstorage.CreateBucketResponse{},
239+
},
240+
wantErr: false,
241+
},
242+
}
243+
p := print.NewPrinter()
244+
p.Cmd = NewCmd(p)
245+
for _, tt := range tests {
246+
t.Run(tt.name, func(t *testing.T) {
247+
if err := outputResult(p, tt.args.outputFormat, tt.args.async, tt.args.bucketName, tt.args.createBucketResponse); (err != nil) != tt.wantErr {
248+
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
249+
}
250+
})
251+
}
252+
}

internal/cmd/object-storage/bucket/describe/describe.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,10 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *objectstora
9898
}
9999

100100
func outputResult(p *print.Printer, outputFormat string, bucket *objectstorage.Bucket) error {
101+
if bucket == nil {
102+
return fmt.Errorf("bucket is empty")
103+
}
104+
101105
switch outputFormat {
102106
case print.JSONOutputFormat:
103107
details, err := json.MarshalIndent(bucket, "", " ")

internal/cmd/object-storage/bucket/describe/describe_test.go

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

internal/cmd/object-storage/bucket/list/list.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,10 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *objectstora
130130
}
131131

132132
func outputResult(p *print.Printer, outputFormat string, buckets []objectstorage.Bucket) error {
133+
if buckets == nil {
134+
return fmt.Errorf("buckets is empty")
135+
}
136+
133137
switch outputFormat {
134138
case print.JSONOutputFormat:
135139
details, err := json.MarshalIndent(buckets, "", " ")

internal/cmd/object-storage/bucket/list/list_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,3 +190,37 @@ func TestBuildRequest(t *testing.T) {
190190
})
191191
}
192192
}
193+
194+
func TestOutputResult(t *testing.T) {
195+
type args struct {
196+
outputFormat string
197+
buckets []objectstorage.Bucket
198+
}
199+
tests := []struct {
200+
name string
201+
args args
202+
wantErr bool
203+
}{
204+
{
205+
name: "empty",
206+
args: args{},
207+
wantErr: true,
208+
},
209+
{
210+
name: "set empty create bucket response",
211+
args: args{
212+
buckets: []objectstorage.Bucket{},
213+
},
214+
wantErr: false,
215+
},
216+
}
217+
p := print.NewPrinter()
218+
p.Cmd = NewCmd(p)
219+
for _, tt := range tests {
220+
t.Run(tt.name, func(t *testing.T) {
221+
if err := outputResult(p, tt.args.outputFormat, tt.args.buckets); (err != nil) != tt.wantErr {
222+
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
223+
}
224+
})
225+
}
226+
}

internal/cmd/object-storage/credentials-group/create/create.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func NewCmd(p *print.Printer) *cobra.Command {
6767
return fmt.Errorf("create Object Storage credentials group: %w", err)
6868
}
6969

70-
return outputResult(p, model, resp)
70+
return outputResult(p, model.OutputFormat, resp)
7171
},
7272
}
7373
configureFlags(cmd)
@@ -112,8 +112,12 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *objectstora
112112
return req
113113
}
114114

115-
func outputResult(p *print.Printer, model *inputModel, resp *objectstorage.CreateCredentialsGroupResponse) error {
116-
switch model.OutputFormat {
115+
func outputResult(p *print.Printer, outputFormat string, resp *objectstorage.CreateCredentialsGroupResponse) error {
116+
if resp == nil || resp.CredentialsGroup == nil {
117+
return fmt.Errorf("create createndials group response is empty")
118+
}
119+
120+
switch outputFormat {
117121
case print.JSONOutputFormat:
118122
details, err := json.MarshalIndent(resp, "", " ")
119123
if err != nil {

internal/cmd/object-storage/credentials-group/create/create_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,3 +192,46 @@ func TestBuildRequest(t *testing.T) {
192192
})
193193
}
194194
}
195+
196+
func TestOutputResult(t *testing.T) {
197+
type args struct {
198+
outputFormat string
199+
createCredentialsGroupResponse *objectstorage.CreateCredentialsGroupResponse
200+
}
201+
tests := []struct {
202+
name string
203+
args args
204+
wantErr bool
205+
}{
206+
{
207+
name: "empty",
208+
args: args{},
209+
wantErr: true,
210+
},
211+
{
212+
name: "set empty create credentials group response",
213+
args: args{
214+
createCredentialsGroupResponse: &objectstorage.CreateCredentialsGroupResponse{},
215+
},
216+
wantErr: true,
217+
},
218+
{
219+
name: "set create credentials group response",
220+
args: args{
221+
createCredentialsGroupResponse: &objectstorage.CreateCredentialsGroupResponse{
222+
CredentialsGroup: &objectstorage.CredentialsGroup{},
223+
},
224+
},
225+
wantErr: false,
226+
},
227+
}
228+
p := print.NewPrinter()
229+
p.Cmd = NewCmd(p)
230+
for _, tt := range tests {
231+
t.Run(tt.name, func(t *testing.T) {
232+
if err := outputResult(p, tt.args.outputFormat, tt.args.createCredentialsGroupResponse); (err != nil) != tt.wantErr {
233+
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
234+
}
235+
})
236+
}
237+
}

internal/cmd/object-storage/credentials-group/list/list_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,3 +187,44 @@ func TestBuildRequest(t *testing.T) {
187187
})
188188
}
189189
}
190+
191+
func TestOutputResult(t *testing.T) {
192+
type args struct {
193+
outputFormat string
194+
credentialsGroups []objectstorage.CredentialsGroup
195+
}
196+
tests := []struct {
197+
name string
198+
args args
199+
wantErr bool
200+
}{
201+
{
202+
name: "empty",
203+
args: args{},
204+
wantErr: false,
205+
},
206+
{
207+
name: "set empty credentials groups",
208+
args: args{
209+
credentialsGroups: []objectstorage.CredentialsGroup{},
210+
},
211+
wantErr: false,
212+
},
213+
{
214+
name: "set empty credentials group",
215+
args: args{
216+
credentialsGroups: []objectstorage.CredentialsGroup{{}},
217+
},
218+
wantErr: false,
219+
},
220+
}
221+
p := print.NewPrinter()
222+
p.Cmd = NewCmd(p)
223+
for _, tt := range tests {
224+
t.Run(tt.name, func(t *testing.T) {
225+
if err := outputResult(p, tt.args.outputFormat, tt.args.credentialsGroups); (err != nil) != tt.wantErr {
226+
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
227+
}
228+
})
229+
}
230+
}

internal/cmd/object-storage/credentials/create/create.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ func NewCmd(p *print.Printer) *cobra.Command {
8181
return fmt.Errorf("create Object Storage credentials: %w", err)
8282
}
8383

84-
return outputResult(p, model, credentialsGroupLabel, resp)
84+
return outputResult(p, model.OutputFormat, credentialsGroupLabel, resp)
8585
},
8686
}
8787
configureFlags(cmd)
@@ -137,8 +137,12 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *objectstora
137137
return req
138138
}
139139

140-
func outputResult(p *print.Printer, model *inputModel, credentialsGroupLabel string, resp *objectstorage.CreateAccessKeyResponse) error {
141-
switch model.OutputFormat {
140+
func outputResult(p *print.Printer, outputFormat, credentialsGroupLabel string, resp *objectstorage.CreateAccessKeyResponse) error {
141+
if resp == nil {
142+
return fmt.Errorf("create access key response is empty")
143+
}
144+
145+
switch outputFormat {
142146
case print.JSONOutputFormat:
143147
details, err := json.MarshalIndent(resp, "", " ")
144148
if err != nil {

0 commit comments

Comments
 (0)