From ee8c61e2f3d8f9c58fa2a5b87213091b69c49c2c Mon Sep 17 00:00:00 2001 From: yanxuean Date: Wed, 18 Oct 2017 22:56:52 +0800 Subject: [PATCH] add digest sub-flag for list image fix #162 Signed-off-by: yanxuean --- cmd/crictl/image.go | 39 +++++++++++++++++++++++++++++---------- 1 file changed, 29 insertions(+), 10 deletions(-) diff --git a/cmd/crictl/image.go b/cmd/crictl/image.go index ac7b921d18..0a842b569e 100644 --- a/cmd/crictl/image.go +++ b/cmd/crictl/image.go @@ -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 { @@ -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") + } } - repoTagPairs := normalizeRepoTagPair(image.RepoTags, image.RepoDigests) + imageName, repoDigest := normalizeRepoDigest(image.RepoDigests) + repoTagPairs := normalizeRepoTagPair(image.RepoTags, imageName) 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 } @@ -270,17 +284,11 @@ func getAuth(creds string) (*pb.AuthConfig, error) { // Ideally repo tag should always be image:tag. // The repoTags is nil when pulling image by repoDigest,Then we will show image name instead. -func normalizeRepoTagPair(repoTags []string, repoDigests []string) (repoTagPairs [][]string) { +func normalizeRepoTagPair(repoTags []string, imageName string) (repoTagPairs [][]string) { if len(repoTags) == 0 { - if len(repoDigests) == 0 { - repoTagPairs = append(repoTagPairs, []string{"errorRepoDigest", "errorRepoDigest"}) - return - } - imageName := strings.Split(repoDigests[0], "@")[0] repoTagPairs = append(repoTagPairs, []string{imageName, ""}) return } - for _, repoTag := range repoTags { if idx := strings.Index(repoTag, ":"); idx == -1 { repoTagPairs = append(repoTagPairs, []string{"errorRepoTag", "errorRepoTag"}) @@ -291,6 +299,17 @@ func normalizeRepoTagPair(repoTags []string, repoDigests []string) (repoTagPairs return } +func normalizeRepoDigest(repoDigests []string) (string, string) { + if len(repoDigests) == 0 { + return "", "" + } + repoDigestPair := strings.Split(repoDigests[0], "@") + if len(repoDigestPair) != 2 { + return "errorName", "errorRepoDigest" + } + return repoDigestPair[0], 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) {