David K. Bainbridge | 215e024 | 2017-09-05 23:18:24 -0700 | [diff] [blame] | 1 | package reference |
| 2 | |
| 3 | import "path" |
| 4 | |
| 5 | // IsNameOnly returns true if reference only contains a repo name. |
| 6 | func IsNameOnly(ref Named) bool { |
| 7 | if _, ok := ref.(NamedTagged); ok { |
| 8 | return false |
| 9 | } |
| 10 | if _, ok := ref.(Canonical); ok { |
| 11 | return false |
| 12 | } |
| 13 | return true |
| 14 | } |
| 15 | |
| 16 | // FamiliarName returns the familiar name string |
| 17 | // for the given named, familiarizing if needed. |
| 18 | func FamiliarName(ref Named) string { |
| 19 | if nn, ok := ref.(normalizedNamed); ok { |
| 20 | return nn.Familiar().Name() |
| 21 | } |
| 22 | return ref.Name() |
| 23 | } |
| 24 | |
| 25 | // FamiliarString returns the familiar string representation |
| 26 | // for the given reference, familiarizing if needed. |
| 27 | func FamiliarString(ref Reference) string { |
| 28 | if nn, ok := ref.(normalizedNamed); ok { |
| 29 | return nn.Familiar().String() |
| 30 | } |
| 31 | return ref.String() |
| 32 | } |
| 33 | |
| 34 | // FamiliarMatch reports whether ref matches the specified pattern. |
| 35 | // See https://godoc.org/path#Match for supported patterns. |
| 36 | func FamiliarMatch(pattern string, ref Reference) (bool, error) { |
| 37 | matched, err := path.Match(pattern, FamiliarString(ref)) |
| 38 | if namedRef, isNamed := ref.(Named); isNamed && !matched { |
| 39 | matched, _ = path.Match(pattern, FamiliarName(namedRef)) |
| 40 | } |
| 41 | return matched, err |
| 42 | } |