Skip to content

Commit

Permalink
handle empty RepoTags for ListImage
Browse files Browse the repository at this point in the history
fix #155
Signed-off-by: yanxuean <[email protected]>

Signed-off-by: yanxuean <[email protected]>
  • Loading branch information
yanxuean committed Oct 13, 2017
1 parent b9f4185 commit 6921f20
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions cmd/crictl/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,7 @@ var listImageCommand = cli.Command{
printHeader = false
fmt.Fprintln(w, "IMAGE\tTAG\tIMAGE ID\tSIZE")
}
repoTags := "<none>"
if image.RepoTags != nil {
repoTags = image.RepoTags[0]
}
repoTagsPair := strings.Split(repoTags, ":")
repoTagsPair := parseRepoTagPair(image.RepoTags)
size := units.HumanSizeWithPrecision(float64(image.GetSize_()), 3)
trunctedImage := strings.TrimPrefix(image.Id, "sha256:")[:truncatedImageIDLen]
fmt.Fprintf(w, "%s\t%s\t%s\t%s\n", repoTagsPair[0], repoTagsPair[1], trunctedImage, size)
Expand Down Expand Up @@ -270,6 +266,21 @@ func getAuth(creds string) (*pb.AuthConfig, error) {
}, nil
}

// Support these cases as below:
// "tag1:tag2", "tag1", "tag1:", ":tag2", ":", "", nil
func parseRepoTagPair(repoTags []string) []string {
const defaultTag = "<none>"
if len(repoTags) == 0 {
return []string{defaultTag, defaultTag}
}
repoTagsPair := strings.Split(repoTags[0]+":"+defaultTag, ":")
// for ":tag2" case
if repoTagsPair[0] == "" {
repoTagsPair[0] = defaultTag
}
return repoTagsPair[0:2]
}

// PullImage sends a PullImageRequest to the server, and parses
// the returned PullImageResponse.
func PullImage(client pb.ImageServiceClient, image string, auth *pb.AuthConfig) (*pb.PullImageResponse, error) {
Expand Down

0 comments on commit 6921f20

Please sign in to comment.