Skip to content

Commit

Permalink
Fix image usage display (#177)
Browse files Browse the repository at this point in the history
  • Loading branch information
majst01 authored Jan 11, 2023
1 parent d86232c commit 77021d4
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 29 deletions.
36 changes: 18 additions & 18 deletions cmd/image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ var (
ID: pointer.Pointer("debian"),
Name: "debian-name",
URL: "debian-url",
Usedby: []string{"456"},
Usedby: []string{"abc-def"},
}
image2 = &models.V1ImageResponse{
Classification: "supported",
Expand All @@ -45,11 +45,11 @@ func Test_ImageCmd_MultiResult(t *testing.T) {
{
name: "list",
cmd: func(want []*models.V1ImageResponse) []string {
return []string{"image", "list"}
return []string{"image", "list", "--show-usage"}
},
mocks: &client.MetalMockFns{
Image: func(mock *mock.Mock) {
mock.On("ListImages", testcommon.MatchIgnoreContext(t, image.NewListImagesParams().WithShowUsage(pointer.Pointer(false))), nil).Return(&image.ListImagesOK{
mock.On("ListImages", testcommon.MatchIgnoreContext(t, image.NewListImagesParams().WithShowUsage(pointer.Pointer(true))), nil).Return(&image.ListImagesOK{
Payload: []*models.V1ImageResponse{
image2,
image1,
Expand All @@ -62,13 +62,13 @@ func Test_ImageCmd_MultiResult(t *testing.T) {
image2,
},
wantTable: pointer.Pointer(`
ID NAME DESCRIPTION FEATURES EXPIRATION STATUS
debian debian-name debian-description machine 3d supported
ubuntu ubuntu-name ubuntu-description machine 3d supported
ID NAME DESCRIPTION FEATURES EXPIRATION STATUS USEDBY
debian debian-name debian-description machine 3d supported 1
ubuntu ubuntu-name ubuntu-description machine 3d supported 1
`),
wantWideTable: pointer.Pointer(`
ID NAME DESCRIPTION FEATURES EXPIRATION STATUS USEDBY
debian debian-name debian-description machine 3d supported 456
debian debian-name debian-description machine 3d supported abc-def
ubuntu ubuntu-name ubuntu-description machine 3d supported 123
`),
template: pointer.Pointer("{{ .id }} {{ .name }}"),
Expand All @@ -77,10 +77,10 @@ debian debian-name
ubuntu ubuntu-name
`),
wantMarkdown: pointer.Pointer(`
| ID | NAME | DESCRIPTION | FEATURES | EXPIRATION | STATUS |
|--------|-------------|--------------------|----------|------------|-----------|
| debian | debian-name | debian-description | machine | 3d | supported |
| ubuntu | ubuntu-name | ubuntu-description | machine | 3d | supported |
| ID | NAME | DESCRIPTION | FEATURES | EXPIRATION | STATUS | USEDBY |
|--------|-------------|--------------------|----------|------------|-----------|--------|
| debian | debian-name | debian-description | machine | 3d | supported | 1 |
| ubuntu | ubuntu-name | ubuntu-description | machine | 3d | supported | 1 |
`),
},
{
Expand Down Expand Up @@ -129,21 +129,21 @@ func Test_ImageCmd_SingleResult(t *testing.T) {
},
want: image1,
wantTable: pointer.Pointer(`
ID NAME DESCRIPTION FEATURES EXPIRATION STATUS
debian debian-name debian-description machine 3d supported
ID NAME DESCRIPTION FEATURES EXPIRATION STATUS USEDBY
debian debian-name debian-description machine 3d supported 1
`),
wantWideTable: pointer.Pointer(`
ID NAME DESCRIPTION FEATURES EXPIRATION STATUS USEDBY
debian debian-name debian-description machine 3d supported 456
ID NAME DESCRIPTION FEATURES EXPIRATION STATUS USEDBY
debian debian-name debian-description machine 3d supported abc-def
`),
template: pointer.Pointer("{{ .id }} {{ .name }}"),
wantTemplate: pointer.Pointer(`
debian debian-name
`),
wantMarkdown: pointer.Pointer(`
| ID | NAME | DESCRIPTION | FEATURES | EXPIRATION | STATUS |
|--------|-------------|--------------------|----------|------------|-----------|
| debian | debian-name | debian-description | machine | 3d | supported |
| ID | NAME | DESCRIPTION | FEATURES | EXPIRATION | STATUS | USEDBY |
|--------|-------------|--------------------|----------|------------|-----------|--------|
| debian | debian-name | debian-description | machine | 3d | supported | 1 |
`),
},
{
Expand Down
19 changes: 8 additions & 11 deletions cmd/tableprinters/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,23 @@ package tableprinters

import (
"fmt"
"sort"
"strings"
"time"

"github.com/metal-stack/metal-go/api/models"
"github.com/metal-stack/metal-lib/pkg/pointer"
"github.com/spf13/viper"
)

func (t *TablePrinter) ImageTable(data []*models.V1ImageResponse, wide bool) ([]string, [][]string, error) {
var (
rows [][]string
)

header := []string{"ID", "Name", "Description", "Features", "Expiration", "Status"}
if wide {
header = []string{"ID", "Name", "Description", "Features", "Expiration", "Status", "UsedBy"}
}
showUsage := viper.GetBool("show-usage")

header := []string{"ID", "Name", "Description", "Features", "Expiration", "Status", "UsedBy"}

sort.SliceStable(data, func(i, j int) bool { return *data[i].ID < *data[j].ID })
for _, image := range data {
id := pointer.SafeDeref(image.ID)
features := strings.Join(image.Features, ",")
Expand All @@ -37,12 +35,11 @@ func (t *TablePrinter) ImageTable(data []*models.V1ImageResponse, wide bool) ([]
if wide {
usedBy = strings.Join(image.Usedby, "\n")
}

if wide {
rows = append(rows, []string{id, name, description, features, expiration, status, usedBy})
} else {
rows = append(rows, []string{id, name, description, features, expiration, status})
if !showUsage {
usedBy = ""
}

rows = append(rows, []string{id, name, description, features, expiration, status, usedBy})
}

return header, rows, nil
Expand Down

0 comments on commit 77021d4

Please sign in to comment.