Skip to content

Commit 3de9ebc

Browse files
committed
handle empty RepoTags for ListImage
fix #155 Signed-off-by: yanxuean <[email protected]> Signed-off-by: yanxuean <[email protected]>
1 parent b9f4185 commit 3de9ebc

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

cmd/crictl/image.go

+16-5
Original file line numberDiff line numberDiff line change
@@ -125,11 +125,7 @@ var listImageCommand = cli.Command{
125125
printHeader = false
126126
fmt.Fprintln(w, "IMAGE\tTAG\tIMAGE ID\tSIZE")
127127
}
128-
repoTags := "<none>"
129-
if image.RepoTags != nil {
130-
repoTags = image.RepoTags[0]
131-
}
132-
repoTagsPair := strings.Split(repoTags, ":")
128+
repoTagsPair := normalizeRepoTagPair(image.RepoTags)
133129
size := units.HumanSizeWithPrecision(float64(image.GetSize_()), 3)
134130
trunctedImage := strings.TrimPrefix(image.Id, "sha256:")[:truncatedImageIDLen]
135131
fmt.Fprintf(w, "%s\t%s\t%s\t%s\n", repoTagsPair[0], repoTagsPair[1], trunctedImage, size)
@@ -270,6 +266,21 @@ func getAuth(creds string) (*pb.AuthConfig, error) {
270266
}, nil
271267
}
272268

269+
// Support these cases as below:
270+
// "tag1:tag2", "tag1", "tag1:", ":tag2", ":", "", nil
271+
func normalizeRepoTagPair(repoTags []string) []string {
272+
const defaultTag = "<none>"
273+
if len(repoTags) == 0 {
274+
return []string{defaultTag, defaultTag}
275+
}
276+
repoTagsPair := strings.Split(repoTags[0]+":"+defaultTag, ":")
277+
// for ":tag2" case
278+
if repoTagsPair[0] == "" {
279+
repoTagsPair[0] = defaultTag
280+
}
281+
return repoTagsPair[0:2]
282+
}
283+
273284
// PullImage sends a PullImageRequest to the server, and parses
274285
// the returned PullImageResponse.
275286
func PullImage(client pb.ImageServiceClient, image string, auth *pb.AuthConfig) (*pb.PullImageResponse, error) {

0 commit comments

Comments
 (0)