Skip to content

Commit 84d7fb6

Browse files
authored
fix(organization): add nil pointer checks for cmd outputs (#615)
relates to STACKITCLI-108
1 parent 317ce8f commit 84d7fb6

File tree

5 files changed

+103
-21
lines changed

5 files changed

+103
-21
lines changed

internal/cmd/organization/member/add/add.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,14 @@ import (
44
"context"
55
"fmt"
66

7+
"github.com/spf13/cobra"
78
"github.com/stackitcloud/stackit-cli/internal/pkg/args"
89
"github.com/stackitcloud/stackit-cli/internal/pkg/examples"
910
"github.com/stackitcloud/stackit-cli/internal/pkg/flags"
1011
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
1112
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
1213
"github.com/stackitcloud/stackit-cli/internal/pkg/services/authorization/client"
1314
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
14-
15-
"github.com/spf13/cobra"
1615
"github.com/stackitcloud/stackit-sdk-go/services/authorization"
1716
)
1817

internal/cmd/organization/member/list/list.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"sort"
88

99
"github.com/goccy/go-yaml"
10+
"github.com/spf13/cobra"
1011
"github.com/stackitcloud/stackit-cli/internal/pkg/args"
1112
"github.com/stackitcloud/stackit-cli/internal/pkg/errors"
1213
"github.com/stackitcloud/stackit-cli/internal/pkg/examples"
@@ -16,8 +17,6 @@ import (
1617
"github.com/stackitcloud/stackit-cli/internal/pkg/services/authorization/client"
1718
"github.com/stackitcloud/stackit-cli/internal/pkg/tables"
1819
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
19-
20-
"github.com/spf13/cobra"
2120
"github.com/stackitcloud/stackit-sdk-go/services/authorization"
2221
)
2322

@@ -86,7 +85,7 @@ func NewCmd(p *print.Printer) *cobra.Command {
8685
members = members[:*model.Limit]
8786
}
8887

89-
return outputResult(p, model, members)
88+
return outputResult(p, model.OutputFormat, model.SortBy, members)
9089
},
9190
}
9291
configureFlags(cmd)
@@ -144,9 +143,9 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *authorizati
144143
return req
145144
}
146145

147-
func outputResult(p *print.Printer, model *inputModel, members []authorization.Member) error {
146+
func outputResult(p *print.Printer, outputFormat, sortBy string, members []authorization.Member) error {
148147
sortFn := func(i, j int) bool {
149-
switch model.SortBy {
148+
switch sortBy {
150149
case "subject":
151150
return *members[i].Subject < *members[j].Subject
152151
case "role":
@@ -157,7 +156,7 @@ func outputResult(p *print.Printer, model *inputModel, members []authorization.M
157156
}
158157
sort.SliceStable(members, sortFn)
159158

160-
switch model.OutputFormat {
159+
switch outputFormat {
161160
case print.JSONOutputFormat:
162161
// Show details
163162
details, err := json.MarshalIndent(members, "", " ")
@@ -187,9 +186,9 @@ func outputResult(p *print.Printer, model *inputModel, members []authorization.M
187186
table.AddRow(utils.PtrString(m.Subject), utils.PtrString(m.Role))
188187
}
189188

190-
if model.SortBy == "subject" {
189+
if sortBy == "subject" {
191190
table.EnableAutoMergeOnColumns(1)
192-
} else if model.SortBy == "role" {
191+
} else if sortBy == "role" {
193192
table.EnableAutoMergeOnColumns(2)
194193
}
195194

internal/cmd/organization/member/list/list_test.go

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

internal/cmd/organization/role/list/list.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"fmt"
77

88
"github.com/goccy/go-yaml"
9+
"github.com/spf13/cobra"
910
"github.com/stackitcloud/stackit-cli/internal/pkg/args"
1011
"github.com/stackitcloud/stackit-cli/internal/pkg/errors"
1112
"github.com/stackitcloud/stackit-cli/internal/pkg/examples"
@@ -15,8 +16,6 @@ import (
1516
"github.com/stackitcloud/stackit-cli/internal/pkg/services/authorization/client"
1617
"github.com/stackitcloud/stackit-cli/internal/pkg/tables"
1718
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
18-
19-
"github.com/spf13/cobra"
2019
"github.com/stackitcloud/stackit-sdk-go/services/authorization"
2120
)
2221

@@ -153,16 +152,18 @@ func outputRolesResult(p *print.Printer, outputFormat string, roles []authorizat
153152
table.SetHeader("ROLE NAME", "ROLE DESCRIPTION", "PERMISSION NAME", "PERMISSION DESCRIPTION")
154153
for i := range roles {
155154
r := roles[i]
156-
for j := range *r.Permissions {
157-
p := (*r.Permissions)[j]
158-
table.AddRow(
159-
utils.PtrString(r.Name),
160-
utils.PtrString(r.Description),
161-
utils.PtrString(p.Name),
162-
utils.PtrString(p.Description),
163-
)
155+
if r.Permissions != nil {
156+
for j := range *r.Permissions {
157+
p := (*r.Permissions)[j]
158+
table.AddRow(
159+
utils.PtrString(r.Name),
160+
utils.PtrString(r.Description),
161+
utils.PtrString(p.Name),
162+
utils.PtrString(p.Description),
163+
)
164+
}
165+
table.AddSeparator()
164166
}
165-
table.AddSeparator()
166167
}
167168
table.EnableAutoMergeOnColumns(1, 2)
168169
err := table.Display(p)

internal/cmd/organization/role/list/list_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,3 +167,44 @@ func TestBuildRequest(t *testing.T) {
167167
})
168168
}
169169
}
170+
171+
func TestOutputResult(t *testing.T) {
172+
type args struct {
173+
outputFormat string
174+
roles []authorization.Role
175+
}
176+
tests := []struct {
177+
name string
178+
args args
179+
wantErr bool
180+
}{
181+
{
182+
name: "empty",
183+
args: args{},
184+
wantErr: false,
185+
},
186+
{
187+
name: "set empty roles slice",
188+
args: args{
189+
roles: []authorization.Role{},
190+
},
191+
wantErr: false,
192+
},
193+
{
194+
name: "set empty role in roles slice",
195+
args: args{
196+
roles: []authorization.Role{{}},
197+
},
198+
wantErr: false,
199+
},
200+
}
201+
p := print.NewPrinter()
202+
p.Cmd = NewCmd(p)
203+
for _, tt := range tests {
204+
t.Run(tt.name, func(t *testing.T) {
205+
if err := outputRolesResult(p, tt.args.outputFormat, tt.args.roles); (err != nil) != tt.wantErr {
206+
t.Errorf("outputRolesResult() error = %v, wantErr %v", err, tt.wantErr)
207+
}
208+
})
209+
}
210+
}

0 commit comments

Comments
 (0)