Skip to content

Commit

Permalink
add digest sub-flag for list image
Browse files Browse the repository at this point in the history
fix #162
Signed-off-by: yanxuean <yan.xuean@zte.com.cn>
  • Loading branch information
yanxuean committed Oct 19, 2017
1 parent f19a6f0 commit 0625532
Showing 1 changed file with 27 additions and 2 deletions.
29 changes: 27 additions & 2 deletions cmd/crictl/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ var listImageCommand = cli.Command{
Name: "output, o",
Usage: "Output format, One of: json|yaml|table",
},
cli.BoolFlag{
Name: "digests",
Usage: "Show digests",
},
},
Action: func(context *cli.Context) error {
if err := getImageClient(context); err != nil {
Expand Down Expand Up @@ -121,15 +125,25 @@ var listImageCommand = cli.Command{
continue
}
if !verbose {
showDigest := context.Bool("digests")
if printHeader {
printHeader = false
fmt.Fprintln(w, "IMAGE\tTAG\tIMAGE ID\tSIZE")
if showDigest {
fmt.Fprintln(w, "IMAGE\tTAG\tDIGEST\tIMAGE ID\tSIZE")
} else {
fmt.Fprintln(w, "IMAGE\tTAG\tIMAGE ID\tSIZE")
}
}
repoDigest := normalizeRepoDigest(image.RepoDigests)
repoTagPairs := normalizeRepoTagPair(image.RepoTags, image.RepoDigests)
size := units.HumanSizeWithPrecision(float64(image.GetSize_()), 3)
trunctedImage := strings.TrimPrefix(image.Id, "sha256:")[:truncatedImageIDLen]
for _, repoTagPair := range repoTagPairs {
fmt.Fprintf(w, "%s\t%s\t%s\t%s\n", repoTagPair[0], repoTagPair[1], trunctedImage, size)
if showDigest {
fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\n", repoTagPair[0], repoTagPair[1], repoDigest, trunctedImage, size)
} else {
fmt.Fprintf(w, "%s\t%s\t%s\t%s\n", repoTagPair[0], repoTagPair[1], trunctedImage, size)
}
}
continue
}
Expand Down Expand Up @@ -291,6 +305,17 @@ func normalizeRepoTagPair(repoTags []string, repoDigests []string) (repoTagPairs
return
}

func normalizeRepoDigest(repoDigests []string) string {
if len(repoDigests) == 0 {
return "<none>"
}
repoDigestPair := strings.Split(repoDigests[0],"@")
if len(repoDigestPair) != 2 {
return "errorRepoDigest"
}
return repoDigestPair[1]
}

// 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 0625532

Please sign in to comment.