diff --git a/cmd/image_test.go b/cmd/image_test.go index afe3384..16e3af4 100644 --- a/cmd/image_test.go +++ b/cmd/image_test.go @@ -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", @@ -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, @@ -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 }}"), @@ -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 | `), }, { @@ -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 | `), }, { diff --git a/cmd/tableprinters/image.go b/cmd/tableprinters/image.go index e4e613c..6188299 100644 --- a/cmd/tableprinters/image.go +++ b/cmd/tableprinters/image.go @@ -2,12 +2,12 @@ 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) { @@ -15,12 +15,10 @@ func (t *TablePrinter) ImageTable(data []*models.V1ImageResponse, wide bool) ([] 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, ",") @@ -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