From 2caeb6192a4312e3d8e45b40e0a77ef8ac0d1ce9 Mon Sep 17 00:00:00 2001 From: Nishant Totla Date: Fri, 11 Nov 2016 12:07:27 -0800 Subject: [PATCH] Adding utility functions - IsNameOnly and WithDefaultTag Signed-off-by: Nishant Totla --- reference/helpers.go | 12 ++++++++++++ reference/normalize.go | 22 ++++++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 reference/helpers.go create mode 100644 reference/normalize.go diff --git a/reference/helpers.go b/reference/helpers.go new file mode 100644 index 00000000..dd7ee0ea --- /dev/null +++ b/reference/helpers.go @@ -0,0 +1,12 @@ +package reference + +// 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 +} diff --git a/reference/normalize.go b/reference/normalize.go new file mode 100644 index 00000000..b19a34e3 --- /dev/null +++ b/reference/normalize.go @@ -0,0 +1,22 @@ +package reference + +var ( + defaultTag = "latest" +) + +// EnsureTagged adds the default tag "latest" to a reference if it only has +// a repo name. +func EnsureTagged(ref Named) NamedTagged { + namedTagged, ok := ref.(NamedTagged) + if !ok { + namedTagged, err := WithTag(ref, defaultTag) + if err != nil { + // Default tag must be valid, to create a NamedTagged + // type with non-validated input the WithTag function + // should be used instead + panic(err) + } + return namedTagged + } + return namedTagged +}