Skip to content

fix(organization): add nil pointer checks for cmd outputs #615

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions internal/cmd/organization/member/add/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@ import (
"context"
"fmt"

"github.com/spf13/cobra"
"github.com/stackitcloud/stackit-cli/internal/pkg/args"
"github.com/stackitcloud/stackit-cli/internal/pkg/examples"
"github.com/stackitcloud/stackit-cli/internal/pkg/flags"
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
"github.com/stackitcloud/stackit-cli/internal/pkg/services/authorization/client"
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"

"github.com/spf13/cobra"
"github.com/stackitcloud/stackit-sdk-go/services/authorization"
)

Expand Down
15 changes: 7 additions & 8 deletions internal/cmd/organization/member/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"sort"

"github.com/goccy/go-yaml"
"github.com/spf13/cobra"
"github.com/stackitcloud/stackit-cli/internal/pkg/args"
"github.com/stackitcloud/stackit-cli/internal/pkg/errors"
"github.com/stackitcloud/stackit-cli/internal/pkg/examples"
Expand All @@ -16,8 +17,6 @@ import (
"github.com/stackitcloud/stackit-cli/internal/pkg/services/authorization/client"
"github.com/stackitcloud/stackit-cli/internal/pkg/tables"
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"

"github.com/spf13/cobra"
"github.com/stackitcloud/stackit-sdk-go/services/authorization"
)

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

return outputResult(p, model, members)
return outputResult(p, model.OutputFormat, model.SortBy, members)
},
}
configureFlags(cmd)
Expand Down Expand Up @@ -144,9 +143,9 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *authorizati
return req
}

func outputResult(p *print.Printer, model *inputModel, members []authorization.Member) error {
func outputResult(p *print.Printer, outputFormat, sortBy string, members []authorization.Member) error {
sortFn := func(i, j int) bool {
switch model.SortBy {
switch sortBy {
case "subject":
return *members[i].Subject < *members[j].Subject
case "role":
Expand All @@ -157,7 +156,7 @@ func outputResult(p *print.Printer, model *inputModel, members []authorization.M
}
sort.SliceStable(members, sortFn)

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

if model.SortBy == "subject" {
if sortBy == "subject" {
table.EnableAutoMergeOnColumns(1)
} else if model.SortBy == "role" {
} else if sortBy == "role" {
table.EnableAutoMergeOnColumns(2)
}

Expand Down
42 changes: 42 additions & 0 deletions internal/cmd/organization/member/list/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,3 +204,45 @@ func TestBuildRequest(t *testing.T) {
})
}
}

func TestOutputResult(t *testing.T) {
type args struct {
outputFormat string
sortBy string
members []authorization.Member
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "empty",
args: args{},
wantErr: false,
},
{
name: "set empty members slice",
args: args{
members: []authorization.Member{},
},
wantErr: false,
},
{
name: "set empty role in roles slice",
args: args{
members: []authorization.Member{{}},
},
wantErr: false,
},
}
p := print.NewPrinter()
p.Cmd = NewCmd(p)
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := outputResult(p, tt.args.outputFormat, tt.args.sortBy, tt.args.members); (err != nil) != tt.wantErr {
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
23 changes: 12 additions & 11 deletions internal/cmd/organization/role/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"

"github.com/goccy/go-yaml"
"github.com/spf13/cobra"
"github.com/stackitcloud/stackit-cli/internal/pkg/args"
"github.com/stackitcloud/stackit-cli/internal/pkg/errors"
"github.com/stackitcloud/stackit-cli/internal/pkg/examples"
Expand All @@ -15,8 +16,6 @@ import (
"github.com/stackitcloud/stackit-cli/internal/pkg/services/authorization/client"
"github.com/stackitcloud/stackit-cli/internal/pkg/tables"
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"

"github.com/spf13/cobra"
"github.com/stackitcloud/stackit-sdk-go/services/authorization"
)

Expand Down Expand Up @@ -153,16 +152,18 @@ func outputRolesResult(p *print.Printer, outputFormat string, roles []authorizat
table.SetHeader("ROLE NAME", "ROLE DESCRIPTION", "PERMISSION NAME", "PERMISSION DESCRIPTION")
for i := range roles {
r := roles[i]
for j := range *r.Permissions {
p := (*r.Permissions)[j]
table.AddRow(
utils.PtrString(r.Name),
utils.PtrString(r.Description),
utils.PtrString(p.Name),
utils.PtrString(p.Description),
)
if r.Permissions != nil {
for j := range *r.Permissions {
p := (*r.Permissions)[j]
table.AddRow(
utils.PtrString(r.Name),
utils.PtrString(r.Description),
utils.PtrString(p.Name),
utils.PtrString(p.Description),
)
}
table.AddSeparator()
}
table.AddSeparator()
}
table.EnableAutoMergeOnColumns(1, 2)
err := table.Display(p)
Expand Down
41 changes: 41 additions & 0 deletions internal/cmd/organization/role/list/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,3 +167,44 @@ func TestBuildRequest(t *testing.T) {
})
}
}

func TestOutputResult(t *testing.T) {
type args struct {
outputFormat string
roles []authorization.Role
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "empty",
args: args{},
wantErr: false,
},
{
name: "set empty roles slice",
args: args{
roles: []authorization.Role{},
},
wantErr: false,
},
{
name: "set empty role in roles slice",
args: args{
roles: []authorization.Role{{}},
},
wantErr: false,
},
}
p := print.NewPrinter()
p.Cmd = NewCmd(p)
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := outputRolesResult(p, tt.args.outputFormat, tt.args.roles); (err != nil) != tt.wantErr {
t.Errorf("outputRolesResult() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
Loading