0
Fork 0
mirror of https://github.com/project-zot/zot.git synced 2025-01-20 22:52:51 -05:00

fix: images command not truncating image name/tag (#851)

Signed-off-by: Lisca Ana-Roberta <ana.kagome@yahoo.com>
This commit is contained in:
Lisca Ana-Roberta 2022-10-11 18:56:03 +03:00 committed by GitHub
parent 815366024b
commit 4bc7a2c824
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 50 additions and 19 deletions

View file

@ -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

View file

@ -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}

View file

@ -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
}

View file

@ -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)