Skip to content

Commit 1666e15

Browse files
committed
adapted security group list command to align to expectations
1 parent 0bc8501 commit 1666e15

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
@@ -184,6 +184,7 @@ func TestBuildRequest(t *testing.T) {
184184
func TestOutputResult(t *testing.T) {
185185
type args struct {
186186
outputFormat string
187+
projectLabel string
187188
items []iaas.SecurityGroup
188189
}
189190
tests := []struct {
@@ -201,7 +202,7 @@ func TestOutputResult(t *testing.T) {
201202
p.Cmd = NewCmd(&types.CmdParams{Printer: p})
202203
for _, tt := range tests {
203204
t.Run(tt.name, func(t *testing.T) {
204-
if err := outputResult(p, tt.args.outputFormat, tt.args.items); (err != nil) != tt.wantErr {
205+
if err := outputResult(p, tt.args.outputFormat, tt.args.projectLabel, tt.args.items); (err != nil) != tt.wantErr {
205206
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
206207
}
207208
})

0 commit comments

Comments
 (0)