Skip to content

Commit b3afc4b

Browse files
committed
added missing --limit flag for list security groups command
1 parent e0db653 commit b3afc4b

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
@@ -34,6 +34,7 @@ func fixtureFlagValues(mods ...func(flagValues map[string]string)) map[string]st
3434
globalflags.RegionFlag: testRegion,
3535

3636
labelSelectorFlag: testLabels,
37+
limitFlag: "10",
3738
}
3839
for _, mod := range mods {
3940
mod(flagValues)
@@ -49,6 +50,7 @@ func fixtureInputModel(mods ...func(model *inputModel)) *inputModel {
4950
Verbosity: globalflags.VerbosityDefault,
5051
},
5152
LabelSelector: utils.Ptr(testLabels),
53+
Limit: utils.Ptr(int64(10)),
5254
}
5355
for _, mod := range mods {
5456
mod(model)
@@ -125,6 +127,20 @@ func TestParseInput(t *testing.T) {
125127
model.LabelSelector = utils.Ptr("foo=bar")
126128
}),
127129
},
130+
{
131+
description: "limit invalid",
132+
flagValues: fixtureFlagValues(func(flagValues map[string]string) {
133+
flagValues[limitFlag] = "invalid"
134+
}),
135+
isValid: false,
136+
},
137+
{
138+
description: "limit invalid 2",
139+
flagValues: fixtureFlagValues(func(flagValues map[string]string) {
140+
flagValues[limitFlag] = "0"
141+
}),
142+
isValid: false,
143+
},
128144
}
129145

130146
for _, tt := range tests {

0 commit comments

Comments
 (0)