helpers.go 1.1 KB

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