Skip to content

Commit dca821f

Browse files
committed
adapted affinity list command to align to expectations
1 parent a31e39f commit dca821f

2 files changed

Lines changed: 24 additions & 18 deletions

File tree

internal/cmd/affinity-groups/list/list.go

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"github.com/stackitcloud/stackit-cli/internal/pkg/flags"
1919
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
2020
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
21+
"github.com/stackitcloud/stackit-cli/internal/pkg/projectname"
2122
"github.com/stackitcloud/stackit-cli/internal/pkg/services/iaas/client"
2223
)
2324

@@ -63,16 +64,19 @@ func NewCmd(params *types.CmdParams) *cobra.Command {
6364
if err != nil {
6465
return fmt.Errorf("list affinity groups: %w", err)
6566
}
67+
items := result.GetItems()
6668

67-
if items := result.Items; items != nil {
68-
if model.Limit != nil && len(*items) > int(*model.Limit) {
69-
*items = (*items)[:*model.Limit]
70-
}
71-
return outputResult(params.Printer, *model, *items)
69+
projectLabel, err := projectname.GetProjectName(ctx, params.Printer, params.CliVersion, cmd)
70+
if err != nil {
71+
params.Printer.Debug(print.ErrorLevel, "get project name: %v", err)
72+
projectLabel = model.ProjectId
7273
}
7374

74-
params.Printer.Outputln("No affinity groups found")
75-
return nil
75+
// Truncate Output
76+
if model.Limit != nil && len(items) > int(*model.Limit) {
77+
items = items[:*model.Limit]
78+
}
79+
return outputResult(params.Printer, model.OutputFormat, projectLabel, items)
7680
},
7781
}
7882
configureFlags(cmd)
@@ -110,13 +114,12 @@ func parseInput(p *print.Printer, cmd *cobra.Command, _ []string) (*inputModel,
110114
return &model, nil
111115
}
112116

113-
func outputResult(p *print.Printer, model inputModel, items []iaas.AffinityGroup) error {
114-
var outputFormat string
115-
if model.GlobalFlagModel != nil {
116-
outputFormat = model.OutputFormat
117-
}
118-
117+
func outputResult(p *print.Printer, outputFormat, projectLabel string, items []iaas.AffinityGroup) error {
119118
return p.OutputResult(outputFormat, items, func() error {
119+
if len(items) == 0 {
120+
p.Outputf("No affinity groups found for project %q\n", projectLabel)
121+
return nil
122+
}
120123
table := tables.NewTable()
121124
table.SetHeader("ID", "NAME", "POLICY")
122125
for _, item := range items {

internal/cmd/affinity-groups/list/list_test.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -142,24 +142,27 @@ func TestBuildRequest(t *testing.T) {
142142
}
143143

144144
func TestOutputResult(t *testing.T) {
145+
type args struct {
146+
outputFormat string
147+
projectLabel string
148+
instances []iaas.AffinityGroup
149+
}
145150
tests := []struct {
146151
description string
147-
model inputModel
148-
response []iaas.AffinityGroup
152+
args args
149153
isValid bool
150154
}{
151155
{
152156
description: "empty",
153-
model: inputModel{},
154-
response: []iaas.AffinityGroup{},
157+
args: args{},
155158
isValid: true,
156159
},
157160
}
158161
p := print.NewPrinter()
159162
p.Cmd = NewCmd(&types.CmdParams{Printer: p})
160163
for _, tt := range tests {
161164
t.Run(tt.description, func(t *testing.T) {
162-
err := outputResult(p, tt.model, tt.response)
165+
err := outputResult(p, tt.args.outputFormat, tt.args.projectLabel, tt.args.instances)
163166
if err != nil {
164167
if !tt.isValid {
165168
return

0 commit comments

Comments
 (0)