Skip to content

Commit 04b0c08

Browse files
committed
feature: fix list command
1 parent 9cd8f1a commit 04b0c08

File tree

2 files changed

+20
-11
lines changed

2 files changed

+20
-11
lines changed

internal/cmd/beta/security-group/list/list.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import (
2222

2323
type inputModel struct {
2424
*globalflags.GlobalFlagModel
25-
Labels string
25+
Labels *string
2626
}
2727

2828
func NewCmd(p *print.Printer) *cobra.Command {
@@ -74,7 +74,7 @@ func executeList(cmd *cobra.Command, p *print.Printer, _ []string) error {
7474
if err != nil {
7575
return fmt.Errorf("list security group: %w", err)
7676
}
77-
if items := response.GetItems(); items == nil || len(*items) > 0 {
77+
if items := response.GetItems(); items == nil || len(*items) == 0 {
7878
p.Info("no security groups found for %q", projectLabel)
7979
} else {
8080
outputResult(p, model.OutputFormat, *items)
@@ -91,8 +91,7 @@ func parseInput(p *print.Printer, cmd *cobra.Command) (*inputModel, error) {
9191

9292
model := inputModel{
9393
GlobalFlagModel: globalFlags,
94-
95-
Labels: flags.FlagToStringValue(p, cmd, "labels"),
94+
Labels: flags.FlagToStringPointer(p, cmd, "labels"),
9695
}
9796

9897
if p.IsVerbosityDebug() {
@@ -109,7 +108,9 @@ func parseInput(p *print.Printer, cmd *cobra.Command) (*inputModel, error) {
109108

110109
func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APIClient) iaas.ApiListSecurityGroupsRequest {
111110
request := apiClient.ListSecurityGroups(ctx, model.ProjectId)
112-
request = request.LabelSelector(model.Labels)
111+
if model.Labels != nil {
112+
request = request.LabelSelector(*model.Labels)
113+
}
113114

114115
return request
115116

@@ -136,7 +137,7 @@ func outputResult(p *print.Printer, outputFormat string, items []iaas.SecurityGr
136137
table := tables.NewTable()
137138
table.SetHeader("ID", "NAME", "LABELS", "STATEFUL")
138139
for _, item := range items {
139-
table.AddRow(item.Id, item.Name, concatLabels(item.Labels), item.Stateful)
140+
table.AddRow(ptrString(item.Id), ptrString(item.Name), concatLabels(item.Labels), ptrString(item.Stateful))
140141
}
141142
err := table.Display(p)
142143
if err != nil {
@@ -147,6 +148,13 @@ func outputResult(p *print.Printer, outputFormat string, items []iaas.SecurityGr
147148
}
148149
}
149150

151+
func ptrString[T any](t*T) string {
152+
if t != nil {
153+
return fmt.Sprintf("%v",*t)
154+
}
155+
return ""
156+
}
157+
150158
func concatLabels(item *map[string]any) string {
151159
if item == nil {
152160
return ""

internal/cmd/beta/security-group/list/list_test.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66

77
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
88
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
9+
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
910

1011
"github.com/google/go-cmp/cmp"
1112
"github.com/google/go-cmp/cmp/cmpopts"
@@ -39,7 +40,7 @@ func fixtureFlagValues(mods ...func(flagValues map[string]string)) map[string]st
3940
func fixtureInputModel(mods ...func(model *inputModel)) *inputModel {
4041
model := &inputModel{
4142
GlobalFlagModel: &globalflags.GlobalFlagModel{ProjectId: testProjectId, Verbosity: globalflags.VerbosityDefault},
42-
Labels: testLabels,
43+
Labels: utils.Ptr(testLabels),
4344
}
4445
for _, mod := range mods {
4546
mod(model)
@@ -102,7 +103,7 @@ func TestParseInput(t *testing.T) {
102103
}),
103104
isValid: true,
104105
expectedModel: fixtureInputModel(func(model *inputModel) {
105-
model.Labels = ""
106+
model.Labels = nil
106107
}),
107108
},
108109
{
@@ -112,7 +113,7 @@ func TestParseInput(t *testing.T) {
112113
}),
113114
isValid: true,
114115
expectedModel: fixtureInputModel(func(model *inputModel) {
115-
model.Labels = "foo=bar"
116+
model.Labels = utils.Ptr("foo=bar")
116117
}),
117118
},
118119
}
@@ -175,7 +176,7 @@ func TestBuildRequest(t *testing.T) {
175176
{
176177
description: "no labels",
177178
model: fixtureInputModel(func(model *inputModel) {
178-
model.Labels = ""
179+
model.Labels = utils.Ptr("")
179180
}),
180181
expectedRequest: fixtureRequest(func(request *iaas.ApiListSecurityGroupsRequest) {
181182
*request = request.LabelSelector("")
@@ -184,7 +185,7 @@ func TestBuildRequest(t *testing.T) {
184185
{
185186
description: "single label",
186187
model: fixtureInputModel(func(model *inputModel) {
187-
model.Labels = "foo=bar"
188+
model.Labels = utils.Ptr("foo=bar")
188189
}),
189190
expectedRequest: fixtureRequest(func(request *iaas.ApiListSecurityGroupsRequest) {
190191
*request = request.LabelSelector("foo=bar")

0 commit comments

Comments
 (0)