Skip to content

Commit c8429b0

Browse files
committed
added missing --limit flag for list security groups command
1 parent 1666e15 commit c8429b0

2 files changed

Lines changed: 43 additions & 2 deletions

File tree

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

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,12 @@ import (
2525
type inputModel struct {
2626
*globalflags.GlobalFlagModel
2727
LabelSelector *string
28+
Limit *int64
2829
}
2930

3031
const (
3132
labelSelectorFlag = "label-selector"
33+
limitFlag = "limit"
3234
)
3335

3436
func NewCmd(params *types.CmdParams) *cobra.Command {
@@ -38,8 +40,16 @@ func NewCmd(params *types.CmdParams) *cobra.Command {
3840
Long: "Lists security groups by its internal ID.",
3941
Args: args.NoArgs,
4042
Example: examples.Build(
41-
examples.NewExample(`List all groups`, `$ stackit security-group list`),
42-
examples.NewExample(`List groups with labels`, `$ stackit security-group list --label-selector label1=value1,label2=value2`),
43+
examples.NewExample(`Lists all security groups`, `$ stackit security-group list`),
44+
examples.NewExample(`Lists security groups with labels`, `$ stackit security-group list --label-selector label1=value1,label2=value2`),
45+
examples.NewExample(
46+
`Lists all security groups in JSON format`,
47+
"$ stackit security-group list --output-format json",
48+
),
49+
examples.NewExample(
50+
`Lists up to 10 security groups`,
51+
"$ stackit security-group list --limit 10",
52+
),
4353
),
4454
RunE: func(cmd *cobra.Command, args []string) error {
4555
ctx := context.Background()
@@ -70,6 +80,11 @@ func NewCmd(params *types.CmdParams) *cobra.Command {
7080
projectLabel = model.ProjectId
7181
}
7282

83+
// Truncate output
84+
if model.Limit != nil && len(items) > int(*model.Limit) {
85+
items = items[:*model.Limit]
86+
}
87+
7388
return outputResult(params.Printer, model.OutputFormat, projectLabel, items)
7489

7590
},
@@ -81,6 +96,7 @@ func NewCmd(params *types.CmdParams) *cobra.Command {
8196

8297
func configureFlags(cmd *cobra.Command) {
8398
cmd.Flags().String(labelSelectorFlag, "", "Filter by label")
99+
cmd.Flags().Int64(limitFlag, 0, "Maximum number of entries to list")
84100
}
85101

86102
func parseInput(p *print.Printer, cmd *cobra.Command, _ []string) (*inputModel, error) {
@@ -89,9 +105,18 @@ func parseInput(p *print.Printer, cmd *cobra.Command, _ []string) (*inputModel,
89105
return nil, &errors.ProjectIdError{}
90106
}
91107

108+
limit := flags.FlagToInt64Pointer(p, cmd, limitFlag)
109+
if limit != nil && *limit < 1 {
110+
return nil, &errors.FlagValidationError{
111+
Flag: limitFlag,
112+
Details: "must be greater than 0",
113+
}
114+
}
115+
92116
model := inputModel{
93117
GlobalFlagModel: globalFlags,
94118
LabelSelector: flags.FlagToStringPointer(p, cmd, labelSelectorFlag),
119+
Limit: limit,
95120
}
96121

97122
p.DebugInputModel(model)

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ func fixtureFlagValues(mods ...func(flagValues map[string]string)) map[string]st
3636
globalflags.RegionFlag: testRegion,
3737

3838
labelSelectorFlag: testLabels,
39+
limitFlag: "10",
3940
}
4041
for _, mod := range mods {
4142
mod(flagValues)
@@ -51,6 +52,7 @@ func fixtureInputModel(mods ...func(model *inputModel)) *inputModel {
5152
Verbosity: globalflags.VerbosityDefault,
5253
},
5354
LabelSelector: utils.Ptr(testLabels),
55+
Limit: utils.Ptr(int64(10)),
5456
}
5557
for _, mod := range mods {
5658
mod(model)
@@ -127,6 +129,20 @@ func TestParseInput(t *testing.T) {
127129
model.LabelSelector = utils.Ptr("foo=bar")
128130
}),
129131
},
132+
{
133+
description: "limit invalid",
134+
flagValues: fixtureFlagValues(func(flagValues map[string]string) {
135+
flagValues[limitFlag] = "invalid"
136+
}),
137+
isValid: false,
138+
},
139+
{
140+
description: "limit invalid 2",
141+
flagValues: fixtureFlagValues(func(flagValues map[string]string) {
142+
flagValues[limitFlag] = "0"
143+
}),
144+
isValid: false,
145+
},
130146
}
131147

132148
for _, tt := range tests {

0 commit comments

Comments
 (0)