From 4bc7a2c8247c5a2a874dc9648d723e2c471c1973 Mon Sep 17 00:00:00 2001 From: Lisca Ana-Roberta <55219463+aokirisaki@users.noreply.github.com> Date: Tue, 11 Oct 2022 18:56:03 +0300 Subject: [PATCH] fix: images command not truncating image name/tag (#851) Signed-off-by: Lisca Ana-Roberta --- pkg/cli/client.go | 2 +- pkg/cli/image_cmd_test.go | 4 ++-- pkg/cli/searcher.go | 45 +++++++++++++++++++++++++++++++-------- pkg/cli/service.go | 18 ++++++++++------ 4 files changed, 50 insertions(+), 19 deletions(-) diff --git a/pkg/cli/client.go b/pkg/cli/client.go index 8da0ab98..077c8ab7 100644 --- a/pkg/cli/client.go +++ b/pkg/cli/client.go @@ -303,7 +303,7 @@ func (p *requestsPool) doJob(ctx context.Context, job *manifestJob) { image.ConfigDigest = configDigest image.Layers = layers - str, err := image.string(*job.config.outputFormat) + str, err := image.string(*job.config.outputFormat, len(job.imageName), len(job.tagName)) if err != nil { if isContextDone(ctx) { return diff --git a/pkg/cli/image_cmd_test.go b/pkg/cli/image_cmd_test.go index 19b20920..3ce28fdd 100644 --- a/pkg/cli/image_cmd_test.go +++ b/pkg/cli/image_cmd_test.go @@ -1568,7 +1568,7 @@ func (service mockService) getAllImages(ctx context.Context, config searchConfig image.Digest = "DigestsAreReallyLong" image.Size = "123445" - str, err := image.string(*config.outputFormat) + str, err := image.string(*config.outputFormat, len(image.RepoName), len(image.Tag)) if err != nil { channel <- stringResult{"", err} @@ -1590,7 +1590,7 @@ func (service mockService) getImageByName(ctx context.Context, config searchConf image.Digest = "DigestsAreReallyLong" image.Size = "123445" - str, err := image.string(*config.outputFormat) + str, err := image.string(*config.outputFormat, len(image.RepoName), len(image.Tag)) if err != nil { channel <- stringResult{"", err} diff --git a/pkg/cli/searcher.go b/pkg/cli/searcher.go index 98b14509..1ec4abee 100644 --- a/pkg/cli/searcher.go +++ b/pkg/cli/searcher.go @@ -358,7 +358,7 @@ func (search cveByImageSearcherGQL) search(config searchConfig) (bool, error) { if len(cveList.Data.CVEListForImage.CVEList) > 0 && (*config.outputFormat == defaultOutoutFormat || *config.outputFormat == "") { - printCVETableHeader(&builder, *config.verbose) + printCVETableHeader(&builder, *config.verbose, 0, 0) fmt.Fprint(config.resultWriter, builder.String()) } @@ -589,7 +589,7 @@ func collectResults(config searchConfig, wg *sync.WaitGroup, imageErr chan strin if !foundResult && (*config.outputFormat == defaultOutoutFormat || *config.outputFormat == "") { var builder strings.Builder - printHeader(&builder, *config.verbose) + printHeader(&builder, *config.verbose, 0, 0) fmt.Fprint(config.resultWriter, builder.String()) } @@ -691,9 +691,9 @@ type stringResult struct { Err error } -type printHeader func(writer io.Writer, verbose bool) +type printHeader func(writer io.Writer, verbose bool, maxImageNameLen, maxTagLen int) -func printImageTableHeader(writer io.Writer, verbose bool) { +func printImageTableHeader(writer io.Writer, verbose bool, maxImageNameLen, maxTagLen int) { table := getImageTableWriter(writer) table.SetColMinWidth(colImageNameIndex, imageNameWidth) @@ -708,8 +708,23 @@ func printImageTableHeader(writer io.Writer, verbose bool) { row := make([]string, 6) //nolint:gomnd - row[colImageNameIndex] = "IMAGE NAME" - row[colTagIndex] = "TAG" + // adding spaces so that image name and tag columns are aligned + // in case the name/tag are fully shown and too long + var offset string + if maxImageNameLen > len("IMAGE NAME") { + offset = strings.Repeat(" ", maxImageNameLen-len("IMAGE NAME")) + row[colImageNameIndex] = "IMAGE NAME" + offset + } else { + row[colImageNameIndex] = "IMAGE NAME" + } + + if maxTagLen > len("TAG") { + offset = strings.Repeat(" ", maxTagLen-len("TAG")) + row[colTagIndex] = "TAG" + offset + } else { + row[colTagIndex] = "TAG" + } + row[colDigestIndex] = "DIGEST" row[colSizeIndex] = "SIZE" @@ -722,7 +737,7 @@ func printImageTableHeader(writer io.Writer, verbose bool) { table.Render() } -func printCVETableHeader(writer io.Writer, verbose bool) { +func printCVETableHeader(writer io.Writer, verbose bool, maxImgLen, maxTagLen int) { table := getCVETableWriter(writer) row := make([]string, 3) //nolint:gomnd row[colCVEIDIndex] = "ID" @@ -735,9 +750,21 @@ func printCVETableHeader(writer io.Writer, verbose bool) { func printResult(config searchConfig, imageList []imageStruct) error { var builder strings.Builder + maxImgNameLen := 0 + maxTagLen := 0 if len(imageList) > 0 { - printImageTableHeader(&builder, *config.verbose) + for i := range imageList { + if maxImgNameLen < len(imageList[i].RepoName) { + maxImgNameLen = len(imageList[i].RepoName) + } + + if maxTagLen < len(imageList[i].Tag) { + maxTagLen = len(imageList[i].Tag) + } + } + + printImageTableHeader(&builder, *config.verbose, maxImgNameLen, maxTagLen) fmt.Fprint(config.resultWriter, builder.String()) } @@ -745,7 +772,7 @@ func printResult(config searchConfig, imageList []imageStruct) error { img := imageList[i] img.verbose = *config.verbose - out, err := img.string(*config.outputFormat) + out, err := img.string(*config.outputFormat, maxImgNameLen, maxTagLen) if err != nil { return err } diff --git a/pkg/cli/service.go b/pkg/cli/service.go index 292a0f4a..9d156d70 100644 --- a/pkg/cli/service.go +++ b/pkg/cli/service.go @@ -888,10 +888,10 @@ type layer struct { Digest string `json:"digest"` } -func (img imageStruct) string(format string) (string, error) { +func (img imageStruct) string(format string, maxImgNameLen, maxTagLen int) (string, error) { switch strings.ToLower(format) { case "", defaultOutoutFormat: - return img.stringPlainText() + return img.stringPlainText(maxImgNameLen, maxTagLen) case "json": return img.stringJSON() case "yml", "yaml": @@ -901,12 +901,14 @@ func (img imageStruct) string(format string) (string, error) { } } -func (img imageStruct) stringPlainText() (string, error) { +func (img imageStruct) stringPlainText(maxImgNameLen, maxTagLen int) (string, error) { var builder strings.Builder table := getImageTableWriter(&builder) - table.SetColMinWidth(colImageNameIndex, imageNameWidth) - table.SetColMinWidth(colTagIndex, tagWidth) + + table.SetColMinWidth(colImageNameIndex, maxImgNameLen) + table.SetColMinWidth(colTagIndex, maxTagLen) + table.SetColMinWidth(colDigestIndex, digestWidth) table.SetColMinWidth(colSizeIndex, sizeWidth) @@ -915,8 +917,10 @@ func (img imageStruct) stringPlainText() (string, error) { table.SetColMinWidth(colLayersIndex, layersWidth) } - imageName := ellipsize(img.RepoName, imageNameWidth, ellipsis) - tagName := ellipsize(img.Tag, tagWidth, ellipsis) + var imageName, tagName string + + imageName = img.RepoName + tagName = img.Tag digest := ellipsize(img.Digest, digestWidth, "") imgSize, _ := strconv.ParseUint(img.Size, 10, 64) size := ellipsize(strings.ReplaceAll(humanize.Bytes(imgSize), " ", ""), sizeWidth, ellipsis)