diff options
author | Tonis Tiigi <tonistiigi@gmail.com> | 2015-12-10 11:01:34 -0800 |
---|---|---|
committer | Tonis Tiigi <tonistiigi@gmail.com> | 2015-12-16 11:58:53 -0800 |
commit | eeb2d4c1adbe4e00f9fbcdc70f9ac31997968e1d (patch) | |
tree | 3e5d9b9195a0f002d1b44b037e515ecd06bc49e9 /reference | |
parent | 15d84a3a48efa12ed8bdc500f28ca58a7b1d1083 (diff) | |
download | docker-eeb2d4c1adbe4e00f9fbcdc70f9ac31997968e1d.tar.gz |
Clean up reference type switches
Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
Diffstat (limited to 'reference')
-rw-r--r-- | reference/reference.go | 19 | ||||
-rw-r--r-- | reference/store.go | 19 |
2 files changed, 22 insertions, 16 deletions
diff --git a/reference/reference.go b/reference/reference.go index 8e887e3637..1e3482475f 100644 --- a/reference/reference.go +++ b/reference/reference.go @@ -132,6 +132,25 @@ func (r *canonicalRef) Digest() digest.Digest { return r.namedRef.Named.(distreference.Canonical).Digest() } +// WithDefaultTag adds a default tag to a reference if it only has a repo name. +func WithDefaultTag(ref Named) Named { + if IsNameOnly(ref) { + ref, _ = WithTag(ref, DefaultTag) + } + return ref +} + +// IsNameOnly returns true if reference only contains a repo name. +func IsNameOnly(ref Named) bool { + if _, ok := ref.(NamedTagged); ok { + return false + } + if _, ok := ref.(Canonical); ok { + return false + } + return true +} + // splitHostname splits a repository name to hostname and remotename string. // If no valid hostname is found, the default hostname is used. Repository name // needs to be already validated before. diff --git a/reference/store.go b/reference/store.go index 85608f7e87..91c5c2aed2 100644 --- a/reference/store.go +++ b/reference/store.go @@ -64,19 +64,6 @@ func (a lexicalAssociations) Len() int { return len(a) } func (a lexicalAssociations) Swap(i, j int) { a[i], a[j] = a[j], a[i] } func (a lexicalAssociations) Less(i, j int) bool { return a[i].Ref.String() < a[j].Ref.String() } -func defaultTagIfNameOnly(ref Named) Named { - switch ref.(type) { - case NamedTagged: - return ref - case Canonical: - return ref - default: - // Should never fail - ref, _ = WithTag(ref, DefaultTag) - return ref - } -} - // NewReferenceStore creates a new reference store, tied to a file path where // the set of references are serialized in JSON format. func NewReferenceStore(jsonPath string) (Store, error) { @@ -107,7 +94,7 @@ func (store *store) AddTag(ref Named, id image.ID, force bool) error { if _, isCanonical := ref.(Canonical); isCanonical { return errors.New("refusing to create a tag with a digest reference") } - return store.addReference(defaultTagIfNameOnly(ref), id, force) + return store.addReference(WithDefaultTag(ref), id, force) } // AddDigest adds a digest reference to the store. @@ -162,7 +149,7 @@ func (store *store) addReference(ref Named, id image.ID, force bool) error { // Delete deletes a reference from the store. It returns true if a deletion // happened, or false otherwise. func (store *store) Delete(ref Named) (bool, error) { - ref = defaultTagIfNameOnly(ref) + ref = WithDefaultTag(ref) store.mu.Lock() defer store.mu.Unlock() @@ -194,7 +181,7 @@ func (store *store) Delete(ref Named) (bool, error) { // Get retrieves an item from the store by func (store *store) Get(ref Named) (image.ID, error) { - ref = defaultTagIfNameOnly(ref) + ref = WithDefaultTag(ref) store.mu.RLock() defer store.mu.RUnlock() |