Skip to content

Commit fb181fb

Browse files
committed
adapted image list command to align to expectations
1 parent dca821f commit fb181fb

2 files changed

Lines changed: 14 additions & 12 deletions

File tree

internal/cmd/image/list/list.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -81,21 +81,18 @@ func NewCmd(params *types.CmdParams) *cobra.Command {
8181

8282
// Call API
8383
request := buildRequest(ctx, model, apiClient)
84-
8584
response, err := request.Execute()
8685
if err != nil {
8786
return fmt.Errorf("list images: %w", err)
8887
}
88+
items := response.GetItems()
8989

90-
if items := response.GetItems(); len(items) == 0 {
91-
params.Printer.Info("No images found for project %q", projectLabel)
92-
} else {
93-
if model.Limit != nil && len(items) > int(*model.Limit) {
94-
items = (items)[:*model.Limit]
95-
}
96-
if err := outputResult(params.Printer, model.OutputFormat, items); err != nil {
97-
return fmt.Errorf("output images: %w", err)
98-
}
90+
// Truncate output
91+
if model.Limit != nil && len(items) > int(*model.Limit) {
92+
items = (items)[:*model.Limit]
93+
}
94+
if err := outputResult(params.Printer, model.OutputFormat, projectLabel, items); err != nil {
95+
return fmt.Errorf("output images: %w", err)
9996
}
10097

10198
return nil
@@ -149,8 +146,12 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APICli
149146
return request
150147
}
151148

152-
func outputResult(p *print.Printer, outputFormat string, items []iaas.Image) error {
149+
func outputResult(p *print.Printer, outputFormat, projectLabel string, items []iaas.Image) error {
153150
return p.OutputResult(outputFormat, items, func() error {
151+
if len(items) == 0 {
152+
p.Outputf("No images found for project %q\n", projectLabel)
153+
return nil
154+
}
154155
table := tables.NewTable()
155156
table.SetHeader("ID", "NAME", "OS", "ARCHITECTURE", "DISTRIBUTION", "VERSION", "SCOPE", "OWNER", "LABELS")
156157
for i := range items {

internal/cmd/image/list/list_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ func TestBuildRequest(t *testing.T) {
189189
func Test_outputResult(t *testing.T) {
190190
type args struct {
191191
outputFormat string
192+
projectLabel string
192193
items []iaas.Image
193194
}
194195
tests := []struct {
@@ -217,7 +218,7 @@ func Test_outputResult(t *testing.T) {
217218
p.Cmd = NewCmd(&types.CmdParams{Printer: p})
218219
for _, tt := range tests {
219220
t.Run(tt.name, func(t *testing.T) {
220-
if err := outputResult(p, tt.args.outputFormat, tt.args.items); (err != nil) != tt.wantErr {
221+
if err := outputResult(p, tt.args.outputFormat, tt.args.projectLabel, tt.args.items); (err != nil) != tt.wantErr {
221222
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
222223
}
223224
})

0 commit comments

Comments
 (0)