From 0ef38ba07971ea9921a98353789fdb5655e65c10 Mon Sep 17 00:00:00 2001 From: Miloslav Trmač Date: Fri, 27 Jul 2018 03:20:25 +0200 Subject: Return early in refNamesFromImageReference instead of appending to pullNames MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Almost all paths appended to pullNames exactly once; just construct a single-element array in place and return it. That way we can add empty lines as separators, and still come out shorter. Should not change behavior. Signed-off-by: Miloslav Trmač Closes: #1176 Approved by: rhatdan --- libpod/image/pull.go | 50 ++++++++++++++++++++++++-------------------------- 1 file changed, 24 insertions(+), 26 deletions(-) diff --git a/libpod/image/pull.go b/libpod/image/pull.go index b17b4434d..2b3f70441 100644 --- a/libpod/image/pull.go +++ b/libpod/image/pull.go @@ -86,8 +86,6 @@ func getPullRefName(srcRef types.ImageReference, destName string) *pullRefName { // refNamesFromImageReference returns a list of pullRefName for a single ImageReference, depending on the used transport. func refNamesFromImageReference(ctx context.Context, srcRef types.ImageReference, imgName string, sc *types.SystemContext) ([]*pullRefName, error) { - var pullNames []*pullRefName - // supports pulling from docker-archive, oci, and registries switch srcRef.Transport().Name() { case DockerArchive: @@ -108,26 +106,28 @@ func refNamesFromImageReference(ctx context.Context, srcRef types.ImageReference if err != nil { return nil, err } - pullInfo := getPullRefName(srcRef, reference) - pullNames = append(pullNames, pullInfo) + return []*pullRefName{getPullRefName(srcRef, reference)}, nil + } + + var dest []string + if len(manifest[0].RepoTags) > 0 { + dest = append(dest, manifest[0].RepoTags...) } else { - var dest []string - if len(manifest[0].RepoTags) > 0 { - dest = append(dest, manifest[0].RepoTags...) - } else { - // If the input image has no repotags, we need to feed it a dest anyways - digest, err := getImageDigest(ctx, srcRef, sc) - if err != nil { - return nil, err - } - dest = append(dest, digest) - } - // Need to load in all the repo tags from the manifest - for _, dst := range dest { - pullInfo := getPullRefName(srcRef, dst) - pullNames = append(pullNames, pullInfo) + // If the input image has no repotags, we need to feed it a dest anyways + digest, err := getImageDigest(ctx, srcRef, sc) + if err != nil { + return nil, err } + dest = append(dest, digest) } + // Need to load in all the repo tags from the manifest + res := []*pullRefName{} + for _, dst := range dest { + pullInfo := getPullRefName(srcRef, dst) + res = append(res, pullInfo) + } + return res, nil + case OCIArchive: // retrieve the manifest from index.json to access the image name manifest, err := ociarchive.LoadManifestDescriptor(srcRef) @@ -146,8 +146,8 @@ func refNamesFromImageReference(ctx context.Context, srcRef types.ImageReference } else { dest = manifest.Annotations["org.opencontainers.image.ref.name"] } - pullInfo := getPullRefName(srcRef, dest) - pullNames = append(pullNames, pullInfo) + return []*pullRefName{getPullRefName(srcRef, dest)}, nil + case DirTransport: path := srcRef.StringWithinTransport() image := path @@ -157,13 +157,11 @@ func refNamesFromImageReference(ctx context.Context, srcRef types.ImageReference // so docker.io isn't prepended, and the path becomes the repository image = DefaultLocalRepo + image } - pullInfo := getPullRefName(srcRef, image) - pullNames = append(pullNames, pullInfo) + return []*pullRefName{getPullRefName(srcRef, image)}, nil + default: - pullInfo := getPullRefName(srcRef, imgName) - pullNames = append(pullNames, pullInfo) + return []*pullRefName{getPullRefName(srcRef, imgName)}, nil } - return pullNames, nil } // refPairsFromImageReference returns a list of pullRefPair for a single ImageReference, depending on the used transport. -- cgit v1.2.3-54-g00ecf