Return early in refNamesFromImageReference instead of appending to pullNames

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č <mitr@redhat.com>

Closes: #1176
Approved by: rhatdan
This commit is contained in:
Miloslav Trmač
2018-07-27 03:20:25 +02:00
committed by Atomic Bot
parent ecc1db39b5
commit 0ef38ba079

View File

@ -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. // 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) { 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 // supports pulling from docker-archive, oci, and registries
switch srcRef.Transport().Name() { switch srcRef.Transport().Name() {
case DockerArchive: case DockerArchive:
@ -108,26 +106,28 @@ func refNamesFromImageReference(ctx context.Context, srcRef types.ImageReference
if err != nil { if err != nil {
return nil, err return nil, err
} }
pullInfo := getPullRefName(srcRef, reference) return []*pullRefName{getPullRefName(srcRef, reference)}, nil
pullNames = append(pullNames, pullInfo)
} 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)
}
} }
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
res := []*pullRefName{}
for _, dst := range dest {
pullInfo := getPullRefName(srcRef, dst)
res = append(res, pullInfo)
}
return res, nil
case OCIArchive: case OCIArchive:
// retrieve the manifest from index.json to access the image name // retrieve the manifest from index.json to access the image name
manifest, err := ociarchive.LoadManifestDescriptor(srcRef) manifest, err := ociarchive.LoadManifestDescriptor(srcRef)
@ -146,8 +146,8 @@ func refNamesFromImageReference(ctx context.Context, srcRef types.ImageReference
} else { } else {
dest = manifest.Annotations["org.opencontainers.image.ref.name"] dest = manifest.Annotations["org.opencontainers.image.ref.name"]
} }
pullInfo := getPullRefName(srcRef, dest) return []*pullRefName{getPullRefName(srcRef, dest)}, nil
pullNames = append(pullNames, pullInfo)
case DirTransport: case DirTransport:
path := srcRef.StringWithinTransport() path := srcRef.StringWithinTransport()
image := path 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 // so docker.io isn't prepended, and the path becomes the repository
image = DefaultLocalRepo + image image = DefaultLocalRepo + image
} }
pullInfo := getPullRefName(srcRef, image) return []*pullRefName{getPullRefName(srcRef, image)}, nil
pullNames = append(pullNames, pullInfo)
default: default:
pullInfo := getPullRefName(srcRef, imgName) return []*pullRefName{getPullRefName(srcRef, imgName)}, nil
pullNames = append(pullNames, pullInfo)
} }
return pullNames, nil
} }
// refPairsFromImageReference returns a list of pullRefPair for a single ImageReference, depending on the used transport. // refPairsFromImageReference returns a list of pullRefPair for a single ImageReference, depending on the used transport.