Return a reference.Named from normalizedTag

Instead of returning a string, return a native value and convert it
into the string in the caller, to make it that small bit more
common to use reference types.

Should not change behavior.

Signed-off-by: Miloslav Trmač <mitr@redhat.com>
This commit is contained in:
Miloslav Trmač
2019-01-09 21:42:37 +01:00
parent b9c0f2c987
commit f92c3ce350
2 changed files with 12 additions and 12 deletions

View File

@@ -452,42 +452,42 @@ func getImageDigest(ctx context.Context, src types.ImageReference, sc *types.Sys
return "@" + digest.Hex(), nil return "@" + digest.Hex(), nil
} }
// normalizeTag returns the canonical version of tag for use in Image.Names() // normalizedTag returns the canonical version of tag for use in Image.Names()
func normalizeTag(tag string) (string, error) { func normalizedTag(tag string) (reference.Named, error) {
decomposedTag, err := decompose(tag) decomposedTag, err := decompose(tag)
if err != nil { if err != nil {
return "", err return nil, err
} }
// If the input doesn't specify a registry, set the registry to localhost // If the input doesn't specify a registry, set the registry to localhost
var ref reference.Named var ref reference.Named
if !decomposedTag.hasRegistry { if !decomposedTag.hasRegistry {
ref, err = decomposedTag.referenceWithRegistry(DefaultLocalRegistry) ref, err = decomposedTag.referenceWithRegistry(DefaultLocalRegistry)
if err != nil { if err != nil {
return "", err return nil, err
} }
} else { } else {
ref, err = decomposedTag.normalizedReference() ref, err = decomposedTag.normalizedReference()
if err != nil { if err != nil {
return "", err return nil, err
} }
} }
// If the input does not have a tag, we need to add one (latest) // If the input does not have a tag, we need to add one (latest)
ref = reference.TagNameOnly(ref) ref = reference.TagNameOnly(ref)
return ref.String(), nil return ref, nil
} }
// TagImage adds a tag to the given image // TagImage adds a tag to the given image
func (i *Image) TagImage(tag string) error { func (i *Image) TagImage(tag string) error {
i.reloadImage() i.reloadImage()
tag, err := normalizeTag(tag) ref, err := normalizedTag(tag)
if err != nil { if err != nil {
return err return err
} }
tags := i.Names() tags := i.Names()
if util.StringInSlice(tag, tags) { if util.StringInSlice(ref.String(), tags) {
return nil return nil
} }
tags = append(tags, tag) tags = append(tags, ref.String())
if err := i.imageruntime.store.SetNames(i.ID(), tags); err != nil { if err := i.imageruntime.store.SetNames(i.ID(), tags); err != nil {
return err return err
} }

View File

@@ -257,7 +257,7 @@ func Test_stripSha256(t *testing.T) {
assert.Equal(t, stripSha256("sha256:a"), "a") assert.Equal(t, stripSha256("sha256:a"), "a")
} }
func TestNormalizeTag(t *testing.T) { func TestNormalizedTag(t *testing.T) {
const digestSuffix = "@sha256:0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef" const digestSuffix = "@sha256:0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
for _, c := range []struct{ input, expected string }{ for _, c := range []struct{ input, expected string }{
@@ -270,12 +270,12 @@ func TestNormalizeTag(t *testing.T) {
{"ns/busybox:latest", "localhost/ns/busybox:latest"}, // Unqualified with a dot-less namespace {"ns/busybox:latest", "localhost/ns/busybox:latest"}, // Unqualified with a dot-less namespace
{"docker.io/busybox:latest", "docker.io/library/busybox:latest"}, // docker.io without /library/ {"docker.io/busybox:latest", "docker.io/library/busybox:latest"}, // docker.io without /library/
} { } {
res, err := normalizeTag(c.input) res, err := normalizedTag(c.input)
if c.expected == "" { if c.expected == "" {
assert.Error(t, err, c.input) assert.Error(t, err, c.input)
} else { } else {
assert.NoError(t, err, c.input) assert.NoError(t, err, c.input)
assert.Equal(t, c.expected, res) assert.Equal(t, c.expected, res.String())
} }
} }
} }