Skip to content

Commit e0db653

Browse files
committed
adapted security group list command to align to expectations
1 parent 4661dc5 commit e0db653

2 files changed

Lines changed: 15 additions & 15 deletions

File tree

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

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,6 @@ func NewCmd(params *types.CmdParams) *cobra.Command {
5454
return err
5555
}
5656

57-
projectLabel, err := projectname.GetProjectName(ctx, params.Printer, params.CliVersion, cmd)
58-
if err != nil {
59-
params.Printer.Debug(print.ErrorLevel, "get project name: %v", err)
60-
projectLabel = model.ProjectId
61-
}
62-
6357
// Call API
6458
request := buildRequest(ctx, model, apiClient)
6559

@@ -68,15 +62,16 @@ func NewCmd(params *types.CmdParams) *cobra.Command {
6862
return fmt.Errorf("list security group: %w", err)
6963
}
7064

71-
if items := response.GetItems(); len(items) == 0 {
72-
params.Printer.Info("No security groups found for project %q", projectLabel)
73-
} else {
74-
if err := outputResult(params.Printer, model.OutputFormat, items); err != nil {
75-
return fmt.Errorf("output security groups: %w", err)
76-
}
65+
items := response.GetItems()
66+
67+
projectLabel, err := projectname.GetProjectName(ctx, params.Printer, params.CliVersion, cmd)
68+
if err != nil {
69+
params.Printer.Debug(print.ErrorLevel, "get project name: %v", err)
70+
projectLabel = model.ProjectId
7771
}
7872

79-
return nil
73+
return outputResult(params.Printer, model.OutputFormat, projectLabel, items)
74+
8075
},
8176
}
8277

@@ -111,8 +106,12 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APICli
111106

112107
return request
113108
}
114-
func outputResult(p *print.Printer, outputFormat string, items []iaas.SecurityGroup) error {
109+
func outputResult(p *print.Printer, outputFormat, projectLabel string, items []iaas.SecurityGroup) error {
115110
return p.OutputResult(outputFormat, items, func() error {
111+
if len(items) == 0 {
112+
p.Outputf("No security groups found for project %q\n", projectLabel)
113+
return nil
114+
}
116115
table := tables.NewTable()
117116
table.SetHeader("ID", "NAME", "STATEFUL", "DESCRIPTION", "LABELS")
118117
for _, item := range items {

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ func TestBuildRequest(t *testing.T) {
182182
func TestOutputResult(t *testing.T) {
183183
type args struct {
184184
outputFormat string
185+
projectLabel string
185186
items []iaas.SecurityGroup
186187
}
187188
tests := []struct {
@@ -198,7 +199,7 @@ func TestOutputResult(t *testing.T) {
198199
params := testparams.NewTestParams()
199200
for _, tt := range tests {
200201
t.Run(tt.name, func(t *testing.T) {
201-
if err := outputResult(params.Printer, tt.args.outputFormat, tt.args.items); (err != nil) != tt.wantErr {
202+
if err := outputResult(params.Printer, tt.args.outputFormat, tt.args.projectLabel, tt.args.items); (err != nil) != tt.wantErr {
202203
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
203204
}
204205
})

0 commit comments

Comments
 (0)